@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
450 lines (449 loc) • 13.6 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
* v1.5.0-next.4
*/
import { h, Host } from "@stencil/core";
import { getElementProp, toAriaBoolean } from "../../utils/dom";
import { CSS } from "./resources";
import { componentLoaded, setComponentLoaded, setUpLoadableComponent } from "../../utils/loadable";
/**
* @slot - A slot for adding text.
*/
export class DropdownItem {
constructor() {
/** Specifies the scale of dropdown-item controlled by the parent, defaults to m */
this.scale = "m";
this.selected = false;
this.iconFlipRtl = undefined;
this.iconStart = undefined;
this.iconEnd = undefined;
this.href = undefined;
this.label = undefined;
this.rel = undefined;
this.target = undefined;
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
await componentLoaded(this);
this.el?.focus();
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
componentWillLoad() {
setUpLoadableComponent(this);
this.initialize();
}
componentDidLoad() {
setComponentLoaded(this);
}
connectedCallback() {
this.initialize();
}
render() {
const scale = getElementProp(this.el, "scale", this.scale);
const iconStartEl = (h("calcite-icon", { class: CSS.iconStart, flipRtl: this.iconFlipRtl === "start" || this.iconFlipRtl === "both", icon: this.iconStart, scale: scale === "l" ? "m" : "s" }));
const contentNode = (h("span", { class: CSS.itemContent }, h("slot", null)));
const iconEndEl = (h("calcite-icon", { class: CSS.iconEnd, flipRtl: this.iconFlipRtl === "end" || this.iconFlipRtl === "both", icon: this.iconEnd, scale: scale === "l" ? "m" : "s" }));
const slottedContent = this.iconStart && this.iconEnd
? [iconStartEl, contentNode, iconEndEl]
: this.iconStart
? [iconStartEl, contentNode]
: this.iconEnd
? [contentNode, iconEndEl]
: contentNode;
const contentEl = !this.href ? (slottedContent) : (h("a", { "aria-label": this.label, class: CSS.link, href: this.href, rel: this.rel, tabIndex: -1, target: this.target,
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => (this.childLink = el) }, slottedContent));
const itemRole = this.href
? null
: this.selectionMode === "single"
? "menuitemradio"
: this.selectionMode === "multiple"
? "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 === "multiple",
[CSS.containerSingle]: this.selectionMode === "single",
[CSS.containerNone]: this.selectionMode === "none"
} }, this.selectionMode !== "none" ? (h("calcite-icon", { class: CSS.icon, icon: this.selectionMode === "multiple" ? "check" : "bullet-point", scale: scale === "l" ? "m" : "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 "multiple":
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.calciteDropdownItemSelect.emit();
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 {
"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"
},
"iconFlipRtl": {
"type": "string",
"mutable": false,
"complexType": {
"original": "FlipContext",
"resolved": "\"both\" | \"end\" | \"start\"",
"references": {
"FlipContext": {
"location": "import",
"path": "../interfaces"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Displays the `iconStart` and/or `iconEnd` as 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": false,
"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": false,
"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": false,
"docs": {
"tags": [],
"text": "Specifies the URL of the linked resource, which can be set as an absolute or relative path.\n\nDetermines if the component will render as an anchor."
},
"attribute": "href",
"reflect": true
},
"label": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Accessible name for the component."
},
"attribute": "label",
"reflect": false
},
"rel": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies the relationship to the linked document defined in `href`."
},
"attribute": "rel",
"reflect": true
},
"target": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Specifies the frame or window to open the linked document."
},
"attribute": "target",
"reflect": true
}
};
}
static get events() {
return [{
"method": "calciteDropdownItemSelect",
"name": "calciteDropdownItemSelect",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [],
"text": "Fires when the component is selected."
},
"complexType": {
"original": "void",
"resolved": "void",
"references": {}
}
}, {
"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 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
}];
}
}