combobox-item.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 { Host, h } 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`, interaction is prevented and the component is displayed with lower opacity. */
  24. this.disabled = false;
  25. /**
  26. * When `true`, the component is selected.
  27. */
  28. this.selected = false;
  29. /** When `true`, the component is active. */
  30. this.active = false;
  31. /** The `id` attribute of the component. When omitted, a globally unique identifier is used. */
  32. this.guid = guid();
  33. this.scale = "m";
  34. // --------------------------------------------------------------------------
  35. //
  36. // Private Methods
  37. //
  38. // --------------------------------------------------------------------------
  39. this.itemClickHandler = (event) => {
  40. event.preventDefault();
  41. if (this.disabled) {
  42. return;
  43. }
  44. this.selected = !this.selected;
  45. };
  46. }
  47. selectedWatchHandler() {
  48. this.calciteComboboxItemChange.emit(this.el);
  49. }
  50. // --------------------------------------------------------------------------
  51. //
  52. // Lifecycle
  53. //
  54. // --------------------------------------------------------------------------
  55. connectedCallback() {
  56. this.ancestors = getAncestors(this.el);
  57. this.scale = getElementProp(this.el, "scale", this.scale);
  58. connectConditionalSlotComponent(this);
  59. }
  60. disconnectedCallback() {
  61. disconnectConditionalSlotComponent(this);
  62. }
  63. componentDidRender() {
  64. updateHostInteraction(this);
  65. }
  66. // --------------------------------------------------------------------------
  67. //
  68. // Public Methods
  69. //
  70. // --------------------------------------------------------------------------
  71. /**
  72. * Used to toggle the selection state. By default this won't trigger an event.
  73. * The first argument allows the value to be coerced, rather than swapping values.
  74. *
  75. * @param coerce
  76. */
  77. async toggleSelected(coerce) {
  78. if (this.disabled) {
  79. return;
  80. }
  81. this.selected = typeof coerce === "boolean" ? coerce : !this.selected;
  82. }
  83. // --------------------------------------------------------------------------
  84. //
  85. // Render Methods
  86. //
  87. // --------------------------------------------------------------------------
  88. renderIcon(isSingle) {
  89. const { icon, disabled, selected } = this;
  90. const level = `${CSS.icon}--indent`;
  91. const defaultIcon = isSingle ? "dot" : "check";
  92. const iconPath = disabled ? "circle-disallowed" : defaultIcon;
  93. const showDot = isSingle && !icon && !disabled;
  94. return showDot ? (h("span", { class: {
  95. [CSS.icon]: true,
  96. [CSS.dot]: true,
  97. [level]: true
  98. } })) : (h("calcite-icon", { class: {
  99. [CSS.icon]: !icon,
  100. [CSS.custom]: !!icon,
  101. [CSS.iconActive]: icon && selected,
  102. [level]: true
  103. }, icon: icon || iconPath, scale: "s" }));
  104. }
  105. renderChildren() {
  106. if (getSlotted(this.el)) {
  107. return (h("ul", { key: "default-slot-container" }, h("slot", null)));
  108. }
  109. return null;
  110. }
  111. render() {
  112. const isSingleSelect = getElementProp(this.el, "selection-mode", "multi") === "single";
  113. const classes = {
  114. [CSS.label]: true,
  115. [CSS.selected]: this.selected,
  116. [CSS.active]: this.active,
  117. [CSS.single]: isSingleSelect
  118. };
  119. const depth = getDepth(this.el);
  120. return (h(Host, { "aria-hidden": "true" }, h("div", { class: `container scale--${this.scale}`, style: { "--calcite-combobox-item-spacing-indent-multiplier": `${depth}` } }, h("li", { class: classes, id: this.guid, onClick: this.itemClickHandler }, this.renderIcon(isSingleSelect), h("span", { class: CSS.title }, this.textLabel)), this.renderChildren())));
  121. }
  122. static get is() { return "calcite-combobox-item"; }
  123. static get encapsulation() { return "shadow"; }
  124. static get originalStyleUrls() {
  125. return {
  126. "$": ["combobox-item.scss"]
  127. };
  128. }
  129. static get styleUrls() {
  130. return {
  131. "$": ["combobox-item.css"]
  132. };
  133. }
  134. static get properties() {
  135. return {
  136. "disabled": {
  137. "type": "boolean",
  138. "mutable": false,
  139. "complexType": {
  140. "original": "boolean",
  141. "resolved": "boolean",
  142. "references": {}
  143. },
  144. "required": false,
  145. "optional": false,
  146. "docs": {
  147. "tags": [],
  148. "text": "When `true`, interaction is prevented and the component is displayed with lower opacity."
  149. },
  150. "attribute": "disabled",
  151. "reflect": true,
  152. "defaultValue": "false"
  153. },
  154. "selected": {
  155. "type": "boolean",
  156. "mutable": true,
  157. "complexType": {
  158. "original": "boolean",
  159. "resolved": "boolean",
  160. "references": {}
  161. },
  162. "required": false,
  163. "optional": false,
  164. "docs": {
  165. "tags": [],
  166. "text": "When `true`, the component is selected."
  167. },
  168. "attribute": "selected",
  169. "reflect": true,
  170. "defaultValue": "false"
  171. },
  172. "active": {
  173. "type": "boolean",
  174. "mutable": false,
  175. "complexType": {
  176. "original": "boolean",
  177. "resolved": "boolean",
  178. "references": {}
  179. },
  180. "required": false,
  181. "optional": false,
  182. "docs": {
  183. "tags": [],
  184. "text": "When `true`, the component is active."
  185. },
  186. "attribute": "active",
  187. "reflect": true,
  188. "defaultValue": "false"
  189. },
  190. "ancestors": {
  191. "type": "unknown",
  192. "mutable": true,
  193. "complexType": {
  194. "original": "ComboboxChildElement[]",
  195. "resolved": "ComboboxChildElement[]",
  196. "references": {
  197. "ComboboxChildElement": {
  198. "location": "import",
  199. "path": "../combobox/interfaces"
  200. }
  201. }
  202. },
  203. "required": false,
  204. "optional": false,
  205. "docs": {
  206. "tags": [],
  207. "text": "Specifies the parent and grandparent items, which are set on `calcite-combobox`."
  208. }
  209. },
  210. "guid": {
  211. "type": "string",
  212. "mutable": false,
  213. "complexType": {
  214. "original": "string",
  215. "resolved": "string",
  216. "references": {}
  217. },
  218. "required": false,
  219. "optional": false,
  220. "docs": {
  221. "tags": [],
  222. "text": "The `id` attribute of the component. When omitted, a globally unique identifier is used."
  223. },
  224. "attribute": "guid",
  225. "reflect": true,
  226. "defaultValue": "guid()"
  227. },
  228. "icon": {
  229. "type": "string",
  230. "mutable": false,
  231. "complexType": {
  232. "original": "string",
  233. "resolved": "string",
  234. "references": {}
  235. },
  236. "required": false,
  237. "optional": true,
  238. "docs": {
  239. "tags": [],
  240. "text": "Specifies an icon to display."
  241. },
  242. "attribute": "icon",
  243. "reflect": true
  244. },
  245. "textLabel": {
  246. "type": "string",
  247. "mutable": false,
  248. "complexType": {
  249. "original": "string",
  250. "resolved": "string",
  251. "references": {}
  252. },
  253. "required": true,
  254. "optional": false,
  255. "docs": {
  256. "tags": [],
  257. "text": "The component's text."
  258. },
  259. "attribute": "text-label",
  260. "reflect": true
  261. },
  262. "value": {
  263. "type": "any",
  264. "mutable": false,
  265. "complexType": {
  266. "original": "any",
  267. "resolved": "any",
  268. "references": {}
  269. },
  270. "required": true,
  271. "optional": false,
  272. "docs": {
  273. "tags": [],
  274. "text": "The component's value."
  275. },
  276. "attribute": "value",
  277. "reflect": false
  278. },
  279. "constant": {
  280. "type": "boolean",
  281. "mutable": false,
  282. "complexType": {
  283. "original": "boolean",
  284. "resolved": "boolean",
  285. "references": {}
  286. },
  287. "required": false,
  288. "optional": false,
  289. "docs": {
  290. "tags": [{
  291. "name": "deprecated",
  292. "text": "use `filterDisabled` instead."
  293. }],
  294. "text": "When `true`, omits the component from the `calcite-combobox` filtered search results."
  295. },
  296. "attribute": "constant",
  297. "reflect": true
  298. },
  299. "filterDisabled": {
  300. "type": "boolean",
  301. "mutable": false,
  302. "complexType": {
  303. "original": "boolean",
  304. "resolved": "boolean",
  305. "references": {}
  306. },
  307. "required": false,
  308. "optional": false,
  309. "docs": {
  310. "tags": [],
  311. "text": "When `true`, omits the component from the `calcite-combobox` filtered search results."
  312. },
  313. "attribute": "filter-disabled",
  314. "reflect": true
  315. }
  316. };
  317. }
  318. static get events() {
  319. return [{
  320. "method": "calciteComboboxItemChange",
  321. "name": "calciteComboboxItemChange",
  322. "bubbles": true,
  323. "cancelable": false,
  324. "composed": true,
  325. "docs": {
  326. "tags": [],
  327. "text": "Emits whenever the component is selected or unselected.\n\n**Note:**: The event's payload is deprecated, please use the event's `target`/`currentTarget` instead"
  328. },
  329. "complexType": {
  330. "original": "DeprecatedEventPayload",
  331. "resolved": "any",
  332. "references": {
  333. "DeprecatedEventPayload": {
  334. "location": "import",
  335. "path": "../interfaces"
  336. }
  337. }
  338. }
  339. }];
  340. }
  341. static get methods() {
  342. return {
  343. "toggleSelected": {
  344. "complexType": {
  345. "signature": "(coerce?: boolean) => Promise<void>",
  346. "parameters": [{
  347. "tags": [{
  348. "name": "param",
  349. "text": "coerce"
  350. }],
  351. "text": ""
  352. }],
  353. "references": {
  354. "Promise": {
  355. "location": "global"
  356. }
  357. },
  358. "return": "Promise<void>"
  359. },
  360. "docs": {
  361. "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.",
  362. "tags": [{
  363. "name": "param",
  364. "text": "coerce"
  365. }]
  366. }
  367. }
  368. };
  369. }
  370. static get elementRef() { return "el"; }
  371. static get watchers() {
  372. return [{
  373. "propName": "selected",
  374. "methodName": "selectedWatchHandler"
  375. }];
  376. }
  377. }