123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- /*!
- * 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 { h, Host } from "@stencil/core";
- import { getElementProp, toAriaBoolean } from "../../utils/dom";
- import { CSS } from "./resources";
- /**
- * @slot - A slot for adding text.
- */
- export class DropdownItem {
- constructor() {
- //--------------------------------------------------------------------------
- //
- // Public Properties
- //
- //--------------------------------------------------------------------------
- /**
- * Indicates whether the item is active.
- *
- * @deprecated Use selected instead.
- */
- this.active = false;
- /** When true, item is selected */
- this.selected = false;
- }
- activeHandler(value) {
- this.selected = value;
- }
- selectedHandler(value) {
- this.active = value;
- }
- //--------------------------------------------------------------------------
- //
- // Public Methods
- //
- //--------------------------------------------------------------------------
- /** Sets focus on the component. */
- async setFocus() {
- var _a;
- (_a = this.el) === null || _a === void 0 ? void 0 : _a.focus();
- }
- //--------------------------------------------------------------------------
- //
- // Lifecycle
- //
- //--------------------------------------------------------------------------
- componentWillLoad() {
- this.initialize();
- }
- connectedCallback() {
- const isSelected = this.selected || this.active;
- if (isSelected) {
- this.activeHandler(isSelected);
- this.selectedHandler(isSelected);
- }
- this.initialize();
- }
- render() {
- const scale = getElementProp(this.el, "scale", "m");
- const iconStartEl = (h("calcite-icon", { class: "dropdown-item-icon-start", flipRtl: this.iconFlipRtl === "start" || this.iconFlipRtl === "both", icon: this.iconStart, scale: "s" }));
- const contentNode = (h("span", { class: "dropdown-item-content" }, h("slot", null)));
- const iconEndEl = (h("calcite-icon", { class: "dropdown-item-icon-end", flipRtl: this.iconFlipRtl === "end" || this.iconFlipRtl === "both", icon: this.iconEnd, scale: "s" }));
- const slottedContent = this.iconStart && this.iconEnd
- ? [iconStartEl, contentNode, iconEndEl]
- : this.iconStart
- ? [iconStartEl, h("slot", null)]
- : this.iconEnd
- ? [contentNode, iconEndEl]
- : contentNode;
- const contentEl = !this.href ? (slottedContent) : (h("a", { "aria-label": this.label, class: "dropdown-link", href: this.href, ref: (el) => (this.childLink = el), rel: this.rel, target: this.target }, slottedContent));
- const itemRole = this.href
- ? null
- : this.selectionMode === "single"
- ? "menuitemradio"
- : this.selectionMode === "multi"
- ? "menuitemcheckbox"
- : "menuitem";
- const itemAria = this.selectionMode !== "none" ? toAriaBoolean(this.selected) : null;
- return (h(Host, { "aria-checked": itemAria, role: itemRole, tabindex: "0" }, h("div", { class: {
- container: true,
- [CSS.containerLink]: !!this.href,
- [CSS.containerSmall]: scale === "s",
- [CSS.containerMedium]: scale === "m",
- [CSS.containerLarge]: scale === "l",
- [CSS.containerMulti]: this.selectionMode === "multi",
- [CSS.containerSingle]: this.selectionMode === "single",
- [CSS.containerNone]: this.selectionMode === "none"
- } }, this.selectionMode !== "none" ? (h("calcite-icon", { class: "dropdown-item-icon", icon: this.selectionMode === "multi" ? "check" : "bullet-point", scale: "s" })) : null, contentEl)));
- }
- //--------------------------------------------------------------------------
- //
- // Event Listeners
- //
- //--------------------------------------------------------------------------
- onClick() {
- this.emitRequestedItem();
- }
- keyDownHandler(event) {
- switch (event.key) {
- case " ":
- case "Enter":
- this.emitRequestedItem();
- if (this.href) {
- this.childLink.click();
- }
- event.preventDefault();
- break;
- case "Escape":
- this.calciteInternalDropdownCloseRequest.emit();
- event.preventDefault();
- break;
- case "Tab":
- this.calciteInternalDropdownItemKeyEvent.emit({ keyboardEvent: event });
- break;
- case "ArrowUp":
- case "ArrowDown":
- case "Home":
- case "End":
- event.preventDefault();
- this.calciteInternalDropdownItemKeyEvent.emit({ keyboardEvent: event });
- break;
- }
- }
- updateActiveItemOnChange(event) {
- const parentEmittedChange = event.composedPath().includes(this.parentDropdownGroupEl);
- if (parentEmittedChange) {
- this.requestedDropdownGroup = event.detail.requestedDropdownGroup;
- this.requestedDropdownItem = event.detail.requestedDropdownItem;
- this.determineActiveItem();
- }
- event.stopPropagation();
- }
- //--------------------------------------------------------------------------
- //
- // Private Methods
- //
- //--------------------------------------------------------------------------
- initialize() {
- this.selectionMode = getElementProp(this.el, "selection-mode", "single");
- this.parentDropdownGroupEl = this.el.closest("calcite-dropdown-group");
- if (this.selectionMode === "none") {
- this.selected = false;
- }
- }
- determineActiveItem() {
- switch (this.selectionMode) {
- case "multi":
- if (this.el === this.requestedDropdownItem) {
- this.selected = !this.selected;
- }
- break;
- case "single":
- if (this.el === this.requestedDropdownItem) {
- this.selected = true;
- }
- else if (this.requestedDropdownGroup === this.parentDropdownGroupEl) {
- this.selected = false;
- }
- break;
- case "none":
- this.selected = false;
- break;
- }
- }
- emitRequestedItem() {
- this.calciteInternalDropdownItemSelect.emit({
- requestedDropdownItem: this.el,
- requestedDropdownGroup: this.parentDropdownGroupEl
- });
- }
- static get is() { return "calcite-dropdown-item"; }
- static get encapsulation() { return "shadow"; }
- static get originalStyleUrls() {
- return {
- "$": ["dropdown-item.scss"]
- };
- }
- static get styleUrls() {
- return {
- "$": ["dropdown-item.css"]
- };
- }
- static get properties() {
- return {
- "active": {
- "type": "boolean",
- "mutable": true,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "deprecated",
- "text": "Use selected instead."
- }],
- "text": "Indicates whether the item is active."
- },
- "attribute": "active",
- "reflect": true,
- "defaultValue": "false"
- },
- "selected": {
- "type": "boolean",
- "mutable": true,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When true, item is selected"
- },
- "attribute": "selected",
- "reflect": true,
- "defaultValue": "false"
- },
- "iconFlipRtl": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "FlipContext",
- "resolved": "\"both\" | \"end\" | \"start\"",
- "references": {
- "FlipContext": {
- "location": "import",
- "path": "../interfaces"
- }
- }
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "When true, the icon will be flipped when the element direction is right-to-left (`\"rtl\"`)."
- },
- "attribute": "icon-flip-rtl",
- "reflect": true
- },
- "iconStart": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Specifies an icon to display at the start of the component."
- },
- "attribute": "icon-start",
- "reflect": true
- },
- "iconEnd": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Specifies an icon to display at the end of the component."
- },
- "attribute": "icon-end",
- "reflect": true
- },
- "href": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "optionally pass a href - used to determine if the component should render as anchor"
- },
- "attribute": "href",
- "reflect": true
- },
- "label": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Applies to the aria-label attribute on the button or hyperlink"
- },
- "attribute": "label",
- "reflect": false
- },
- "rel": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "The rel attribute to apply to the hyperlink"
- },
- "attribute": "rel",
- "reflect": true
- },
- "target": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "The target attribute to apply to the hyperlink"
- },
- "attribute": "target",
- "reflect": true
- }
- };
- }
- static get events() {
- return [{
- "method": "calciteInternalDropdownItemSelect",
- "name": "calciteInternalDropdownItemSelect",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": ""
- },
- "complexType": {
- "original": "RequestedItem",
- "resolved": "RequestedItem",
- "references": {
- "RequestedItem": {
- "location": "import",
- "path": "../dropdown-group/interfaces"
- }
- }
- }
- }, {
- "method": "calciteInternalDropdownItemKeyEvent",
- "name": "calciteInternalDropdownItemKeyEvent",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": ""
- },
- "complexType": {
- "original": "ItemKeyboardEvent",
- "resolved": "ItemKeyboardEvent",
- "references": {
- "ItemKeyboardEvent": {
- "location": "import",
- "path": "../dropdown/interfaces"
- }
- }
- }
- }, {
- "method": "calciteInternalDropdownCloseRequest",
- "name": "calciteInternalDropdownCloseRequest",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [{
- "name": "internal",
- "text": undefined
- }],
- "text": ""
- },
- "complexType": {
- "original": "void",
- "resolved": "void",
- "references": {}
- }
- }];
- }
- static get methods() {
- return {
- "setFocus": {
- "complexType": {
- "signature": "() => Promise<void>",
- "parameters": [],
- "references": {
- "Promise": {
- "location": "global"
- }
- },
- "return": "Promise<void>"
- },
- "docs": {
- "text": "Sets focus on the component.",
- "tags": []
- }
- }
- };
- }
- static get elementRef() { return "el"; }
- static get watchers() {
- return [{
- "propName": "active",
- "methodName": "activeHandler"
- }, {
- "propName": "selected",
- "methodName": "selectedHandler"
- }];
- }
- static get listeners() {
- return [{
- "name": "click",
- "method": "onClick",
- "target": undefined,
- "capture": false,
- "passive": false
- }, {
- "name": "keydown",
- "method": "keyDownHandler",
- "target": undefined,
- "capture": false,
- "passive": false
- }, {
- "name": "calciteInternalDropdownItemChange",
- "method": "updateActiveItemOnChange",
- "target": "body",
- "capture": false,
- "passive": false
- }];
- }
- }
|