|
| 1 | +import { html, LitElement } from 'lit' |
| 2 | +import { customElement, property, state } from 'lit/decorators.js' |
| 3 | +import { log, table } from '../utils/console' |
| 4 | +import type { SelectionMode } from '../types' |
| 5 | +import { BoxIcons } from '../types' |
| 6 | + |
| 7 | +@customElement('little-layout-field') |
| 8 | +export class LittleLayoutField extends LitElement { |
| 9 | + /** |
| 10 | + * =========================================================================== |
| 11 | + * PROPS |
| 12 | + * =========================================================================== |
| 13 | + */ |
| 14 | + /** |
| 15 | + * Pass-through prop based on field setting. |
| 16 | + */ |
| 17 | + @property({ attribute: 'box-icons', type: Object }) |
| 18 | + boxIcons: BoxIcons = {} |
| 19 | + |
| 20 | + /** |
| 21 | + * Pass-through prop based on field setting. |
| 22 | + */ |
| 23 | + @property({ type: Boolean }) |
| 24 | + clearable = false |
| 25 | + |
| 26 | + /** |
| 27 | + * The default value of the field, based on the field’s current value or the default set in field settings. |
| 28 | + */ |
| 29 | + @property({ attribute: 'default-value' }) |
| 30 | + defaultValue = '' |
| 31 | + |
| 32 | + /** |
| 33 | + * Pass-through prop based on field setting. |
| 34 | + */ |
| 35 | + @property({ type: Boolean }) |
| 36 | + editable = false |
| 37 | + |
| 38 | + /** |
| 39 | + * The ID attribute for the input field. |
| 40 | + */ |
| 41 | + @property({ attribute: 'field-id' }) |
| 42 | + fieldId = '' |
| 43 | + |
| 44 | + /** |
| 45 | + * The name attribute for the input field. |
| 46 | + */ |
| 47 | + @property({ attribute: 'field-name' }) |
| 48 | + fieldName = '' |
| 49 | + |
| 50 | + /** |
| 51 | + * Pass-through prop based on field setting. |
| 52 | + */ |
| 53 | + @property({ attribute: 'layout-cols', type: Number }) |
| 54 | + layoutCols = 1 |
| 55 | + |
| 56 | + /** |
| 57 | + * Pass-through prop based on field setting. |
| 58 | + */ |
| 59 | + @property({ attribute: 'layout-rows', type: Number }) |
| 60 | + layoutRows = 1 |
| 61 | + |
| 62 | + /** |
| 63 | + * Pass-through prop based on field setting. |
| 64 | + */ |
| 65 | + @property({ attribute: 'selection-mode' }) |
| 66 | + selectionMode: SelectionMode = 'box' |
| 67 | + |
| 68 | + /** |
| 69 | + * ========================================================================= |
| 70 | + * STATE |
| 71 | + * ========================================================================= |
| 72 | + */ |
| 73 | + /** |
| 74 | + * The value attribute for the input field. This is updated via an event from the child component. |
| 75 | + */ |
| 76 | + @state() |
| 77 | + private _fieldValue = '' |
| 78 | + |
| 79 | + /** |
| 80 | + * ========================================================================= |
| 81 | + * EVENTS |
| 82 | + * ========================================================================= |
| 83 | + */ |
| 84 | + /** |
| 85 | + * Handles the `@value-updated` event. Sets the `_fieldValue` passed in from the event. |
| 86 | + */ |
| 87 | + private _fieldValueListener(e: CustomEvent) { |
| 88 | + this._fieldValue = e.detail.fieldValue |
| 89 | + this._setInputValueFromFieldValue() |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * ========================================================================= |
| 94 | + * METHODS |
| 95 | + * ========================================================================= |
| 96 | + */ |
| 97 | + /** |
| 98 | + * Sets `_fieldValue` from the value of the input. |
| 99 | + */ |
| 100 | + private _getFieldValueFromInput() { |
| 101 | + const littleLayoutInput = this.querySelector('input') |
| 102 | + |
| 103 | + if (littleLayoutInput) { |
| 104 | + this._fieldValue = littleLayoutInput.value |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the value of the input from `_fieldValue`. |
| 110 | + */ |
| 111 | + private _setInputValueFromFieldValue() { |
| 112 | + const littleLayoutInput = this.querySelector('input') |
| 113 | + |
| 114 | + if (littleLayoutInput) { |
| 115 | + littleLayoutInput.value = this._fieldValue |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * ========================================================================= |
| 121 | + * LIFECYCLE |
| 122 | + * ========================================================================= |
| 123 | + */ |
| 124 | + connectedCallback() { |
| 125 | + super.connectedCallback() |
| 126 | + |
| 127 | + this._getFieldValueFromInput() |
| 128 | + |
| 129 | + const littleLayoutInput = this.querySelector('input') |
| 130 | + |
| 131 | + if (littleLayoutInput) { |
| 132 | + littleLayoutInput.addEventListener('change', this._getFieldValueFromInput) |
| 133 | + littleLayoutInput.addEventListener('input', this._getFieldValueFromInput) |
| 134 | + } |
| 135 | + |
| 136 | + log('Little Layout: connected') |
| 137 | + table({ |
| 138 | + clearable: this.clearable, |
| 139 | + layoutCols: this.layoutCols, |
| 140 | + defaultValue: this.defaultValue, |
| 141 | + editable: this.editable, |
| 142 | + fieldId: this.fieldId, |
| 143 | + fieldName: this.fieldName, |
| 144 | + layoutRows: this.layoutRows, |
| 145 | + selectionMode: this.selectionMode, |
| 146 | + }) |
| 147 | + } |
| 148 | + |
| 149 | + disconnectedCallback() { |
| 150 | + super.disconnectedCallback() |
| 151 | + |
| 152 | + const littleLayoutInput = this.querySelector('input') |
| 153 | + |
| 154 | + if (littleLayoutInput) { |
| 155 | + littleLayoutInput.removeEventListener('change', this._getFieldValueFromInput) |
| 156 | + littleLayoutInput.removeEventListener('input', this._getFieldValueFromInput) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + render() { |
| 161 | + return html` |
| 162 | + <little-layout-field-control |
| 163 | + ?clearable="${this.clearable}" |
| 164 | + ?editable="${this.editable}" |
| 165 | + box-icons="${JSON.stringify(this.boxIcons)}" |
| 166 | + field-default="${this._fieldValue}" |
| 167 | + layout-cols="${this.layoutCols}" |
| 168 | + layout-rows="${this.layoutRows}" |
| 169 | + selection-mode="${this.selectionMode}" |
| 170 | + @value-updated="${this._fieldValueListener}" |
| 171 | + ></little-layout-field-control> |
| 172 | + ` |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Changes the render mode for this component from shadow DOM to light DOM. |
| 177 | + */ |
| 178 | + protected createRenderRoot() { |
| 179 | + return this |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +declare global { |
| 184 | + interface HTMLElementTagNameMap { |
| 185 | + 'little-layout-field': LittleLayoutField |
| 186 | + } |
| 187 | +} |
0 commit comments