@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
242 lines (237 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.
* v1.5.0-next.4
*/
import { r as registerInstance, c as createEvent, h, a as getElement } from './index-c8875db5.js';
import { g as getSlotted } from './dom-4f5d1224.js';
import { c as connectInteractive, d as disconnectInteractive, u as updateHostInteraction } from './interactive-cff0a7ea.js';
import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label-0948e0eb.js';
import { s as setUpLoadableComponent, a as setComponentLoaded, c as componentLoaded } from './loadable-8d606217.js';
import { c as connectLocalized, d as disconnectLocalized } from './locale-0c018cdb.js';
import { c as createObserver } from './observers-d19b0d93.js';
import { c as connectMessages, s as setUpMessages, d as disconnectMessages, u as updateMessages } from './t9n-f819e5a2.js';
import './guid-bdb80778.js';
import './resources-35f23920.js';
import './browser-4be2add2.js';
import './key-582cee8f.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 inlineEditableCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-right{0%{opacity:0;transform:translate3D(-5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-left{0%{opacity:0;transform:translate3D(5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity: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;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-right{animation-name:in-right}.calcite-animate__in-left{animation-name:in-left}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing);--calcite-floating-ui-z-index:var(--calcite-app-z-index-dropdown)}:host([hidden]){display:none}:host([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) *,:host([disabled]) ::slotted(*){pointer-events:none}:host{display:block}:host([scale=s]) .controls-wrapper{block-size:1.5rem}:host([scale=m]) .controls-wrapper{block-size:2rem}:host([scale=l]) .controls-wrapper{block-size:2.75rem}:host(:not([editing-enabled]):not([disabled])) .wrapper:hover{background-color:var(--calcite-ui-foreground-2)}.wrapper{box-sizing:border-box;display:flex;justify-content:space-between;background-color:var(--calcite-ui-foreground-1);transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}.wrapper .input-wrapper{flex:1 1 0%}.controls-wrapper{display:flex}: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 = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.calciteInlineEditableEditCancel = createEvent(this, "calciteInlineEditableEditCancel", 6);
this.calciteInlineEditableEditConfirm = createEvent(this, "calciteInlineEditableEditConfirm", 6);
this.calciteInternalInlineEditableEnableEditingChange = createEvent(this, "calciteInternalInlineEditableEnableEditingChange", 6);
this.mutationObserver = createObserver("mutation", () => this.mutationObserverCallback());
this.enableEditing = () => {
this.valuePriorToEditing = this.inputElement?.value;
this.editingEnabled = true;
this.inputElement?.setFocus();
this.calciteInternalInlineEditableEnableEditingChange.emit();
};
this.disableEditing = () => {
this.editingEnabled = false;
};
this.cancelEditing = () => {
if (this.inputElement) {
this.inputElement.value = this.valuePriorToEditing;
}
this.disableEditing();
this.enableEditingButton.setFocus();
if (!this.editingEnabled && !!this.shouldEmitCancel) {
this.calciteInlineEditableEditCancel.emit();
}
};
this.escapeKeyHandler = async (event) => {
if (event.defaultPrevented) {
return;
}
if (event.key === "Escape") {
event.preventDefault();
this.cancelEditing();
}
if (event.key === "Tab" && this.shouldShowControls) {
if (!event.shiftKey && event.target === this.inputElement) {
event.preventDefault();
this.cancelEditingButton.setFocus();
}
if (!!event.shiftKey && event.target === this.cancelEditingButton) {
event.preventDefault();
this.inputElement?.setFocus();
}
}
};
this.cancelEditingHandler = async (event) => {
event.preventDefault();
this.cancelEditing();
};
this.enableEditingHandler = async (event) => {
if (this.disabled ||
event.target === this.cancelEditingButton ||
event.target === this.confirmEditingButton) {
return;
}
event.preventDefault();
if (!this.editingEnabled) {
this.enableEditing();
}
};
this.confirmChangesHandler = async (event) => {
event.preventDefault();
this.calciteInlineEditableEditConfirm.emit();
try {
if (this.afterConfirm) {
this.loading = true;
await this.afterConfirm();
this.disableEditing();
this.enableEditingButton.setFocus();
}
}
catch (error) {
}
finally {
this.loading = false;
}
};
this.disabled = false;
this.editingEnabled = false;
this.loading = false;
this.controls = false;
this.scale = undefined;
this.afterConfirm = undefined;
this.messages = undefined;
this.messageOverrides = undefined;
this.defaultMessages = undefined;
this.effectiveLocale = undefined;
}
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;
}
}
onMessagesChange() {
/* wired up by t9n util */
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
connectInteractive(this);
connectLabel(this);
connectLocalized(this);
connectMessages(this);
this.mutationObserver?.observe(this.el, { childList: true });
this.mutationObserverCallback();
}
async componentWillLoad() {
setUpLoadableComponent(this);
await setUpMessages(this);
}
componentDidLoad() {
setComponentLoaded(this);
}
disconnectedCallback() {
disconnectInteractive(this);
disconnectLabel(this);
disconnectLocalized(this);
disconnectMessages(this);
this.mutationObserver?.disconnect();
}
componentDidRender() {
updateHostInteraction(this);
}
render() {
return (h("div", { class: CSS.wrapper, onClick: this.enableEditingHandler, onKeyDown: this.escapeKeyHandler }, h("div", { class: CSS.inputWrapper }, h("slot", null)), h("div", { class: CSS.controlsWrapper }, h("calcite-button", { appearance: "transparent", class: CSS.enableEditingButton, disabled: this.disabled, iconStart: "pencil", kind: "neutral", label: this.messages.enableEditing, onClick: this.enableEditingHandler, scale: this.scale, style: {
opacity: this.editingEnabled ? "0" : "1",
width: this.editingEnabled ? "0" : "inherit"
}, type: "button",
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => (this.enableEditingButton = el) }), this.shouldShowControls && [
h("div", { class: CSS.cancelEditingButtonWrapper }, h("calcite-button", { appearance: "transparent", class: CSS.cancelEditingButton, disabled: this.disabled, iconStart: "x", kind: "neutral", label: this.messages.cancelEditing, onClick: this.cancelEditingHandler, scale: this.scale, type: "button",
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => (this.cancelEditingButton = el) })),
h("calcite-button", { appearance: "solid", class: CSS.confirmChangesButton, disabled: this.disabled, iconStart: "check", kind: "brand", label: this.messages.confirmChanges, loading: this.loading, onClick: this.confirmChangesHandler, scale: this.scale, type: "button",
// eslint-disable-next-line react/jsx-sort-props
ref: (el) => (this.confirmEditingButton = el) })
])));
}
//--------------------------------------------------------------------------
//
// Event Listeners
//
//--------------------------------------------------------------------------
blurHandler() {
if (!this.controls) {
this.disableEditing();
}
}
effectiveLocaleChange() {
updateMessages(this, this.effectiveLocale);
}
//--------------------------------------------------------------------------
//
// Public Methods
//
//--------------------------------------------------------------------------
/** Sets focus on the component. */
async setFocus() {
await componentLoaded(this);
this.el?.focus();
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
mutationObserverCallback() {
this.updateSlottedInput();
this.scale = this.scale || this.inputElement?.scale;
}
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;
}
static get delegatesFocus() { return true; }
static get assetsDirs() { return ["assets"]; }
get el() { return getElement(this); }
static get watchers() { return {
"disabled": ["disabledWatcher"],
"editingEnabled": ["editingEnabledWatcher"],
"messageOverrides": ["onMessagesChange"],
"effectiveLocale": ["effectiveLocaleChange"]
}; }
};
InlineEditable.style = inlineEditableCss;
export { InlineEditable as calcite_inline_editable };