import { css, html, LitElement } from "lit"; import { customElement, property, state } from "lit/decorators.js"; import "../../field-header.js"; @customElement("default-array-input") export class DefaultArrayInput extends LitElement { static override styles = [ css` .property { display: flex; flex-direction: column; gap: 12px; } .item-container { display: flex; flex-direction: column; gap: 4px; padding-bottom: 8px; } .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 { flex-grow: 1; } .add-input::part(suffix) { cursor: pointer; } .new-line-container { display: flex; gap: 4px; } .icon-wrapper { display: flex; } `, ]; @property() property: string = ""; @property() moduleId?: string; @property() value: Array = []; @property() schema: any = {}; @property() required?: boolean = false; @property() handleInlangProjectChange: ( value: Array, 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; handleInputChange(e: Event) { const inputElement = e.target as HTMLInputElement; this._inputValue = inputElement.value; } handleAddItemClick() { if (this._inputValue && this._inputValue.trim() !== "") { this.value ? this.value.push(this._inputValue) : (this.value = [this._inputValue]); this.handleInlangProjectChange(this.value, this.property, this.moduleId); this._inputValue = "null"; this._inputValue = undefined; } } handleDeleteItemClick(index: number) { if (this.value) { this.value.splice(index, 1); this.handleInlangProjectChange(this.value, this.property, this.moduleId); this._inputValue = "null"; this._inputValue = undefined; } } override render() { return html`
${this.value && this.value.length > 0 ? html`
${this.value.map((arrayItem, index) => { return html`
{ this.handleDeleteItemClick(index); }} >
`; })}
` : undefined}
this.handleInputChange(e)} @keydown=${(e: KeyboardEvent) => { if (e.key === "Enter") { this.handleAddItemClick(); } }} value=${this._inputValue} > { this.handleAddItemClick(); }} > Add
`; } } // add types declare global { interface HTMLElementTagNameMap { "default-array-input": DefaultArrayInput; } }