@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
404 lines (403 loc) • 11.7 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.
*/
import { Component, Element, Event, h, Host, Listen, Method, Prop } from "@stencil/core";
import { getElementProp } 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. */
this.active = false;
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
this.el.focus();
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
this.selectionMode = getElementProp(this.el, "selection-mode", "single");
this.parentDropdownGroupEl = this.el.closest("calcite-dropdown-group");
if (this.selectionMode === "none") {
this.active = false;
}
}
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" ? this.active.toString() : 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(e) {
switch (e.key) {
case " ":
this.emitRequestedItem();
if (this.href) {
e.preventDefault();
this.childLink.click();
}
break;
case "Enter":
this.emitRequestedItem();
if (this.href) {
this.childLink.click();
}
break;
case "Escape":
this.calciteDropdownCloseRequest.emit();
break;
case "Tab":
case "ArrowUp":
case "ArrowDown":
case "Home":
case "End":
this.calciteDropdownItemKeyEvent.emit({ keyboardEvent: e });
break;
}
e.preventDefault();
}
updateActiveItemOnChange(event) {
const parentEmittedChange = event.composedPath().includes(this.parentDropdownGroupEl);
if (parentEmittedChange) {
this.requestedDropdownGroup = event.detail.requestedDropdownGroup;
this.requestedDropdownItem = event.detail.requestedDropdownItem;
this.determineActiveItem();
}
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
determineActiveItem() {
switch (this.selectionMode) {
case "multi":
if (this.el === this.requestedDropdownItem) {
this.active = !this.active;
}
break;
case "single":
if (this.el === this.requestedDropdownItem) {
this.active = true;
}
else if (this.requestedDropdownGroup === this.parentDropdownGroupEl) {
this.active = false;
}
break;
case "none":
this.active = false;
break;
}
}
emitRequestedItem() {
this.calciteDropdownItemSelect.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": [],
"text": "Indicates whether the item is active."
},
"attribute": "active",
"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": "flip the icon(s) in 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": "optionally pass an icon to display at the start of an item - accepts calcite ui icon names"
},
"attribute": "icon-start",
"reflect": true
},
"iconEnd": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": "optionally pass an icon to display at the end of an item - accepts calcite ui icon names"
},
"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": false
},
"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": false
}
}; }
static get events() { return [{
"method": "calciteDropdownItemSelect",
"name": "calciteDropdownItemSelect",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}, {
"method": "calciteDropdownItemKeyEvent",
"name": "calciteDropdownItemKeyEvent",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"complexType": {
"original": "ItemKeyboardEvent",
"resolved": "ItemKeyboardEvent",
"references": {
"ItemKeyboardEvent": {
"location": "import",
"path": "../dropdown/interfaces"
}
}
}
}, {
"method": "calciteDropdownCloseRequest",
"name": "calciteDropdownCloseRequest",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"complexType": {
"original": "any",
"resolved": "any",
"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": "calciteDropdownItemChange",
"method": "updateActiveItemOnChange",
"target": "body",
"capture": false,
"passive": false
}]; }
}