123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- /*!
- * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
- * v1.0.0-beta.97
- */
- import { Host, h } from "@stencil/core";
- import { getElementProp, getSlotted } from "../../utils/dom";
- import { CSS } from "./resources";
- import { guid } from "../../utils/guid";
- import { getAncestors, getDepth } from "../combobox/utils";
- import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot";
- import { updateHostInteraction } from "../../utils/interactive";
- /**
- * @slot - A slot for adding nested `calcite-combobox-item`s.
- */
- export class ComboboxItem {
- constructor() {
- // --------------------------------------------------------------------------
- //
- // Properties
- //
- // --------------------------------------------------------------------------
- /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
- this.disabled = false;
- /**
- * When `true`, the component is selected.
- */
- this.selected = false;
- /** When `true`, the component is active. */
- this.active = false;
- /** The `id` attribute of the component. When omitted, a globally unique identifier is used. */
- this.guid = guid();
- this.scale = "m";
- // --------------------------------------------------------------------------
- //
- // Private Methods
- //
- // --------------------------------------------------------------------------
- this.itemClickHandler = (event) => {
- event.preventDefault();
- if (this.disabled) {
- return;
- }
- this.selected = !this.selected;
- };
- }
- selectedWatchHandler() {
- this.calciteComboboxItemChange.emit(this.el);
- }
- // --------------------------------------------------------------------------
- //
- // Lifecycle
- //
- // --------------------------------------------------------------------------
- connectedCallback() {
- this.ancestors = getAncestors(this.el);
- this.scale = getElementProp(this.el, "scale", this.scale);
- connectConditionalSlotComponent(this);
- }
- disconnectedCallback() {
- disconnectConditionalSlotComponent(this);
- }
- componentDidRender() {
- updateHostInteraction(this);
- }
- // --------------------------------------------------------------------------
- //
- // Public Methods
- //
- // --------------------------------------------------------------------------
- /**
- * Used to toggle the selection state. By default this won't trigger an event.
- * The first argument allows the value to be coerced, rather than swapping values.
- *
- * @param coerce
- */
- async toggleSelected(coerce) {
- if (this.disabled) {
- return;
- }
- this.selected = typeof coerce === "boolean" ? coerce : !this.selected;
- }
- // --------------------------------------------------------------------------
- //
- // Render Methods
- //
- // --------------------------------------------------------------------------
- renderIcon(isSingle) {
- const { icon, disabled, selected } = this;
- const level = `${CSS.icon}--indent`;
- const defaultIcon = isSingle ? "dot" : "check";
- const iconPath = disabled ? "circle-disallowed" : defaultIcon;
- const showDot = isSingle && !icon && !disabled;
- return showDot ? (h("span", { class: {
- [CSS.icon]: true,
- [CSS.dot]: true,
- [level]: true
- } })) : (h("calcite-icon", { class: {
- [CSS.icon]: !icon,
- [CSS.custom]: !!icon,
- [CSS.iconActive]: icon && selected,
- [level]: true
- }, icon: icon || iconPath, scale: "s" }));
- }
- renderChildren() {
- if (getSlotted(this.el)) {
- return (h("ul", { key: "default-slot-container" }, h("slot", null)));
- }
- return null;
- }
- render() {
- const isSingleSelect = getElementProp(this.el, "selection-mode", "multi") === "single";
- const classes = {
- [CSS.label]: true,
- [CSS.selected]: this.selected,
- [CSS.active]: this.active,
- [CSS.single]: isSingleSelect
- };
- const depth = getDepth(this.el);
- 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())));
- }
- static get is() { return "calcite-combobox-item"; }
- static get encapsulation() { return "shadow"; }
- static get originalStyleUrls() {
- return {
- "$": ["combobox-item.scss"]
- };
- }
- static get styleUrls() {
- return {
- "$": ["combobox-item.css"]
- };
- }
- static get properties() {
- return {
- "disabled": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, interaction is prevented and the component is displayed with lower opacity."
- },
- "attribute": "disabled",
- "reflect": true,
- "defaultValue": "false"
- },
- "selected": {
- "type": "boolean",
- "mutable": true,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, the component is selected."
- },
- "attribute": "selected",
- "reflect": true,
- "defaultValue": "false"
- },
- "active": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, the component is active."
- },
- "attribute": "active",
- "reflect": true,
- "defaultValue": "false"
- },
- "ancestors": {
- "type": "unknown",
- "mutable": true,
- "complexType": {
- "original": "ComboboxChildElement[]",
- "resolved": "ComboboxChildElement[]",
- "references": {
- "ComboboxChildElement": {
- "location": "import",
- "path": "../combobox/interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Specifies the parent and grandparent items, which are set on `calcite-combobox`."
- }
- },
- "guid": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The `id` attribute of the component. When omitted, a globally unique identifier is used."
- },
- "attribute": "guid",
- "reflect": true,
- "defaultValue": "guid()"
- },
- "icon": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Specifies an icon to display."
- },
- "attribute": "icon",
- "reflect": true
- },
- "textLabel": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": true,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The component's text."
- },
- "attribute": "text-label",
- "reflect": true
- },
- "value": {
- "type": "any",
- "mutable": false,
- "complexType": {
- "original": "any",
- "resolved": "any",
- "references": {}
- },
- "required": true,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "The component's value."
- },
- "attribute": "value",
- "reflect": false
- },
- "constant": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "deprecated",
- "text": "use `filterDisabled` instead."
- }],
- "text": "When `true`, omits the component from the `calcite-combobox` filtered search results."
- },
- "attribute": "constant",
- "reflect": true
- },
- "filterDisabled": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, omits the component from the `calcite-combobox` filtered search results."
- },
- "attribute": "filter-disabled",
- "reflect": true
- }
- };
- }
- static get events() {
- return [{
- "method": "calciteComboboxItemChange",
- "name": "calciteComboboxItemChange",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [],
- "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"
- },
- "complexType": {
- "original": "DeprecatedEventPayload",
- "resolved": "any",
- "references": {
- "DeprecatedEventPayload": {
- "location": "import",
- "path": "../interfaces"
- }
- }
- }
- }];
- }
- static get methods() {
- return {
- "toggleSelected": {
- "complexType": {
- "signature": "(coerce?: boolean) => Promise<void>",
- "parameters": [{
- "tags": [{
- "name": "param",
- "text": "coerce"
- }],
- "text": ""
- }],
- "references": {
- "Promise": {
- "location": "global"
- }
- },
- "return": "Promise<void>"
- },
- "docs": {
- "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.",
- "tags": [{
- "name": "param",
- "text": "coerce"
- }]
- }
- }
- };
- }
- static get elementRef() { return "el"; }
- static get watchers() {
- return [{
- "propName": "selected",
- "methodName": "selectedWatchHandler"
- }];
- }
- }
|