|
| 1 | +import { LitElement, html, css, nothing } from 'lit'; |
| 2 | +import { customElement, property } from 'lit/decorators.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * @class FormInput |
| 6 | + * |
| 7 | + * This class is a form input element, which should be wrapped in a form element. |
| 8 | + * |
| 9 | + * @example |
| 10 | + * <wc-forminput name="email" value="">...</wc-forminput> |
| 11 | + */ |
| 12 | +@customElement('wc-forminput') |
| 13 | +export class FormInput extends LitElement { |
| 14 | + #internals: ElementInternals; |
| 15 | + @property({ type: String }) name: string; |
| 16 | + @property({ type: String }) value: string; |
| 17 | + @property({ type: Boolean }) disabled: boolean; |
| 18 | + @property({ type: Boolean }) required: boolean; |
| 19 | + @property({ type: Boolean }) autocomplete: boolean; |
| 20 | + |
| 21 | + constructor() { |
| 22 | + super(); |
| 23 | + |
| 24 | + // Attach with the form |
| 25 | + this.#internals = this.attachInternals(); |
| 26 | + } |
| 27 | + |
| 28 | + render() { |
| 29 | + return html` |
| 30 | + <label class=${this.className || nothing}> |
| 31 | + <slot></slot><br> |
| 32 | + <input |
| 33 | + name=${this.name || nothing} |
| 34 | + value=${this.value || nothing} |
| 35 | + ?disabled=${this.disabled} |
| 36 | + ?required=${this.required} |
| 37 | + ?autocomplete=${this.autocomplete} |
| 38 | + @input=${this.#onInput}> |
| 39 | + </label> |
| 40 | + `; |
| 41 | + } |
| 42 | + |
| 43 | + static get styles() { |
| 44 | + return css` |
| 45 | + :host { |
| 46 | + margin: var(--form-control-margin); |
| 47 | + } |
| 48 | + label { |
| 49 | + cursor: pointer; |
| 50 | + user-select: none; |
| 51 | + font-size: var(--form-input-font-size-label); |
| 52 | + } |
| 53 | + input { |
| 54 | + margin: var(--form-input-margin); |
| 55 | + padding: var(--form-input-padding); |
| 56 | + border-width: var(--form-input-border-width); |
| 57 | + border-color: var(--form-input-border-color); |
| 58 | + background-color: var(--form-input-background-color); |
| 59 | + width: 100%; |
| 60 | + } |
| 61 | + `; |
| 62 | + } |
| 63 | + |
| 64 | + get className() { |
| 65 | + const classes = []; |
| 66 | + if (this.disabled) { |
| 67 | + classes.push('disabled'); |
| 68 | + } |
| 69 | + if (this.required) { |
| 70 | + classes.push('required'); |
| 71 | + } |
| 72 | + if (this.autocomplete) { |
| 73 | + classes.push('autocomplete'); |
| 74 | + } |
| 75 | + return classes.join(' '); |
| 76 | + } |
| 77 | + |
| 78 | + static get formAssociated() { |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + // Form control properties |
| 83 | + get form() { return this.#internals ? this.#internals.form : null; } |
| 84 | + |
| 85 | + // Form control properties |
| 86 | + get type() { return this.localName; } |
| 87 | + |
| 88 | + // Form control properties |
| 89 | + get validity() { return this.#internals ? this.#internals.validity : null; } |
| 90 | + |
| 91 | + // Form control properties |
| 92 | + get validationMessage() { return this.#internals ? this.#internals.validationMessage : null; } |
| 93 | + |
| 94 | + // Form control properties |
| 95 | + get willValidate() { return this.#internals ? this.#internals.willValidate : null; } |
| 96 | + |
| 97 | + |
| 98 | + // Event hander for input event to update the value |
| 99 | + #onInput(event: Event) { |
| 100 | + if (!this.disabled) { |
| 101 | + this.value = event.target.value; |
| 102 | + this.#internals.setFormValue(this.value); |
| 103 | + return true; |
| 104 | + } |
| 105 | + return false; |
| 106 | + } |
| 107 | +} |
0 commit comments