|
1 | 1 |
|
2 |
| -import { LitElement, html, css } from 'lit'; |
| 2 | +import { LitElement, html, css, nothing } from 'lit'; |
3 | 3 |
|
4 | 4 | /**
|
5 |
| - * FormElement |
| 5 | + * FormElement is the base class for all form elements |
6 | 6 | */
|
7 | 7 | export class FormElement extends LitElement {
|
| 8 | + #internals; |
| 9 | + |
8 | 10 | constructor() {
|
9 | 11 | super();
|
| 12 | + |
| 13 | + // Attach with the form |
| 14 | + this.#internals = this.attachInternals(); |
| 15 | + |
10 | 16 | // Default properties
|
11 |
| - this.name = 'wc-form'; |
12 |
| - this.method = "POST"; |
13 |
| - this.action = "#"; |
| 17 | + this.name = this.textContent; |
| 18 | + this.value = ''; |
| 19 | + this.description = ''; |
| 20 | + this.placeholder = ''; |
| 21 | + this.disabled = false; |
| 22 | + this.required = false; |
| 23 | + this.autocomplete = false; |
| 24 | + this.labelabove = false; |
| 25 | + } |
| 26 | + static get formAssociated() { |
| 27 | + return true; |
14 | 28 | }
|
15 | 29 | static get properties() {
|
16 | 30 | return {
|
17 | 31 | /**
|
18 |
| - * Name of the form |
19 |
| - * @type {String} |
| 32 | + * The name for the input |
| 33 | + * @type {string} |
20 | 34 | */
|
21 | 35 | name: { type: String },
|
22 | 36 |
|
23 | 37 | /**
|
24 |
| - * The method for submitting the form (POST or GET) |
| 38 | + * The value for the input |
25 | 39 | * @type {string}
|
26 | 40 | */
|
27 |
| - method: { type: String }, |
| 41 | + value: { type: String, reflect: true }, |
28 | 42 |
|
29 | 43 | /**
|
30 |
| - * The action when submitting the form |
| 44 | + * Description of the input |
| 45 | + */ |
| 46 | + description: { type: String }, |
| 47 | + |
| 48 | + /** |
| 49 | + * The placeholder text for the input |
31 | 50 | * @type {string}
|
32 | 51 | */
|
33 |
| - action: { type: String }, |
| 52 | + placeholder: { type: String }, |
| 53 | + |
| 54 | + /** |
| 55 | + * Whether the control is enabled |
| 56 | + * @type {boolean} |
| 57 | + */ |
| 58 | + disabled: { type: Boolean }, |
| 59 | + |
| 60 | + /** |
| 61 | + * Whether the input is required to be filled |
| 62 | + * for the form to submit |
| 63 | + * @type {boolean} |
| 64 | + */ |
| 65 | + required: { type: Boolean }, |
| 66 | + |
| 67 | + /** |
| 68 | + * Whether auto-complete is enabled |
| 69 | + * @type {boolean} |
| 70 | + */ |
| 71 | + autocomplete: { type: Boolean }, |
| 72 | + |
| 73 | + /** |
| 74 | + * Label position above, rather than to the left |
| 75 | + * @type {boolean} |
| 76 | + */ |
| 77 | + labelabove: { type: Boolean } |
34 | 78 | };
|
35 | 79 | }
|
36 | 80 | static get styles() {
|
37 | 81 | return css`
|
38 |
| - form { |
39 |
| - display: inline-block; |
40 |
| - width: 100%; |
41 |
| - margin: var(--form-margin); |
| 82 | + div { |
| 83 | + display: flex; |
| 84 | + justify-content: start; |
| 85 | + border: 1px solid red; |
42 | 86 | }
|
43 |
| - `; |
| 87 | + .labelbeside { |
| 88 | + flex-direction: row; |
| 89 | + } |
| 90 | + .labelabove { |
| 91 | + flex-direction: column; |
| 92 | + } |
| 93 | + div.description { |
| 94 | + flex-direction: column; |
| 95 | + color: var(--form-input-description-color); |
| 96 | + font-size: var(--form-input-description-font-size); |
| 97 | + font-weight: var(--form-input-description-font-weight); |
| 98 | + } |
| 99 | + .control { |
| 100 | + border: 1px solid red; |
| 101 | + flex: 1; |
| 102 | + background-color: var(--form-input-background-color); |
| 103 | + color: var(--form-input-color); |
| 104 | + margin: var(--form-input-margin-y) var(--form-input-margin-x) var(--form-input-margin-y) var(--form-input-margin-x); |
| 105 | + padding: var(--form-input-padding-y) var(--form-input-padding-x) var(--form-input-padding-y) var(--form-input-padding-x); |
| 106 | + font-family: inherit; |
| 107 | + font-size: var(--form-input-font-size); |
| 108 | + font-weight: var(--form-input-font-weight); |
| 109 | + line-height: var(--form-input-line-height); |
| 110 | + border: var(--form-input-border); |
| 111 | + } |
| 112 | + .control::placeholder { |
| 113 | + color: var(--form-input-placeholder-color); |
| 114 | + } |
| 115 | + .error { |
| 116 | + color: var(--form-input-error-color); |
| 117 | + } |
| 118 | + label { |
| 119 | + display: block; |
| 120 | + min-width: 10em; |
| 121 | + background-color: var(--form-label-background-color); |
| 122 | + color: var(--form-label-color); |
| 123 | + padding: var(--form-label-padding-y) var(--form-label-padding-x) var(--form-label-padding-y) var(--form-label-padding-x); |
| 124 | + font-size: var(--form-label-font-size); |
| 125 | + font-weight: var(--form-label-font-weight); |
| 126 | + line-height: var(--form-label-line-height); |
| 127 | + border: var(--form-label-border) solid red; |
| 128 | + } |
| 129 | + `; |
| 130 | + } |
| 131 | + _renderLabel() { |
| 132 | + if (this.innerHTML) { |
| 133 | + return html`<label for="${this.name}"><slot></slot></label>`; |
| 134 | + } else { |
| 135 | + return nothing; |
| 136 | + } |
| 137 | + } |
| 138 | + _renderInput() { |
| 139 | + return html`<input |
| 140 | + class="control" |
| 141 | + name=${this.name || nothing} |
| 142 | + value=${this.value || nothing} |
| 143 | + ?required=${this.required} |
| 144 | + ?disabled=${this.disabled} |
| 145 | + placeholder=${this.placeholder || nothing} |
| 146 | + autocomplete=${this.autocomplete ? 'on' : 'off'} |
| 147 | + @input=${this._updateValue} |
| 148 | + >`; |
| 149 | + } |
| 150 | + _renderDescription() { |
| 151 | + if (this.validationMessage) { |
| 152 | + return html`<span class="error">${this.validationMessage}</span>`; |
| 153 | + } else if (this.description) { |
| 154 | + return html`<span>${this.description}</span>`; |
| 155 | + } else { |
| 156 | + return nothing; |
| 157 | + } |
44 | 158 | }
|
45 | 159 | render() {
|
| 160 | + const renderLabel = this._renderLabel(); |
| 161 | + const renderInput = this._renderInput(); |
| 162 | + const renderDescription = this._renderDescription(); |
46 | 163 | return html`
|
47 |
| - <form method="${this.method}" name="${this.name}" action="${this.action}" @submit=${this.onSubmit}> |
48 |
| - <slot></slot> |
49 |
| - </form> |
50 |
| - `; |
| 164 | + <div class="${this.labelabove ? 'labelabove' : 'labelbeside'}"> |
| 165 | + ${renderLabel} |
| 166 | + <div class="description"> |
| 167 | + ${renderInput} |
| 168 | + ${renderDescription} |
| 169 | + </div> |
| 170 | + </div> |
| 171 | + `; |
51 | 172 | }
|
52 |
| - submit() { |
53 |
| - if(this.shadowRoot) { |
54 |
| - this.shadowRoot.querySelector('form').submit(); |
| 173 | + |
| 174 | + // Form control properties |
| 175 | + get form() { return this.#internals ? this.#internals.form : null; } |
| 176 | + get type() { return this.localName; } |
| 177 | + get validity() { return this.#internals ? this.#internals.validity : null; } |
| 178 | + get validationMessage() { return this.#internals ? this.#internals.validationMessage : null; } |
| 179 | + get willValidate() { return this.#internals ? this.#internals.willValidate : null; } |
| 180 | + |
| 181 | + // Callbacks |
| 182 | + _updateValue(event) { |
| 183 | + if (!this.disabled) { |
| 184 | + this.value = event.target.value; |
| 185 | + this.#internals.setFormValue(this.value); |
55 | 186 | }
|
56 | 187 | }
|
57 |
| - onSubmit(event) { |
58 |
| - event.preventDefault(); |
59 |
| - console.log('Form submitted'); |
60 |
| - } |
61 | 188 | }
|
62 | 189 |
|
63 |
| -customElements.define('wc-form', FormElement); |
| 190 | +customElements.define('wc-form-input', FormElement); |
0 commit comments