import { css, html, LitElement } from "lit" import { customElement, property, state } from "lit/decorators.js" @customElement("default-object-input") export class DefaultObjectInput extends LitElement { static override styles = [ css` .property { display: flex; flex-direction: column; gap: 12px; } .disabled-input::part(base) { cursor: unset; opacity: 1; } .disabled-input::part(suffix) { cursor: pointer; opacity: 0.5; } .disabled-input::part(suffix):hover { opacity: 1; } .add-input::part(suffix) { cursor: pointer; } .add-item-container { display: flex; align-items: center; gap: 4px; } .add-item-side { flex-grow: 1; } .remove-icon { width: 44px; display: flex; justify-content: flex-start; margin-left: 6px; cursor: pointer; color: var(--sl-input-placeholder-color); } .remove-icon:hover { color: var(--sl-input-color); } .list-container { display: flex; flex-direction: column; gap: 3px; padding-bottom: 8px; } .icon { padding-top: 0.5rem; } sl-input::part(input) { width: inherit; } `, ] @property() property: string = "" @property() keyPlaceholder?: string = "Enter key" @property() valuePlaceholder?: string = "Enter value" @property() moduleId?: string @property() value: Record = {} @property() schema: any = {} @property() withTitle?: boolean = true @property() withDescription?: boolean = true @property() required?: boolean = false @property() handleInlangProjectChange: ( value: Record, key: string, moduleId?: string ) => void = () => {} private get _description(): string | undefined { return this.schema.description || undefined } private get _title(): string | undefined { return this.schema.title || undefined } @state() private _inputValue: string | undefined = undefined @state() private _inputKey: string | undefined = undefined handleAddItemClick() { if ( this._inputValue && this._inputKey && this._inputValue.trim() !== "" && this._inputKey.trim() !== "" ) { if (!this.value) { this.value = {} } this.value[this._inputKey as any] = this._inputValue this.handleInlangProjectChange(this.value, this.property, this.moduleId) this._inputValue = "null" this._inputValue = undefined this._inputKey = "null" this._inputKey = undefined } } handleDeleteItemClick(key: string) { if (this.value) { delete this.value[key] this.handleInlangProjectChange(this.value, this.property, this.moduleId) this._inputValue = "null" this._inputValue = undefined this._inputKey = "null" this._inputKey = undefined } } override render() { return html`
${this.value ? html`
${this.value && Object.entries(this.value).map(([key, value]) => { return html`
{ this.handleDeleteItemClick(key as string) }} >
` })}
` : ``}
{ this._inputKey = (e.target as HTMLInputElement).value }} @keydown=${(e: KeyboardEvent) => { if (e.key === "Enter") { this.handleAddItemClick() } }} value=${this._inputKey} > { this._inputValue = (e.target as HTMLInputElement).value }} @keydown=${(e: KeyboardEvent) => { if (e.key === "Enter") { this.handleAddItemClick() } }} value=${this._inputValue} > { this.handleAddItemClick() }} > Add
` } } // add types declare global { interface HTMLElementTagNameMap { "default-object-input": DefaultObjectInput } }