@kelvininc/ui-components
Version:
Kelvin UI Components
150 lines (145 loc) • 6.88 kB
JavaScript
import { r as registerInstance, c as createEvent, h, H as Host, F as Fragment, g as getElement } from './index-D-JVwta2.js';
import { E as EActionButtonType } from './action-button.types-DVds6a5Z.js';
import { E as EIconName } from './icon.types-SVedE_O8.js';
const DEFAULT_MAX_LENGTH = 64;
const DELAYED_BLUR_MS = 200;
const inlineEditableFieldCss = "@property --rotation{syntax:\"<angle>\";initial-value:0deg;inherits:false}@keyframes rotate-border{to{--rotation:360deg}}.inline-editable-field-container{display:inline-flex;align-items:center;width:100%;--margin-top-bottom:var(--kv-spacing, 4px);--margin-left-right:var(--kv-spacing-2x, 8px)}.inline-editable-field-container__hover .inline-editable-field-slot{border-radius:4px;background:var(--kv-neutral-8, #202020)}.inline-editable-field-slot[contenteditable]{padding:var(--margin-top-bottom) var(--margin-left-right);margin:calc(var(--margin-top-bottom) * -1) calc(var(--margin-left-right) * -1);white-space:nowrap;overflow:hidden;outline:1px solid transparent;transition:all 0.25s ease-in-out}.inline-editable-field-slot[contenteditable]:hover,.inline-editable-field-slot[contenteditable]:focus{border-radius:4px;background:var(--kv-neutral-8, #202020)}.inline-editable-field-slot[contenteditable]:focus{outline-color:var(--kv-neutral-2, #e5e5e5);text-overflow:unset}.inline-editable-field-slot br{display:none}.inline-editable-field-slot *{display:inline;white-space:nowrap}.inline-editable-field-actions{display:inline-flex;gap:var(--kv-spacing, 4px);flex:0;margin-left:calc(var(--margin-left-right) + var(--kv-spacing-2x, 8px))}.inline-editable-field-actions__focus{width:64px;align-self:flex-end;margin-bottom:calc(var(--margin-left-right) * -1)}";
const KvInlineEditableField = class {
constructor(hostRef) {
registerInstance(this, hostRef);
this.contentEdited = createEvent(this, "contentEdited", 7);
this.contentFocused = createEvent(this, "contentFocused", 7);
this.contentBlured = createEvent(this, "contentBlured", 7);
/**
* The maximum length of the editable field.
*/
this.maxLength = DEFAULT_MAX_LENGTH;
this.isHovering = false;
this.isEditing = false;
this.isSaving = false;
this.isSaveDisabled = false;
this.discardContent = () => {
this.slotEl.innerText = this.value;
};
this.saveChanges = () => {
this.slotEl.blur();
const newValue = this.slotEl.innerText;
if (newValue.length === 0)
return;
if (newValue !== this.value) {
this.isSaving = true;
this.contentEdited.emit(newValue);
}
};
this.handleBlur = () => {
this.slotEl.innerText = this.slotEl.innerText.trim();
this.timeoutID = window.setTimeout(() => {
// if saving button hasn't clicked, discard content
if (!this.isSaving) {
this.discardContent();
}
this.isEditing = false;
this.isSaving = false;
this.contentBlured.emit();
}, DELAYED_BLUR_MS);
};
this.handleFocus = () => {
clearTimeout(this.timeoutID);
this.isEditing = true;
this.contentFocused.emit();
this.checkSaveBtnDisabled();
};
this.handleMaxLength = (event) => {
if (this.maxLength && this.slotEl.innerText.length >= this.maxLength) {
event.preventDefault();
}
};
this.checkSaveBtnDisabled = () => {
this.isSaveDisabled = this.slotEl.innerText.trim().length === 0;
};
}
/**
* Resets the content of the editable field.
*/
async resetContent() {
this.discardContent();
this.slotEl.blur();
}
handleMouseHover() {
this.isHovering = true;
}
handleMouseLeave() {
this.isHovering = false;
}
handleKeyDown(event) {
if (!this.isEditing || this.disabled)
return;
switch (event.key) {
case 'Escape':
this.slotEl.blur();
break;
case 'Enter':
this.saveChanges();
break;
case 'Backspace':
break;
default:
this.handleMaxLength(event);
}
}
handleDisableChange(state) {
if (state) {
this.destroyEditableContent();
}
else {
this.initializeEditableContent();
}
}
initializeEditableContent() {
this.slotEl.classList.add('inline-editable-field-slot');
this.slotEl.setAttribute('contenteditable', 'true');
this.slotEl.addEventListener('blur', this.handleBlur);
this.slotEl.addEventListener('focus', this.handleFocus);
this.slotEl.addEventListener('input', this.checkSaveBtnDisabled);
}
destroyEditableContent() {
if (!this.slotEl)
return;
this.slotEl.classList.remove('inline-editable-field-slot');
this.slotEl.removeAttribute('contenteditable');
this.slotEl.removeEventListener('blur', this.handleBlur);
this.slotEl.removeEventListener('focus', this.handleFocus);
this.slotEl.removeEventListener('input', this.checkSaveBtnDisabled);
}
connectedCallback() {
if (this.disabled) {
return;
}
if (this.el.children.length !== 1) {
throw new Error('Inline editable field must have exactly one child element to be editable');
}
this.slotEl = this.el.children[0];
this.initializeEditableContent();
}
disconnectedCallback() {
this.destroyEditableContent();
}
render() {
if (this.disabled) {
return (h(Host, null, h("slot", null)));
}
return (h(Host, { class: {
'inline-editable-field-container': true,
'inline-editable-field-container__hover': this.isHovering
} }, h("slot", null), h("div", { class: {
'inline-editable-field-actions': true,
'inline-editable-field-actions__focus': this.isEditing
} }, this.isEditing && (h(Fragment, null, h("kv-action-button-icon", { type: EActionButtonType.Ghost, icon: EIconName.Close, onClickButton: this.discardContent }), h("kv-action-button-icon", { type: EActionButtonType.Ghost, icon: EIconName.DoneAll, disabled: this.isSaveDisabled, onClickButton: this.saveChanges }))))));
}
get el() { return getElement(this); }
static get watchers() { return {
"disabled": ["handleDisableChange"]
}; }
};
KvInlineEditableField.style = inlineEditableFieldCss;
export { KvInlineEditableField as kv_inline_editable_field };