UNPKG

@kelvininc/ui-components

Version:
152 lines (146 loc) 6.95 kB
'use strict'; var index = require('./index-DpuMIXDY.js'); var actionButton_types = require('./action-button.types-BYOe6st0.js'); var icon_types = require('./icon.types-B8lvUrX_.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) { index.registerInstance(this, hostRef); this.contentEdited = index.createEvent(this, "contentEdited", 7); this.contentFocused = index.createEvent(this, "contentFocused", 7); this.contentBlured = index.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 (index.h(index.Host, null, index.h("slot", null))); } return (index.h(index.Host, { class: { 'inline-editable-field-container': true, 'inline-editable-field-container__hover': this.isHovering } }, index.h("slot", null), index.h("div", { class: { 'inline-editable-field-actions': true, 'inline-editable-field-actions__focus': this.isEditing } }, this.isEditing && (index.h(index.Fragment, null, index.h("kv-action-button-icon", { type: actionButton_types.EActionButtonType.Ghost, icon: icon_types.EIconName.Close, onClickButton: this.discardContent }), index.h("kv-action-button-icon", { type: actionButton_types.EActionButtonType.Ghost, icon: icon_types.EIconName.DoneAll, disabled: this.isSaveDisabled, onClickButton: this.saveChanges })))))); } get el() { return index.getElement(this); } static get watchers() { return { "disabled": ["handleDisableChange"] }; } }; KvInlineEditableField.style = inlineEditableFieldCss; exports.kv_inline_editable_field = KvInlineEditableField;