stepper.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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.97
  5. */
  6. import { h } from "@stencil/core";
  7. import { focusElement } from "../../utils/dom";
  8. /**
  9. * @slot - A slot for adding `calcite-stepper-item`s.
  10. */
  11. export class Stepper {
  12. constructor() {
  13. //--------------------------------------------------------------------------
  14. //
  15. // Public Properties
  16. //
  17. //--------------------------------------------------------------------------
  18. /** When `true`, displays a status icon in the `calcite-stepper-item` heading. */
  19. this.icon = false;
  20. /** Defines the layout of the component. */
  21. this.layout = "horizontal";
  22. /** When `true`, displays the step number in the `calcite-stepper-item` heading. */
  23. this.numbered = false;
  24. /** Specifies the size of the component. */
  25. this.scale = "m";
  26. //--------------------------------------------------------------------------
  27. //
  28. // Private State/Props
  29. //
  30. //--------------------------------------------------------------------------
  31. this.itemMap = new Map();
  32. /** list of sorted Stepper items */
  33. this.items = [];
  34. /** list of enabled Stepper items */
  35. this.enabledItems = [];
  36. }
  37. //--------------------------------------------------------------------------
  38. //
  39. // Lifecycle
  40. //
  41. //--------------------------------------------------------------------------
  42. componentDidLoad() {
  43. // if no stepper items are set as active, default to the first one
  44. if (typeof this.currentPosition !== "number") {
  45. this.calciteInternalStepperItemChange.emit({
  46. position: 0
  47. });
  48. }
  49. }
  50. render() {
  51. return (h("slot", { onSlotchange: (event) => {
  52. const items = event.currentTarget
  53. .assignedElements()
  54. .filter((el) => (el === null || el === void 0 ? void 0 : el.tagName) === "CALCITE-STEPPER-ITEM");
  55. const spacing = Array(items.length).fill("1fr").join(" ");
  56. this.el.style.gridTemplateAreas = spacing;
  57. this.el.style.gridTemplateColumns = spacing;
  58. } }));
  59. }
  60. //--------------------------------------------------------------------------
  61. //
  62. // Event Listeners
  63. //
  64. //--------------------------------------------------------------------------
  65. calciteInternalStepperItemKeyEvent(event) {
  66. const item = event.detail.item;
  67. const itemToFocus = event.target;
  68. const isFirstItem = this.itemIndex(itemToFocus) === 0;
  69. const isLastItem = this.itemIndex(itemToFocus) === this.enabledItems.length - 1;
  70. switch (item.key) {
  71. case "ArrowDown":
  72. case "ArrowRight":
  73. if (isLastItem) {
  74. this.focusFirstItem();
  75. }
  76. else {
  77. this.focusNextItem(itemToFocus);
  78. }
  79. break;
  80. case "ArrowUp":
  81. case "ArrowLeft":
  82. if (isFirstItem) {
  83. this.focusLastItem();
  84. }
  85. else {
  86. this.focusPrevItem(itemToFocus);
  87. }
  88. break;
  89. case "Home":
  90. this.focusFirstItem();
  91. break;
  92. case "End":
  93. this.focusLastItem();
  94. break;
  95. }
  96. event.stopPropagation();
  97. }
  98. registerItem(event) {
  99. const item = event.target;
  100. const { content, position } = event.detail;
  101. this.itemMap.set(item, { position, content });
  102. this.items = this.sortItems();
  103. this.enabledItems = this.filterItems();
  104. event.stopPropagation();
  105. }
  106. updateItem(event) {
  107. const { position } = event.detail;
  108. if (typeof position === "number") {
  109. this.currentPosition = position;
  110. }
  111. this.calciteInternalStepperItemChange.emit({
  112. position
  113. });
  114. }
  115. handleUserRequestedStepperItemSelect(event) {
  116. const { position } = event.detail;
  117. this.calciteStepperItemChange.emit({
  118. position
  119. });
  120. }
  121. //--------------------------------------------------------------------------
  122. //
  123. // Public Methods
  124. //
  125. //--------------------------------------------------------------------------
  126. /** Set the next `calcite-stepper-item` as active. */
  127. async nextStep() {
  128. const enabledStepIndex = this.getEnabledStepIndex(this.currentPosition + 1, "next");
  129. if (typeof enabledStepIndex !== "number") {
  130. return;
  131. }
  132. this.updateStep(enabledStepIndex);
  133. }
  134. /** Set the previous `calcite-stepper-item` as active. */
  135. async prevStep() {
  136. const enabledStepIndex = this.getEnabledStepIndex(this.currentPosition - 1, "previous");
  137. if (typeof enabledStepIndex !== "number") {
  138. return;
  139. }
  140. this.updateStep(enabledStepIndex);
  141. }
  142. /**
  143. * Set a specified `calcite-stepper-item` as active.
  144. *
  145. * @param step
  146. */
  147. async goToStep(step) {
  148. const position = step - 1;
  149. if (this.currentPosition !== position) {
  150. this.updateStep(position);
  151. }
  152. }
  153. /** Set the first `calcite-stepper-item` as active. */
  154. async startStep() {
  155. const enabledStepIndex = this.getEnabledStepIndex(0, "next");
  156. if (typeof enabledStepIndex !== "number") {
  157. return;
  158. }
  159. this.updateStep(enabledStepIndex);
  160. }
  161. /** Set the last `calcite-stepper-item` as active. */
  162. async endStep() {
  163. const enabledStepIndex = this.getEnabledStepIndex(this.items.length - 1, "previous");
  164. if (typeof enabledStepIndex !== "number") {
  165. return;
  166. }
  167. this.updateStep(enabledStepIndex);
  168. }
  169. //--------------------------------------------------------------------------
  170. //
  171. // Private Methods
  172. //
  173. //--------------------------------------------------------------------------
  174. getEnabledStepIndex(startIndex, direction = "next") {
  175. var _a;
  176. const { items, currentPosition } = this;
  177. let newIndex = startIndex;
  178. while ((_a = items[newIndex]) === null || _a === void 0 ? void 0 : _a.disabled) {
  179. newIndex = newIndex + (direction === "previous" ? -1 : 1);
  180. }
  181. return newIndex !== currentPosition && newIndex < items.length && newIndex >= 0
  182. ? newIndex
  183. : null;
  184. }
  185. updateStep(position) {
  186. this.currentPosition = position;
  187. this.calciteInternalStepperItemChange.emit({
  188. position
  189. });
  190. }
  191. focusFirstItem() {
  192. const firstItem = this.enabledItems[0];
  193. focusElement(firstItem);
  194. }
  195. focusLastItem() {
  196. const lastItem = this.enabledItems[this.enabledItems.length - 1];
  197. focusElement(lastItem);
  198. }
  199. focusNextItem(el) {
  200. const index = this.itemIndex(el);
  201. const nextItem = this.enabledItems[index + 1] || this.enabledItems[0];
  202. focusElement(nextItem);
  203. }
  204. focusPrevItem(el) {
  205. const index = this.itemIndex(el);
  206. const prevItem = this.enabledItems[index - 1] || this.enabledItems[this.enabledItems.length - 1];
  207. focusElement(prevItem);
  208. }
  209. itemIndex(el) {
  210. return this.enabledItems.indexOf(el);
  211. }
  212. sortItems() {
  213. const { itemMap } = this;
  214. return Array.from(itemMap.keys()).sort((a, b) => itemMap.get(a).position - itemMap.get(b).position);
  215. }
  216. filterItems() {
  217. return this.items.filter((item) => !item.disabled);
  218. }
  219. static get is() { return "calcite-stepper"; }
  220. static get encapsulation() { return "shadow"; }
  221. static get originalStyleUrls() {
  222. return {
  223. "$": ["stepper.scss"]
  224. };
  225. }
  226. static get styleUrls() {
  227. return {
  228. "$": ["stepper.css"]
  229. };
  230. }
  231. static get properties() {
  232. return {
  233. "icon": {
  234. "type": "boolean",
  235. "mutable": false,
  236. "complexType": {
  237. "original": "boolean",
  238. "resolved": "boolean",
  239. "references": {}
  240. },
  241. "required": false,
  242. "optional": false,
  243. "docs": {
  244. "tags": [],
  245. "text": "When `true`, displays a status icon in the `calcite-stepper-item` heading."
  246. },
  247. "attribute": "icon",
  248. "reflect": true,
  249. "defaultValue": "false"
  250. },
  251. "layout": {
  252. "type": "string",
  253. "mutable": false,
  254. "complexType": {
  255. "original": "Extract<\"horizontal\" | \"vertical\", Layout>",
  256. "resolved": "\"horizontal\" | \"vertical\"",
  257. "references": {
  258. "Extract": {
  259. "location": "global"
  260. },
  261. "Layout": {
  262. "location": "import",
  263. "path": "../interfaces"
  264. }
  265. }
  266. },
  267. "required": false,
  268. "optional": false,
  269. "docs": {
  270. "tags": [],
  271. "text": "Defines the layout of the component."
  272. },
  273. "attribute": "layout",
  274. "reflect": true,
  275. "defaultValue": "\"horizontal\""
  276. },
  277. "numbered": {
  278. "type": "boolean",
  279. "mutable": false,
  280. "complexType": {
  281. "original": "boolean",
  282. "resolved": "boolean",
  283. "references": {}
  284. },
  285. "required": false,
  286. "optional": false,
  287. "docs": {
  288. "tags": [],
  289. "text": "When `true`, displays the step number in the `calcite-stepper-item` heading."
  290. },
  291. "attribute": "numbered",
  292. "reflect": true,
  293. "defaultValue": "false"
  294. },
  295. "numberingSystem": {
  296. "type": "string",
  297. "mutable": false,
  298. "complexType": {
  299. "original": "NumberingSystem",
  300. "resolved": "\"arab\" | \"arabext\" | \"bali\" | \"beng\" | \"deva\" | \"fullwide\" | \"gujr\" | \"guru\" | \"hanidec\" | \"khmr\" | \"knda\" | \"laoo\" | \"latn\" | \"limb\" | \"mlym\" | \"mong\" | \"mymr\" | \"orya\" | \"tamldec\" | \"telu\" | \"thai\" | \"tibt\"",
  301. "references": {
  302. "NumberingSystem": {
  303. "location": "import",
  304. "path": "../../utils/locale"
  305. }
  306. }
  307. },
  308. "required": false,
  309. "optional": true,
  310. "docs": {
  311. "tags": [],
  312. "text": "Specifies the Unicode numeral system used by the component for localization."
  313. },
  314. "attribute": "numbering-system",
  315. "reflect": true
  316. },
  317. "scale": {
  318. "type": "string",
  319. "mutable": false,
  320. "complexType": {
  321. "original": "Scale",
  322. "resolved": "\"l\" | \"m\" | \"s\"",
  323. "references": {
  324. "Scale": {
  325. "location": "import",
  326. "path": "../interfaces"
  327. }
  328. }
  329. },
  330. "required": false,
  331. "optional": false,
  332. "docs": {
  333. "tags": [],
  334. "text": "Specifies the size of the component."
  335. },
  336. "attribute": "scale",
  337. "reflect": true,
  338. "defaultValue": "\"m\""
  339. }
  340. };
  341. }
  342. static get events() {
  343. return [{
  344. "method": "calciteStepperItemChange",
  345. "name": "calciteStepperItemChange",
  346. "bubbles": true,
  347. "cancelable": false,
  348. "composed": true,
  349. "docs": {
  350. "tags": [],
  351. "text": "Fires when the active `calcite-stepper-item` changes."
  352. },
  353. "complexType": {
  354. "original": "StepperItemChangeEventDetail",
  355. "resolved": "StepperItemChangeEventDetail",
  356. "references": {
  357. "StepperItemChangeEventDetail": {
  358. "location": "import",
  359. "path": "./interfaces"
  360. }
  361. }
  362. }
  363. }, {
  364. "method": "calciteInternalStepperItemChange",
  365. "name": "calciteInternalStepperItemChange",
  366. "bubbles": true,
  367. "cancelable": false,
  368. "composed": true,
  369. "docs": {
  370. "tags": [{
  371. "name": "internal",
  372. "text": undefined
  373. }],
  374. "text": "Fires when the active `calcite-stepper-item` changes."
  375. },
  376. "complexType": {
  377. "original": "StepperItemChangeEventDetail",
  378. "resolved": "StepperItemChangeEventDetail",
  379. "references": {
  380. "StepperItemChangeEventDetail": {
  381. "location": "import",
  382. "path": "./interfaces"
  383. }
  384. }
  385. }
  386. }];
  387. }
  388. static get methods() {
  389. return {
  390. "nextStep": {
  391. "complexType": {
  392. "signature": "() => Promise<void>",
  393. "parameters": [],
  394. "references": {
  395. "Promise": {
  396. "location": "global"
  397. }
  398. },
  399. "return": "Promise<void>"
  400. },
  401. "docs": {
  402. "text": "Set the next `calcite-stepper-item` as active.",
  403. "tags": []
  404. }
  405. },
  406. "prevStep": {
  407. "complexType": {
  408. "signature": "() => Promise<void>",
  409. "parameters": [],
  410. "references": {
  411. "Promise": {
  412. "location": "global"
  413. }
  414. },
  415. "return": "Promise<void>"
  416. },
  417. "docs": {
  418. "text": "Set the previous `calcite-stepper-item` as active.",
  419. "tags": []
  420. }
  421. },
  422. "goToStep": {
  423. "complexType": {
  424. "signature": "(step: number) => Promise<void>",
  425. "parameters": [{
  426. "tags": [{
  427. "name": "param",
  428. "text": "step"
  429. }],
  430. "text": ""
  431. }],
  432. "references": {
  433. "Promise": {
  434. "location": "global"
  435. }
  436. },
  437. "return": "Promise<void>"
  438. },
  439. "docs": {
  440. "text": "Set a specified `calcite-stepper-item` as active.",
  441. "tags": [{
  442. "name": "param",
  443. "text": "step"
  444. }]
  445. }
  446. },
  447. "startStep": {
  448. "complexType": {
  449. "signature": "() => Promise<void>",
  450. "parameters": [],
  451. "references": {
  452. "Promise": {
  453. "location": "global"
  454. }
  455. },
  456. "return": "Promise<void>"
  457. },
  458. "docs": {
  459. "text": "Set the first `calcite-stepper-item` as active.",
  460. "tags": []
  461. }
  462. },
  463. "endStep": {
  464. "complexType": {
  465. "signature": "() => Promise<void>",
  466. "parameters": [],
  467. "references": {
  468. "Promise": {
  469. "location": "global"
  470. }
  471. },
  472. "return": "Promise<void>"
  473. },
  474. "docs": {
  475. "text": "Set the last `calcite-stepper-item` as active.",
  476. "tags": []
  477. }
  478. }
  479. };
  480. }
  481. static get elementRef() { return "el"; }
  482. static get listeners() {
  483. return [{
  484. "name": "calciteInternalStepperItemKeyEvent",
  485. "method": "calciteInternalStepperItemKeyEvent",
  486. "target": undefined,
  487. "capture": false,
  488. "passive": false
  489. }, {
  490. "name": "calciteInternalStepperItemRegister",
  491. "method": "registerItem",
  492. "target": undefined,
  493. "capture": false,
  494. "passive": false
  495. }, {
  496. "name": "calciteInternalStepperItemSelect",
  497. "method": "updateItem",
  498. "target": undefined,
  499. "capture": false,
  500. "passive": false
  501. }, {
  502. "name": "calciteInternalUserRequestedStepperItemSelect",
  503. "method": "handleUserRequestedStepperItemSelect",
  504. "target": undefined,
  505. "capture": false,
  506. "passive": false
  507. }];
  508. }
  509. }