stepper-item.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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, Host, Listen, Prop, Watch } from "@stencil/core";
  7. import { getElementProp, toAriaBoolean } from "../../utils/dom";
  8. import { updateHostInteraction } from "../../utils/interactive";
  9. /**
  10. * @slot - A slot for adding custom content.
  11. */
  12. export class StepperItem {
  13. constructor() {
  14. //--------------------------------------------------------------------------
  15. //
  16. // Public Properties
  17. //
  18. //--------------------------------------------------------------------------
  19. /** is the step active */
  20. this.active = false;
  21. /** has the step been completed */
  22. this.complete = false;
  23. /** does the step contain an error that needs to be resolved by the user */
  24. this.error = false;
  25. /** is the step disabled and not navigable to by a user */
  26. this.disabled = false;
  27. /** should the items display an icon based on status */
  28. /** @internal */
  29. this.icon = false;
  30. /** optionally display the step number next to the title and subtitle */
  31. /** @internal */
  32. this.numbered = false;
  33. /** the scale of the item */
  34. /** @internal */
  35. this.scale = "m";
  36. //--------------------------------------------------------------------------
  37. //
  38. // Private Methods
  39. //
  40. //--------------------------------------------------------------------------
  41. this.keyDownHandler = (e) => {
  42. if (!this.disabled && e.target === this.el) {
  43. switch (e.key) {
  44. case " ":
  45. case "Enter":
  46. this.emitRequestedItem();
  47. e.preventDefault();
  48. break;
  49. case "ArrowUp":
  50. case "ArrowDown":
  51. case "ArrowLeft":
  52. case "ArrowRight":
  53. case "Home":
  54. case "End":
  55. this.calciteStepperItemKeyEvent.emit({ item: e });
  56. e.preventDefault();
  57. break;
  58. }
  59. }
  60. };
  61. this.emitRequestedItem = () => {
  62. if (!this.disabled) {
  63. this.calciteStepperItemSelect.emit({
  64. position: this.itemPosition,
  65. content: this.itemContent
  66. });
  67. }
  68. };
  69. this.setItemContent = (event) => {
  70. this.itemPosition = this.getItemPosition();
  71. const itemContent = event.target.assignedNodes({ flatten: true });
  72. if (itemContent.length) {
  73. this.itemContent = itemContent;
  74. }
  75. this.registerStepperItem();
  76. if (this.active) {
  77. this.emitRequestedItem();
  78. }
  79. };
  80. }
  81. // watch for removal of disabled to register step
  82. disabledWatcher() {
  83. this.registerStepperItem();
  84. }
  85. //--------------------------------------------------------------------------
  86. //
  87. // Lifecycle
  88. //
  89. //--------------------------------------------------------------------------
  90. componentWillLoad() {
  91. this.icon = getElementProp(this.el, "icon", false);
  92. this.numbered = getElementProp(this.el, "numbered", false);
  93. this.layout = getElementProp(this.el, "layout", false);
  94. this.scale = getElementProp(this.el, "scale", "m");
  95. this.parentStepperEl = this.el.parentElement;
  96. }
  97. componentDidRender() {
  98. updateHostInteraction(this, true);
  99. }
  100. render() {
  101. return (h(Host, { "aria-expanded": toAriaBoolean(this.active), onClick: this.emitRequestedItem, onKeyDown: this.keyDownHandler },
  102. h("div", { class: "container" },
  103. h("div", { class: "stepper-item-header" },
  104. this.icon ? this.renderIcon() : null,
  105. this.numbered ? (h("div", { class: "stepper-item-number" },
  106. this.getItemPosition() + 1,
  107. ".")) : null,
  108. h("div", { class: "stepper-item-header-text" },
  109. h("span", { class: "stepper-item-title" }, this.itemTitle),
  110. h("span", { class: "stepper-item-subtitle" }, this.itemSubtitle))),
  111. h("div", { class: "stepper-item-content" },
  112. h("slot", { onSlotchange: this.setItemContent })))));
  113. }
  114. //--------------------------------------------------------------------------
  115. //
  116. // Event Listeners
  117. //
  118. //--------------------------------------------------------------------------
  119. updateActiveItemOnChange(event) {
  120. if (event.target === this.parentStepperEl ||
  121. event.composedPath().includes(this.parentStepperEl)) {
  122. this.activePosition = event.detail.position;
  123. this.determineActiveItem();
  124. }
  125. }
  126. renderIcon() {
  127. const path = this.active
  128. ? "circleF"
  129. : this.error
  130. ? "exclamationMarkCircleF"
  131. : this.complete
  132. ? "checkCircleF"
  133. : "circle";
  134. return h("calcite-icon", { class: "stepper-item-icon", icon: path, scale: "s" });
  135. }
  136. determineActiveItem() {
  137. this.active = !this.disabled && this.itemPosition === this.activePosition;
  138. }
  139. registerStepperItem() {
  140. this.calciteStepperItemRegister.emit({
  141. position: this.itemPosition,
  142. content: this.itemContent
  143. });
  144. }
  145. getItemPosition() {
  146. return Array.prototype.indexOf.call(this.parentStepperEl.querySelectorAll("calcite-stepper-item"), this.el);
  147. }
  148. static get is() { return "calcite-stepper-item"; }
  149. static get encapsulation() { return "shadow"; }
  150. static get originalStyleUrls() { return {
  151. "$": ["stepper-item.scss"]
  152. }; }
  153. static get styleUrls() { return {
  154. "$": ["stepper-item.css"]
  155. }; }
  156. static get properties() { return {
  157. "active": {
  158. "type": "boolean",
  159. "mutable": true,
  160. "complexType": {
  161. "original": "boolean",
  162. "resolved": "boolean",
  163. "references": {}
  164. },
  165. "required": false,
  166. "optional": false,
  167. "docs": {
  168. "tags": [],
  169. "text": "is the step active"
  170. },
  171. "attribute": "active",
  172. "reflect": true,
  173. "defaultValue": "false"
  174. },
  175. "complete": {
  176. "type": "boolean",
  177. "mutable": false,
  178. "complexType": {
  179. "original": "boolean",
  180. "resolved": "boolean",
  181. "references": {}
  182. },
  183. "required": false,
  184. "optional": false,
  185. "docs": {
  186. "tags": [],
  187. "text": "has the step been completed"
  188. },
  189. "attribute": "complete",
  190. "reflect": true,
  191. "defaultValue": "false"
  192. },
  193. "error": {
  194. "type": "boolean",
  195. "mutable": false,
  196. "complexType": {
  197. "original": "boolean",
  198. "resolved": "boolean",
  199. "references": {}
  200. },
  201. "required": false,
  202. "optional": false,
  203. "docs": {
  204. "tags": [],
  205. "text": "does the step contain an error that needs to be resolved by the user"
  206. },
  207. "attribute": "error",
  208. "reflect": false,
  209. "defaultValue": "false"
  210. },
  211. "disabled": {
  212. "type": "boolean",
  213. "mutable": false,
  214. "complexType": {
  215. "original": "boolean",
  216. "resolved": "boolean",
  217. "references": {}
  218. },
  219. "required": false,
  220. "optional": false,
  221. "docs": {
  222. "tags": [],
  223. "text": "is the step disabled and not navigable to by a user"
  224. },
  225. "attribute": "disabled",
  226. "reflect": true,
  227. "defaultValue": "false"
  228. },
  229. "itemTitle": {
  230. "type": "string",
  231. "mutable": false,
  232. "complexType": {
  233. "original": "string",
  234. "resolved": "string",
  235. "references": {}
  236. },
  237. "required": false,
  238. "optional": true,
  239. "docs": {
  240. "tags": [],
  241. "text": "pass a title for the stepper item"
  242. },
  243. "attribute": "item-title",
  244. "reflect": false
  245. },
  246. "itemSubtitle": {
  247. "type": "string",
  248. "mutable": false,
  249. "complexType": {
  250. "original": "string",
  251. "resolved": "string",
  252. "references": {}
  253. },
  254. "required": false,
  255. "optional": true,
  256. "docs": {
  257. "tags": [],
  258. "text": "pass a title for the stepper item"
  259. },
  260. "attribute": "item-subtitle",
  261. "reflect": false
  262. },
  263. "layout": {
  264. "type": "string",
  265. "mutable": true,
  266. "complexType": {
  267. "original": "string",
  268. "resolved": "string",
  269. "references": {}
  270. },
  271. "required": false,
  272. "optional": true,
  273. "docs": {
  274. "tags": [{
  275. "name": "internal",
  276. "text": undefined
  277. }],
  278. "text": ""
  279. },
  280. "attribute": "layout",
  281. "reflect": true
  282. },
  283. "icon": {
  284. "type": "boolean",
  285. "mutable": true,
  286. "complexType": {
  287. "original": "boolean",
  288. "resolved": "boolean",
  289. "references": {}
  290. },
  291. "required": false,
  292. "optional": false,
  293. "docs": {
  294. "tags": [{
  295. "name": "internal",
  296. "text": undefined
  297. }],
  298. "text": ""
  299. },
  300. "attribute": "icon",
  301. "reflect": false,
  302. "defaultValue": "false"
  303. },
  304. "numbered": {
  305. "type": "boolean",
  306. "mutable": true,
  307. "complexType": {
  308. "original": "boolean",
  309. "resolved": "boolean",
  310. "references": {}
  311. },
  312. "required": false,
  313. "optional": false,
  314. "docs": {
  315. "tags": [{
  316. "name": "internal",
  317. "text": undefined
  318. }],
  319. "text": ""
  320. },
  321. "attribute": "numbered",
  322. "reflect": false,
  323. "defaultValue": "false"
  324. },
  325. "scale": {
  326. "type": "string",
  327. "mutable": true,
  328. "complexType": {
  329. "original": "Scale",
  330. "resolved": "\"l\" | \"m\" | \"s\"",
  331. "references": {
  332. "Scale": {
  333. "location": "import",
  334. "path": "../interfaces"
  335. }
  336. }
  337. },
  338. "required": false,
  339. "optional": false,
  340. "docs": {
  341. "tags": [{
  342. "name": "internal",
  343. "text": undefined
  344. }],
  345. "text": ""
  346. },
  347. "attribute": "scale",
  348. "reflect": true,
  349. "defaultValue": "\"m\""
  350. }
  351. }; }
  352. static get events() { return [{
  353. "method": "calciteStepperItemKeyEvent",
  354. "name": "calciteStepperItemKeyEvent",
  355. "bubbles": true,
  356. "cancelable": true,
  357. "composed": true,
  358. "docs": {
  359. "tags": [{
  360. "name": "internal",
  361. "text": undefined
  362. }],
  363. "text": ""
  364. },
  365. "complexType": {
  366. "original": "StepperItemKeyEventDetail",
  367. "resolved": "StepperItemKeyEventDetail",
  368. "references": {
  369. "StepperItemKeyEventDetail": {
  370. "location": "import",
  371. "path": "../stepper/interfaces"
  372. }
  373. }
  374. }
  375. }, {
  376. "method": "calciteStepperItemSelect",
  377. "name": "calciteStepperItemSelect",
  378. "bubbles": true,
  379. "cancelable": true,
  380. "composed": true,
  381. "docs": {
  382. "tags": [{
  383. "name": "internal",
  384. "text": undefined
  385. }],
  386. "text": ""
  387. },
  388. "complexType": {
  389. "original": "StepperItemEventDetail",
  390. "resolved": "StepperItemEventDetail",
  391. "references": {
  392. "StepperItemEventDetail": {
  393. "location": "import",
  394. "path": "../stepper/interfaces"
  395. }
  396. }
  397. }
  398. }, {
  399. "method": "calciteStepperItemRegister",
  400. "name": "calciteStepperItemRegister",
  401. "bubbles": true,
  402. "cancelable": true,
  403. "composed": true,
  404. "docs": {
  405. "tags": [{
  406. "name": "internal",
  407. "text": undefined
  408. }],
  409. "text": ""
  410. },
  411. "complexType": {
  412. "original": "StepperItemEventDetail",
  413. "resolved": "StepperItemEventDetail",
  414. "references": {
  415. "StepperItemEventDetail": {
  416. "location": "import",
  417. "path": "../stepper/interfaces"
  418. }
  419. }
  420. }
  421. }]; }
  422. static get elementRef() { return "el"; }
  423. static get watchers() { return [{
  424. "propName": "disabled",
  425. "methodName": "disabledWatcher"
  426. }]; }
  427. static get listeners() { return [{
  428. "name": "calciteStepperItemChange",
  429. "method": "updateActiveItemOnChange",
  430. "target": "body",
  431. "capture": false,
  432. "passive": false
  433. }]; }
  434. }