stepper.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { Component, Element, Event, h, Listen, Method, Prop, Watch } from "@stencil/core";
  7. /**
  8. * @slot - A slot for adding `calcite-stepper-item`s.
  9. */
  10. export class Stepper {
  11. constructor() {
  12. //--------------------------------------------------------------------------
  13. //
  14. // Public Properties
  15. //
  16. //--------------------------------------------------------------------------
  17. /** optionally display a status icon next to the step title */
  18. this.icon = false;
  19. /** specify the layout of stepper, defaults to horizontal */
  20. this.layout = "horizontal";
  21. /** optionally display the number next to the step title */
  22. this.numbered = false;
  23. /** specify the scale of stepper, defaults to m */
  24. this.scale = "m";
  25. //--------------------------------------------------------------------------
  26. //
  27. // Private State/Props
  28. //
  29. //--------------------------------------------------------------------------
  30. this.itemMap = new Map();
  31. /** list of sorted Stepper items */
  32. this.items = [];
  33. /** list of enabled Stepper items */
  34. this.enabledItems = [];
  35. }
  36. // watch for removal of disabled to register step
  37. contentWatcher() {
  38. if (this.layout === "horizontal") {
  39. if (!this.stepperContentContainer && this.requestedContent) {
  40. this.addHorizontalContentContainer();
  41. }
  42. this.updateContent(this.requestedContent);
  43. }
  44. }
  45. //--------------------------------------------------------------------------
  46. //
  47. // Lifecycle
  48. //
  49. //--------------------------------------------------------------------------
  50. componentDidLoad() {
  51. // if no stepper items are set as active, default to the first one
  52. if (!this.currentPosition) {
  53. this.calciteStepperItemChange.emit({
  54. position: 0
  55. });
  56. }
  57. }
  58. componentWillLoad() {
  59. if (this.layout === "horizontal" && !this.stepperContentContainer) {
  60. this.addHorizontalContentContainer();
  61. }
  62. }
  63. render() {
  64. return h("slot", null);
  65. }
  66. //--------------------------------------------------------------------------
  67. //
  68. // Event Listeners
  69. //
  70. //--------------------------------------------------------------------------
  71. calciteStepperItemKeyEvent(e) {
  72. const item = e.detail.item;
  73. const itemToFocus = e.target;
  74. const isFirstItem = this.itemIndex(itemToFocus) === 0;
  75. const isLastItem = this.itemIndex(itemToFocus) === this.enabledItems.length - 1;
  76. switch (item.key) {
  77. case "ArrowDown":
  78. case "ArrowRight":
  79. if (isLastItem) {
  80. this.focusFirstItem();
  81. }
  82. else {
  83. this.focusNextItem(itemToFocus);
  84. }
  85. break;
  86. case "ArrowUp":
  87. case "ArrowLeft":
  88. if (isFirstItem) {
  89. this.focusLastItem();
  90. }
  91. else {
  92. this.focusPrevItem(itemToFocus);
  93. }
  94. break;
  95. case "Home":
  96. this.focusFirstItem();
  97. break;
  98. case "End":
  99. this.focusLastItem();
  100. break;
  101. }
  102. }
  103. registerItem(event) {
  104. const item = event.target;
  105. const { content, position } = event.detail;
  106. if (content && item.active) {
  107. this.requestedContent = content;
  108. }
  109. this.itemMap.set(item, position);
  110. this.items = this.sortItems();
  111. this.enabledItems = this.filterItems();
  112. }
  113. updateItem(event) {
  114. if (event.detail.content) {
  115. this.requestedContent = event.detail.content;
  116. }
  117. this.currentPosition = event.detail.position;
  118. this.calciteStepperItemChange.emit({
  119. position: this.currentPosition
  120. });
  121. }
  122. //--------------------------------------------------------------------------
  123. //
  124. // Public Methods
  125. //
  126. //--------------------------------------------------------------------------
  127. /** set the next step as active */
  128. async nextStep() {
  129. const enabledStepIndex = this.getEnabledStepIndex(this.currentPosition + 1, "next");
  130. if (typeof enabledStepIndex !== "number") {
  131. return;
  132. }
  133. this.emitChangedItem(enabledStepIndex);
  134. }
  135. /** set the previous step as active */
  136. async prevStep() {
  137. const enabledStepIndex = this.getEnabledStepIndex(this.currentPosition - 1, "previous");
  138. if (typeof enabledStepIndex !== "number") {
  139. return;
  140. }
  141. this.emitChangedItem(enabledStepIndex);
  142. }
  143. /** set the requested step as active */
  144. async goToStep(step) {
  145. const position = step - 1;
  146. if (this.currentPosition !== position) {
  147. this.emitChangedItem(position);
  148. }
  149. }
  150. /** set the first step as active */
  151. async startStep() {
  152. const enabledStepIndex = this.getEnabledStepIndex(0, "next");
  153. if (typeof enabledStepIndex !== "number") {
  154. return;
  155. }
  156. this.emitChangedItem(enabledStepIndex);
  157. }
  158. /** set the last step as active */
  159. async endStep() {
  160. const enabledStepIndex = this.getEnabledStepIndex(this.items.length - 1, "previous");
  161. if (typeof enabledStepIndex !== "number") {
  162. return;
  163. }
  164. this.emitChangedItem(enabledStepIndex);
  165. }
  166. //--------------------------------------------------------------------------
  167. //
  168. // Private Methods
  169. //
  170. //--------------------------------------------------------------------------
  171. getEnabledStepIndex(startIndex, direction = "next") {
  172. var _a;
  173. const { items, currentPosition } = this;
  174. let newIndex = startIndex;
  175. while ((_a = items[newIndex]) === null || _a === void 0 ? void 0 : _a.disabled) {
  176. newIndex = newIndex + (direction === "previous" ? -1 : 1);
  177. }
  178. return newIndex !== currentPosition && newIndex < items.length && newIndex >= 0
  179. ? newIndex
  180. : null;
  181. }
  182. addHorizontalContentContainer() {
  183. this.stepperContentContainer = document.createElement("div");
  184. this.stepperContentContainer.classList.add("calcite-stepper-content");
  185. this.el.insertAdjacentElement("beforeend", this.stepperContentContainer);
  186. }
  187. emitChangedItem(position) {
  188. this.currentPosition = position;
  189. this.calciteStepperItemChange.emit({
  190. position
  191. });
  192. }
  193. focusFirstItem() {
  194. const firstItem = this.enabledItems[0];
  195. this.focusElement(firstItem);
  196. }
  197. focusLastItem() {
  198. const lastItem = this.enabledItems[this.enabledItems.length - 1];
  199. this.focusElement(lastItem);
  200. }
  201. focusNextItem(e) {
  202. const index = this.itemIndex(e);
  203. const nextItem = this.enabledItems[index + 1] || this.enabledItems[0];
  204. this.focusElement(nextItem);
  205. }
  206. focusPrevItem(e) {
  207. const index = this.itemIndex(e);
  208. const prevItem = this.enabledItems[index - 1] || this.enabledItems[this.enabledItems.length - 1];
  209. this.focusElement(prevItem);
  210. }
  211. itemIndex(e) {
  212. return this.enabledItems.indexOf(e);
  213. }
  214. focusElement(item) {
  215. item.focus();
  216. }
  217. sortItems() {
  218. const { itemMap } = this;
  219. return Array.from(itemMap.keys()).sort((a, b) => itemMap.get(a) - itemMap.get(b));
  220. }
  221. filterItems() {
  222. return this.items.filter((item) => !item.disabled);
  223. }
  224. updateContent(content) {
  225. this.stepperContentContainer.innerHTML = "";
  226. this.stepperContentContainer.append(...content);
  227. }
  228. static get is() { return "calcite-stepper"; }
  229. static get encapsulation() { return "shadow"; }
  230. static get originalStyleUrls() { return {
  231. "$": ["stepper.scss"]
  232. }; }
  233. static get styleUrls() { return {
  234. "$": ["stepper.css"]
  235. }; }
  236. static get properties() { return {
  237. "icon": {
  238. "type": "boolean",
  239. "mutable": false,
  240. "complexType": {
  241. "original": "boolean",
  242. "resolved": "boolean",
  243. "references": {}
  244. },
  245. "required": false,
  246. "optional": false,
  247. "docs": {
  248. "tags": [],
  249. "text": "optionally display a status icon next to the step title"
  250. },
  251. "attribute": "icon",
  252. "reflect": true,
  253. "defaultValue": "false"
  254. },
  255. "layout": {
  256. "type": "string",
  257. "mutable": false,
  258. "complexType": {
  259. "original": "Layout",
  260. "resolved": "\"grid\" | \"horizontal\" | \"vertical\"",
  261. "references": {
  262. "Layout": {
  263. "location": "import",
  264. "path": "../interfaces"
  265. }
  266. }
  267. },
  268. "required": false,
  269. "optional": false,
  270. "docs": {
  271. "tags": [],
  272. "text": "specify the layout of stepper, defaults to horizontal"
  273. },
  274. "attribute": "layout",
  275. "reflect": true,
  276. "defaultValue": "\"horizontal\""
  277. },
  278. "numbered": {
  279. "type": "boolean",
  280. "mutable": false,
  281. "complexType": {
  282. "original": "boolean",
  283. "resolved": "boolean",
  284. "references": {}
  285. },
  286. "required": false,
  287. "optional": false,
  288. "docs": {
  289. "tags": [],
  290. "text": "optionally display the number next to the step title"
  291. },
  292. "attribute": "numbered",
  293. "reflect": true,
  294. "defaultValue": "false"
  295. },
  296. "scale": {
  297. "type": "string",
  298. "mutable": false,
  299. "complexType": {
  300. "original": "Scale",
  301. "resolved": "\"l\" | \"m\" | \"s\"",
  302. "references": {
  303. "Scale": {
  304. "location": "import",
  305. "path": "../interfaces"
  306. }
  307. }
  308. },
  309. "required": false,
  310. "optional": false,
  311. "docs": {
  312. "tags": [],
  313. "text": "specify the scale of stepper, defaults to m"
  314. },
  315. "attribute": "scale",
  316. "reflect": true,
  317. "defaultValue": "\"m\""
  318. },
  319. "requestedContent": {
  320. "type": "unknown",
  321. "mutable": true,
  322. "complexType": {
  323. "original": "Node[]",
  324. "resolved": "Node[]",
  325. "references": {
  326. "Node": {
  327. "location": "global"
  328. }
  329. }
  330. },
  331. "required": false,
  332. "optional": false,
  333. "docs": {
  334. "tags": [{
  335. "name": "internal",
  336. "text": undefined
  337. }],
  338. "text": ""
  339. }
  340. }
  341. }; }
  342. static get events() { return [{
  343. "method": "calciteStepperItemChange",
  344. "name": "calciteStepperItemChange",
  345. "bubbles": true,
  346. "cancelable": true,
  347. "composed": true,
  348. "docs": {
  349. "tags": [{
  350. "name": "internal",
  351. "text": undefined
  352. }],
  353. "text": "This event fires when the active stepper item has changed."
  354. },
  355. "complexType": {
  356. "original": "StepperItemChangeEventDetail",
  357. "resolved": "StepperItemChangeEventDetail",
  358. "references": {
  359. "StepperItemChangeEventDetail": {
  360. "location": "import",
  361. "path": "./interfaces"
  362. }
  363. }
  364. }
  365. }]; }
  366. static get methods() { return {
  367. "nextStep": {
  368. "complexType": {
  369. "signature": "() => Promise<void>",
  370. "parameters": [],
  371. "references": {
  372. "Promise": {
  373. "location": "global"
  374. }
  375. },
  376. "return": "Promise<void>"
  377. },
  378. "docs": {
  379. "text": "set the next step as active",
  380. "tags": []
  381. }
  382. },
  383. "prevStep": {
  384. "complexType": {
  385. "signature": "() => Promise<void>",
  386. "parameters": [],
  387. "references": {
  388. "Promise": {
  389. "location": "global"
  390. }
  391. },
  392. "return": "Promise<void>"
  393. },
  394. "docs": {
  395. "text": "set the previous step as active",
  396. "tags": []
  397. }
  398. },
  399. "goToStep": {
  400. "complexType": {
  401. "signature": "(step: number) => Promise<void>",
  402. "parameters": [{
  403. "tags": [],
  404. "text": ""
  405. }],
  406. "references": {
  407. "Promise": {
  408. "location": "global"
  409. }
  410. },
  411. "return": "Promise<void>"
  412. },
  413. "docs": {
  414. "text": "set the requested step as active",
  415. "tags": []
  416. }
  417. },
  418. "startStep": {
  419. "complexType": {
  420. "signature": "() => Promise<void>",
  421. "parameters": [],
  422. "references": {
  423. "Promise": {
  424. "location": "global"
  425. }
  426. },
  427. "return": "Promise<void>"
  428. },
  429. "docs": {
  430. "text": "set the first step as active",
  431. "tags": []
  432. }
  433. },
  434. "endStep": {
  435. "complexType": {
  436. "signature": "() => Promise<void>",
  437. "parameters": [],
  438. "references": {
  439. "Promise": {
  440. "location": "global"
  441. }
  442. },
  443. "return": "Promise<void>"
  444. },
  445. "docs": {
  446. "text": "set the last step as active",
  447. "tags": []
  448. }
  449. }
  450. }; }
  451. static get elementRef() { return "el"; }
  452. static get watchers() { return [{
  453. "propName": "requestedContent",
  454. "methodName": "contentWatcher"
  455. }]; }
  456. static get listeners() { return [{
  457. "name": "calciteStepperItemKeyEvent",
  458. "method": "calciteStepperItemKeyEvent",
  459. "target": undefined,
  460. "capture": false,
  461. "passive": false
  462. }, {
  463. "name": "calciteStepperItemRegister",
  464. "method": "registerItem",
  465. "target": undefined,
  466. "capture": false,
  467. "passive": false
  468. }, {
  469. "name": "calciteStepperItemSelect",
  470. "method": "updateItem",
  471. "target": undefined,
  472. "capture": false,
  473. "passive": false
  474. }]; }
  475. }