combobox-item.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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, Host, Method, Prop, h, Watch } from "@stencil/core";
  7. import { getElementProp, getSlotted } from "../../utils/dom";
  8. import { CSS } from "./resources";
  9. import { guid } from "../../utils/guid";
  10. import { getAncestors, getDepth } from "../combobox/utils";
  11. import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot";
  12. import { updateHostInteraction } from "../../utils/interactive";
  13. /**
  14. * @slot - A slot for adding nested `calcite-combobox-item`s.
  15. */
  16. export class ComboboxItem {
  17. constructor() {
  18. // --------------------------------------------------------------------------
  19. //
  20. // Properties
  21. //
  22. // --------------------------------------------------------------------------
  23. /** When true, the item cannot be clicked and is visually muted. */
  24. this.disabled = false;
  25. /** Set this to true to pre-select an item. Toggles when an item is checked/unchecked. */
  26. this.selected = false;
  27. /** True when item is highlighted either from keyboard or mouse hover */
  28. this.active = false;
  29. /** Unique identifier, used for accessibility */
  30. this.guid = guid();
  31. this.scale = "m";
  32. // --------------------------------------------------------------------------
  33. //
  34. // Private Methods
  35. //
  36. // --------------------------------------------------------------------------
  37. this.itemClickHandler = (event) => {
  38. event.preventDefault();
  39. if (this.disabled) {
  40. return;
  41. }
  42. this.selected = !this.selected;
  43. };
  44. }
  45. selectedWatchHandler() {
  46. this.calciteComboboxItemChange.emit(this.el);
  47. }
  48. // --------------------------------------------------------------------------
  49. //
  50. // Lifecycle
  51. //
  52. // --------------------------------------------------------------------------
  53. connectedCallback() {
  54. this.ancestors = getAncestors(this.el);
  55. this.scale = getElementProp(this.el, "scale", this.scale);
  56. connectConditionalSlotComponent(this);
  57. }
  58. disconnectedCallback() {
  59. disconnectConditionalSlotComponent(this);
  60. }
  61. componentDidRender() {
  62. updateHostInteraction(this);
  63. }
  64. // --------------------------------------------------------------------------
  65. //
  66. // Public Methods
  67. //
  68. // --------------------------------------------------------------------------
  69. /**
  70. * Used to toggle the selection state. By default this won't trigger an event.
  71. * The first argument allows the value to be coerced, rather than swapping values.
  72. */
  73. async toggleSelected(coerce) {
  74. if (this.disabled) {
  75. return;
  76. }
  77. this.selected = typeof coerce === "boolean" ? coerce : !this.selected;
  78. }
  79. // --------------------------------------------------------------------------
  80. //
  81. // Render Methods
  82. //
  83. // --------------------------------------------------------------------------
  84. renderIcon(isSingle) {
  85. const { icon, disabled, selected } = this;
  86. const level = `${CSS.icon}--indent`;
  87. const defaultIcon = isSingle ? "dot" : "check";
  88. const iconPath = disabled ? "circle-disallowed" : defaultIcon;
  89. const showDot = isSingle && !icon && !disabled;
  90. return showDot ? (h("span", { class: {
  91. [CSS.icon]: true,
  92. [CSS.dot]: true,
  93. [level]: true
  94. } })) : (h("calcite-icon", { class: {
  95. [CSS.icon]: !icon,
  96. [CSS.custom]: !!icon,
  97. [CSS.iconActive]: icon && selected,
  98. [level]: true
  99. }, icon: icon || iconPath, scale: "s" }));
  100. }
  101. renderChildren() {
  102. if (getSlotted(this.el)) {
  103. return (h("ul", { key: "default-slot-container" },
  104. h("slot", null)));
  105. }
  106. return null;
  107. }
  108. render() {
  109. const isSingleSelect = getElementProp(this.el, "selection-mode", "multi") === "single";
  110. const classes = {
  111. [CSS.label]: true,
  112. [CSS.selected]: this.selected,
  113. [CSS.active]: this.active,
  114. [CSS.single]: isSingleSelect
  115. };
  116. const depth = getDepth(this.el);
  117. return (h(Host, { "aria-hidden": "true" },
  118. h("div", { class: `container scale--${this.scale}`, style: { "--calcite-combobox-item-spacing-indent-multiplier": `${depth}` } },
  119. h("li", { class: classes, id: this.guid, onClick: this.itemClickHandler },
  120. this.renderIcon(isSingleSelect),
  121. h("span", { class: CSS.title }, this.textLabel)),
  122. this.renderChildren())));
  123. }
  124. static get is() { return "calcite-combobox-item"; }
  125. static get encapsulation() { return "shadow"; }
  126. static get originalStyleUrls() { return {
  127. "$": ["combobox-item.scss"]
  128. }; }
  129. static get styleUrls() { return {
  130. "$": ["combobox-item.css"]
  131. }; }
  132. static get properties() { return {
  133. "disabled": {
  134. "type": "boolean",
  135. "mutable": false,
  136. "complexType": {
  137. "original": "boolean",
  138. "resolved": "boolean",
  139. "references": {}
  140. },
  141. "required": false,
  142. "optional": false,
  143. "docs": {
  144. "tags": [],
  145. "text": "When true, the item cannot be clicked and is visually muted."
  146. },
  147. "attribute": "disabled",
  148. "reflect": true,
  149. "defaultValue": "false"
  150. },
  151. "selected": {
  152. "type": "boolean",
  153. "mutable": true,
  154. "complexType": {
  155. "original": "boolean",
  156. "resolved": "boolean",
  157. "references": {}
  158. },
  159. "required": false,
  160. "optional": false,
  161. "docs": {
  162. "tags": [],
  163. "text": "Set this to true to pre-select an item. Toggles when an item is checked/unchecked."
  164. },
  165. "attribute": "selected",
  166. "reflect": true,
  167. "defaultValue": "false"
  168. },
  169. "active": {
  170. "type": "boolean",
  171. "mutable": false,
  172. "complexType": {
  173. "original": "boolean",
  174. "resolved": "boolean",
  175. "references": {}
  176. },
  177. "required": false,
  178. "optional": false,
  179. "docs": {
  180. "tags": [],
  181. "text": "True when item is highlighted either from keyboard or mouse hover"
  182. },
  183. "attribute": "active",
  184. "reflect": false,
  185. "defaultValue": "false"
  186. },
  187. "ancestors": {
  188. "type": "unknown",
  189. "mutable": true,
  190. "complexType": {
  191. "original": "ComboboxChildElement[]",
  192. "resolved": "ComboboxChildElement[]",
  193. "references": {
  194. "ComboboxChildElement": {
  195. "location": "import",
  196. "path": "../combobox/interfaces"
  197. }
  198. }
  199. },
  200. "required": false,
  201. "optional": false,
  202. "docs": {
  203. "tags": [],
  204. "text": "Parent and grandparent combobox items, this is set internally for use from combobox"
  205. }
  206. },
  207. "guid": {
  208. "type": "string",
  209. "mutable": false,
  210. "complexType": {
  211. "original": "string",
  212. "resolved": "string",
  213. "references": {}
  214. },
  215. "required": false,
  216. "optional": false,
  217. "docs": {
  218. "tags": [],
  219. "text": "Unique identifier, used for accessibility"
  220. },
  221. "attribute": "guid",
  222. "reflect": false,
  223. "defaultValue": "guid()"
  224. },
  225. "icon": {
  226. "type": "string",
  227. "mutable": false,
  228. "complexType": {
  229. "original": "string",
  230. "resolved": "string",
  231. "references": {}
  232. },
  233. "required": false,
  234. "optional": true,
  235. "docs": {
  236. "tags": [],
  237. "text": "Custom icon to display both in combobox chips and next to combobox item text"
  238. },
  239. "attribute": "icon",
  240. "reflect": false
  241. },
  242. "textLabel": {
  243. "type": "string",
  244. "mutable": false,
  245. "complexType": {
  246. "original": "string",
  247. "resolved": "string",
  248. "references": {}
  249. },
  250. "required": true,
  251. "optional": false,
  252. "docs": {
  253. "tags": [],
  254. "text": "The main label for this item."
  255. },
  256. "attribute": "text-label",
  257. "reflect": true
  258. },
  259. "value": {
  260. "type": "any",
  261. "mutable": false,
  262. "complexType": {
  263. "original": "any",
  264. "resolved": "any",
  265. "references": {}
  266. },
  267. "required": true,
  268. "optional": false,
  269. "docs": {
  270. "tags": [],
  271. "text": "The item's associated value"
  272. },
  273. "attribute": "value",
  274. "reflect": false
  275. },
  276. "constant": {
  277. "type": "boolean",
  278. "mutable": false,
  279. "complexType": {
  280. "original": "boolean",
  281. "resolved": "boolean",
  282. "references": {}
  283. },
  284. "required": false,
  285. "optional": false,
  286. "docs": {
  287. "tags": [],
  288. "text": "Don't filter this item based on the search text"
  289. },
  290. "attribute": "constant",
  291. "reflect": true
  292. }
  293. }; }
  294. static get events() { return [{
  295. "method": "calciteComboboxItemChange",
  296. "name": "calciteComboboxItemChange",
  297. "bubbles": true,
  298. "cancelable": true,
  299. "composed": true,
  300. "docs": {
  301. "tags": [],
  302. "text": "Emitted whenever the item is selected or unselected."
  303. },
  304. "complexType": {
  305. "original": "any",
  306. "resolved": "any",
  307. "references": {}
  308. }
  309. }]; }
  310. static get methods() { return {
  311. "toggleSelected": {
  312. "complexType": {
  313. "signature": "(coerce?: boolean) => Promise<void>",
  314. "parameters": [{
  315. "tags": [],
  316. "text": ""
  317. }],
  318. "references": {
  319. "Promise": {
  320. "location": "global"
  321. }
  322. },
  323. "return": "Promise<void>"
  324. },
  325. "docs": {
  326. "text": "Used to toggle the selection state. By default this won't trigger an event.\nThe first argument allows the value to be coerced, rather than swapping values.",
  327. "tags": []
  328. }
  329. }
  330. }; }
  331. static get elementRef() { return "el"; }
  332. static get watchers() { return [{
  333. "propName": "selected",
  334. "methodName": "selectedWatchHandler"
  335. }]; }
  336. }