@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
282 lines (276 loc) • 14.3 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 { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { g as getElementProp, b as getSlotted } from './dom.js';
import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label.js';
import { c as createObserver } from './observers.js';
import { u as updateHostInteraction } from './interactive.js';
import { d as defineCustomElement$4 } from './button.js';
import { d as defineCustomElement$3 } from './icon.js';
import { d as defineCustomElement$2 } from './loader.js';
const CSS = {
wrapper: "wrapper",
confirmChangesButton: "confirm-changes-button",
cancelEditingButton: "cancel-editing-button",
inputWrapper: "input-wrapper",
cancelEditingButtonWrapper: "cancel-editing-button-wrapper",
enableEditingButton: "enable-editing-button",
controlsWrapper: "controls-wrapper"
};
const TEXT = {
intlEnablingEditing: "Click to edit",
intlCancelEditing: "Cancel",
intlConfirmChanges: "Save"
};
const inlineEditableCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:block}:host([scale=s]) .controls-wrapper{height:1.5rem}:host([scale=m]) .controls-wrapper{height:2rem}:host([scale=l]) .controls-wrapper{height:2.75rem}:host(:not([editing-enabled]):not([disabled])) .wrapper:hover{background-color:var(--calcite-ui-foreground-2)}.wrapper{-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;-ms-flex-pack:justify;justify-content:space-between;background-color:var(--calcite-ui-foreground-1);-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}.wrapper .input-wrapper{-ms-flex:1 1 0%;flex:1 1 0%}.controls-wrapper{display:-ms-flexbox;display:flex}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) .cancel-editing-button-wrapper{border-color:var(--calcite-ui-border-2)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}";
const InlineEditable = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
this.calciteInlineEditableEditCancel = createEvent(this, "calciteInlineEditableEditCancel", 7);
this.calciteInlineEditableEditConfirm = createEvent(this, "calciteInlineEditableEditConfirm", 7);
this.calciteInlineEditableEnableEditingChange = createEvent(this, "calciteInlineEditableEnableEditingChange", 7);
//--------------------------------------------------------------------------
//
// Props
//
//--------------------------------------------------------------------------
/** specify whether editing can be enabled */
this.disabled = false;
/** specify whether the wrapped input element is editable, defaults to false */
this.editingEnabled = false;
/** specify whether the confirm button should display a loading state, defaults to false */
this.loading = false;
/** specify whether save/cancel controls should be displayed when editingEnabled is true, defaults to false */
this.controls = false;
/** specify text to be user for the enable editing button's aria-label, defaults to `Click to edit`
* @default "Click to edit"
*/
this.intlEnableEditing = TEXT.intlEnablingEditing;
/** specify text to be user for the cancel editing button's aria-label, defaults to `Cancel`
* @default "Cancel"
*/
this.intlCancelEditing = TEXT.intlCancelEditing;
/** specify text to be user for the confirm changes button's aria-label, defaults to `Save`
* @default "Save"
*/
this.intlConfirmChanges = TEXT.intlConfirmChanges;
this.mutationObserver = createObserver("mutation", () => this.mutationObserverCallback());
this.transitionEnd = () => {
if (!this.editingEnabled && !!this.shouldEmitCancel) {
this.calciteInlineEditableEditCancel.emit();
}
};
this.enableEditing = () => {
var _a, _b;
this.valuePriorToEditing = (_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.value;
this.editingEnabled = true;
(_b = this.inputElement) === null || _b === void 0 ? void 0 : _b.setFocus();
this.calciteInlineEditableEnableEditingChange.emit();
};
this.disableEditing = () => {
this.editingEnabled = false;
};
this.cancelEditing = () => {
if (this.inputElement) {
this.inputElement.value = this.valuePriorToEditing;
}
this.disableEditing();
this.enableEditingButton.setFocus();
};
this.escapeKeyHandler = async (e) => {
var _a;
if (e.key !== "Escape") {
if (e.key === "Tab" && this.shouldShowControls) {
if (!e.shiftKey && e.target === this.inputElement) {
e.preventDefault();
this.cancelEditingButton.setFocus();
}
if (!!e.shiftKey && e.target === this.cancelEditingButton) {
e.preventDefault();
(_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.setFocus();
}
}
return;
}
this.cancelEditing();
};
this.cancelEditingHandler = async (e) => {
e.preventDefault();
this.cancelEditing();
};
this.enableEditingHandler = async (e) => {
if (this.disabled ||
e.target === this.cancelEditingButton ||
e.target === this.confirmEditingButton) {
return;
}
e.preventDefault();
if (!this.editingEnabled) {
this.enableEditing();
}
};
this.confirmChangesHandler = async (e) => {
e.preventDefault();
this.calciteInlineEditableEditConfirm.emit();
try {
if (this.afterConfirm) {
this.loading = true;
await this.afterConfirm();
this.disableEditing();
this.enableEditingButton.setFocus();
}
}
catch (e) {
}
finally {
this.loading = false;
}
};
}
disabledWatcher(disabled) {
if (this.inputElement) {
this.inputElement.disabled = disabled;
}
}
editingEnabledWatcher(newValue, oldValue) {
if (this.inputElement) {
this.inputElement.editingEnabled = newValue;
}
if (!newValue && !!oldValue) {
this.shouldEmitCancel = true;
}
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
var _a;
connectLabel(this);
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true });
this.mutationObserverCallback();
}
disconnectedCallback() {
var _a;
disconnectLabel(this);
(_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
}
componentDidRender() {
updateHostInteraction(this);
}
render() {
return (h("div", { class: CSS.wrapper, onClick: this.enableEditingHandler, onKeyDown: this.escapeKeyHandler, onTransitionEnd: this.transitionEnd }, h("div", { class: CSS.inputWrapper }, h("slot", null)), h("div", { class: CSS.controlsWrapper }, h("calcite-button", { appearance: "transparent", class: CSS.enableEditingButton, color: "neutral", disabled: this.disabled, iconStart: "pencil", label: this.intlEnableEditing, onClick: this.enableEditingHandler, ref: (el) => (this.enableEditingButton = el), scale: this.scale, style: {
opacity: this.editingEnabled ? "0" : "1",
width: this.editingEnabled ? "0" : "inherit"
}, type: "button" }), this.shouldShowControls && [
h("div", { class: CSS.cancelEditingButtonWrapper }, h("calcite-button", { appearance: "transparent", class: CSS.cancelEditingButton, color: "neutral", disabled: this.disabled, iconStart: "x", label: this.intlCancelEditing, onClick: this.cancelEditingHandler, ref: (el) => (this.cancelEditingButton = el), scale: this.scale, type: "button" })),
h("calcite-button", { appearance: "solid", class: CSS.confirmChangesButton, color: "blue", disabled: this.disabled, iconStart: "check", label: this.intlConfirmChanges, loading: this.loading, onClick: this.confirmChangesHandler, ref: (el) => (this.confirmEditingButton = el), scale: this.scale, type: "button" })
])));
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
blurHandler() {
if (!this.controls) {
this.disableEditing();
}
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
async setFocus() {
var _a, _b;
if (this.editingEnabled) {
(_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.setFocus();
}
else {
(_b = this.enableEditingButton) === null || _b === void 0 ? void 0 : _b.setFocus();
}
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
mutationObserverCallback() {
var _a;
this.updateSlottedInput();
this.scale =
this.scale || ((_a = this.inputElement) === null || _a === void 0 ? void 0 : _a.scale) || getElementProp(this.el, "scale", undefined);
}
onLabelClick() {
this.setFocus();
}
updateSlottedInput() {
const inputElement = getSlotted(this.el, {
matches: "calcite-input"
});
this.inputElement = inputElement;
if (!inputElement) {
return;
}
this.inputElement.disabled = this.disabled;
this.inputElement.label = this.inputElement.label || getLabelText(this);
}
get shouldShowControls() {
return this.editingEnabled && this.controls;
}
get el() { return this; }
static get watchers() { return {
"disabled": ["disabledWatcher"],
"editingEnabled": ["editingEnabledWatcher"]
}; }
static get style() { return inlineEditableCss; }
}, [1, "calcite-inline-editable", {
"disabled": [516],
"editingEnabled": [1540, "editing-enabled"],
"loading": [1540],
"controls": [516],
"intlEnableEditing": [513, "intl-enable-editing"],
"intlCancelEditing": [513, "intl-cancel-editing"],
"intlConfirmChanges": [513, "intl-confirm-changes"],
"scale": [1537],
"afterConfirm": [16],
"setFocus": [64]
}, [[0, "calciteInputBlur", "blurHandler"]]]);
function defineCustomElement$1() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-inline-editable", "calcite-button", "calcite-icon", "calcite-loader"];
components.forEach(tagName => { switch (tagName) {
case "calcite-inline-editable":
if (!customElements.get(tagName)) {
customElements.define(tagName, InlineEditable);
}
break;
case "calcite-button":
if (!customElements.get(tagName)) {
defineCustomElement$4();
}
break;
case "calcite-icon":
if (!customElements.get(tagName)) {
defineCustomElement$3();
}
break;
case "calcite-loader":
if (!customElements.get(tagName)) {
defineCustomElement$2();
}
break;
} });
}
defineCustomElement$1();
const CalciteInlineEditable = InlineEditable;
const defineCustomElement = defineCustomElement$1;
export { CalciteInlineEditable, defineCustomElement };