@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
593 lines (592 loc) • 17.4 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 { connectInteractive, disconnectInteractive, updateHostInteraction } from "../../utils/interactive";
import { numberStringFormatter, disconnectLocalized, connectLocalized } from "../../utils/locale";
import { setUpLoadableComponent, setComponentLoaded, componentLoaded } from "../../utils/loadable";
/**
* @slot - A slot for adding custom content.
*/
export class StepperItem {
constructor() {
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
this.keyDownHandler = (event) => {
if (!this.disabled && event.target === this.el) {
switch (event.key) {
case " ":
case "Enter":
this.emitUserRequestedItem();
event.preventDefault();
break;
case "ArrowUp":
case "ArrowDown":
case "ArrowLeft":
case "ArrowRight":
case "Home":
case "End":
this.calciteInternalStepperItemKeyEvent.emit({ item: event });
event.preventDefault();
break;
}
}
};
this.handleItemClick = (event) => {
if (this.disabled ||
(this.layout === "horizontal" &&
event
.composedPath()
.some((el) => el.classList?.contains("stepper-item-content")))) {
return;
}
this.emitUserRequestedItem();
};
this.emitUserRequestedItem = () => {
this.emitRequestedItem();
if (!this.disabled) {
const position = this.itemPosition;
this.calciteInternalUserRequestedStepperItemSelect.emit({
position
});
}
};
this.emitRequestedItem = () => {
if (!this.disabled) {
const position = this.itemPosition;
this.calciteInternalStepperItemSelect.emit({
position
});
}
};
this.selected = false;
this.complete = false;
this.error = false;
this.disabled = false;
this.heading = undefined;
this.description = undefined;
this.layout = "horizontal";
this.icon = false;
this.iconFlipRtl = false;
this.numbered = false;
this.scale = "m";
this.numberingSystem = undefined;
this.effectiveLocale = "";
}
selectedHandler() {
if (this.selected) {
this.emitRequestedItem();
}
}
// watch for removal of disabled to register step
disabledWatcher() {
this.registerStepperItem();
}
effectiveLocaleWatcher(locale) {
numberStringFormatter.numberFormatOptions = {
locale,
numberingSystem: this.numberingSystem,
useGrouping: false
};
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
connectInteractive(this);
connectLocalized(this);
}
componentWillLoad() {
setUpLoadableComponent(this);
this.icon = getElementProp(this.el, "icon", false);
this.numbered = getElementProp(this.el, "numbered", false);
this.layout = getElementProp(this.el, "layout", false);
this.scale = getElementProp(this.el, "scale", "m");
this.parentStepperEl = this.el.parentElement;
this.itemPosition = this.getItemPosition();
this.registerStepperItem();
if (this.selected) {
this.emitRequestedItem();
}
}
componentDidLoad() {
setComponentLoaded(this);
}
componentDidRender() {
updateHostInteraction(this, true);
}
disconnectedCallback() {
disconnectInteractive(this);
disconnectLocalized(this);
}
render() {
return (h(Host, { "aria-expanded": toAriaBoolean(this.selected), onClick: this.handleItemClick, onKeyDown: this.keyDownHandler }, h("div", { class: "container" }, h("div", { class: "stepper-item-header", tabIndex:
/* additional tab index logic needed because of display: contents */
this.layout === "horizontal" && !this.disabled ? 0 : null,
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => (this.headerEl = el) }, this.icon ? this.renderIcon() : null, this.numbered ? h("div", { class: "stepper-item-number" }, this.renderNumbers(), ".") : null, h("div", { class: "stepper-item-header-text" }, h("span", { class: "stepper-item-heading" }, this.heading), h("span", { class: "stepper-item-description" }, this.description))), h("div", { class: "stepper-item-content" }, h("slot", null)))));
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
updateActiveItemOnChange(event) {
if (event.target === this.parentStepperEl ||
event.composedPath().includes(this.parentStepperEl)) {
this.selectedPosition = event.detail.position;
this.determineSelectedItem();
}
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
await componentLoaded(this);
(this.layout === "vertical" ? this.el : this.headerEl)?.focus();
}
renderIcon() {
const path = this.selected
? "circleF"
: this.error
? "exclamationMarkCircleF"
: this.complete
? "checkCircleF"
: "circle";
return (h("calcite-icon", { class: "stepper-item-icon", flipRtl: this.iconFlipRtl, icon: path, scale: "s" }));
}
determineSelectedItem() {
this.selected = !this.disabled && this.itemPosition === this.selectedPosition;
}
registerStepperItem() {
this.calciteInternalStepperItemRegister.emit({
position: this.itemPosition
});
}
getItemPosition() {
return Array.from(this.parentStepperEl?.querySelectorAll("calcite-stepper-item")).indexOf(this.el);
}
renderNumbers() {
numberStringFormatter.numberFormatOptions = {
locale: this.effectiveLocale,
numberingSystem: this.numberingSystem,
useGrouping: false
};
return numberStringFormatter.numberFormatter.format(this.itemPosition + 1);
}
static get is() { return "calcite-stepper-item"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["stepper-item.scss"]
};
}
static get styleUrls() {
return {
"$": ["stepper-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"
},
"complete": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, the step has been completed."
},
"attribute": "complete",
"reflect": true,
"defaultValue": "false"
},
"error": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "When `true`, the component contains an error that requires resolution from the user."
},
"attribute": "error",
"reflect": true,
"defaultValue": "false"
},
"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"
},
"heading": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "The component header text."
},
"attribute": "heading",
"reflect": false
},
"description": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "A description for the component. Displays below the header text."
},
"attribute": "description",
"reflect": false
},
"layout": {
"type": "string",
"mutable": true,
"complexType": {
"original": "Extract<\"horizontal\" | \"vertical\", Layout>",
"resolved": "\"horizontal\" | \"vertical\"",
"references": {
"Extract": {
"location": "global"
},
"Layout": {
"location": "import",
"path": "../interfaces"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"attribute": "layout",
"reflect": true,
"defaultValue": "\"horizontal\""
},
"icon": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"attribute": "icon",
"reflect": false,
"defaultValue": "false"
},
"iconFlipRtl": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"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,
"defaultValue": "false"
},
"numbered": {
"type": "boolean",
"mutable": true,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"attribute": "numbered",
"reflect": false,
"defaultValue": "false"
},
"scale": {
"type": "string",
"mutable": true,
"complexType": {
"original": "Scale",
"resolved": "\"l\" | \"m\" | \"s\"",
"references": {
"Scale": {
"location": "import",
"path": "../interfaces"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"attribute": "scale",
"reflect": true,
"defaultValue": "\"m\""
},
"numberingSystem": {
"type": "string",
"mutable": false,
"complexType": {
"original": "NumberingSystem",
"resolved": "\"arab\" | \"arabext\" | \"bali\" | \"beng\" | \"deva\" | \"fullwide\" | \"gujr\" | \"guru\" | \"hanidec\" | \"khmr\" | \"knda\" | \"laoo\" | \"latn\" | \"limb\" | \"mlym\" | \"mong\" | \"mymr\" | \"orya\" | \"tamldec\" | \"telu\" | \"thai\" | \"tibt\"",
"references": {
"NumberingSystem": {
"location": "import",
"path": "../../utils/locale"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"attribute": "numbering-system",
"reflect": false
}
};
}
static get states() {
return {
"effectiveLocale": {}
};
}
static get events() {
return [{
"method": "calciteInternalStepperItemKeyEvent",
"name": "calciteInternalStepperItemKeyEvent",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"complexType": {
"original": "StepperItemKeyEventDetail",
"resolved": "StepperItemKeyEventDetail",
"references": {
"StepperItemKeyEventDetail": {
"location": "import",
"path": "../stepper/interfaces"
}
}
}
}, {
"method": "calciteInternalStepperItemSelect",
"name": "calciteInternalStepperItemSelect",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"complexType": {
"original": "StepperItemEventDetail",
"resolved": "StepperItemEventDetail",
"references": {
"StepperItemEventDetail": {
"location": "import",
"path": "../stepper/interfaces"
}
}
}
}, {
"method": "calciteInternalUserRequestedStepperItemSelect",
"name": "calciteInternalUserRequestedStepperItemSelect",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"complexType": {
"original": "StepperItemChangeEventDetail",
"resolved": "StepperItemChangeEventDetail",
"references": {
"StepperItemChangeEventDetail": {
"location": "import",
"path": "../stepper/interfaces"
}
}
}
}, {
"method": "calciteInternalStepperItemRegister",
"name": "calciteInternalStepperItemRegister",
"bubbles": true,
"cancelable": false,
"composed": true,
"docs": {
"tags": [{
"name": "internal",
"text": undefined
}],
"text": ""
},
"complexType": {
"original": "StepperItemEventDetail",
"resolved": "StepperItemEventDetail",
"references": {
"StepperItemEventDetail": {
"location": "import",
"path": "../stepper/interfaces"
}
}
}
}];
}
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": "selected",
"methodName": "selectedHandler"
}, {
"propName": "disabled",
"methodName": "disabledWatcher"
}, {
"propName": "effectiveLocale",
"methodName": "effectiveLocaleWatcher"
}];
}
static get listeners() {
return [{
"name": "calciteInternalStepperItemChange",
"method": "updateActiveItemOnChange",
"target": "body",
"capture": false,
"passive": false
}];
}
}