|
| 1 | + |
| 2 | +import { LitElement, html, css } from 'lit'; |
| 3 | + |
| 4 | +/** |
| 5 | + * FormInputElement |
| 6 | + */ |
| 7 | +export class FormInputElement extends LitElement { |
| 8 | + constructor() { |
| 9 | + super(); |
| 10 | + // Default properties |
| 11 | + this.name = 'input'; |
| 12 | + this.required = false; |
| 13 | + this.autocomplete = false; |
| 14 | + this.labelabove = false; |
| 15 | + } |
| 16 | + static get properties() { |
| 17 | + return { |
| 18 | + /** |
| 19 | + * The name for the input |
| 20 | + * @type {string} |
| 21 | + */ |
| 22 | + name: { type: String }, |
| 23 | + |
| 24 | + /** |
| 25 | + * The value for the input |
| 26 | + * @type {string} |
| 27 | + */ |
| 28 | + value: { type: String }, |
| 29 | + |
| 30 | + /** |
| 31 | + * The placeholder text for the input |
| 32 | + * @type {string} |
| 33 | + */ |
| 34 | + placeholder: { type: String }, |
| 35 | + |
| 36 | + /** |
| 37 | + * Whether the input is required to be filled |
| 38 | + * for the form to submit |
| 39 | + * @type {boolean} |
| 40 | + */ |
| 41 | + required: { type: Boolean }, |
| 42 | + |
| 43 | + /** |
| 44 | + * The pattern used to validate the value on submission |
| 45 | + * @type {string} |
| 46 | + */ |
| 47 | + pattern: { type: String }, |
| 48 | + |
| 49 | + /** |
| 50 | + * Whether auto-complete is enabled |
| 51 | + * @type {boolean} |
| 52 | + */ |
| 53 | + autocomplete: { type: Boolean }, |
| 54 | + |
| 55 | + /** |
| 56 | + * Label position above, rather than to the left |
| 57 | + * @type {boolean} |
| 58 | + */ |
| 59 | + labelabove: { type: Boolean }, |
| 60 | + }; |
| 61 | + } |
| 62 | + static get styles() { |
| 63 | + return css` |
| 64 | + div { |
| 65 | + display: flex; |
| 66 | + justify-content: start; |
| 67 | + } |
| 68 | + .labelbeside { |
| 69 | + flex-direction: row; |
| 70 | + } |
| 71 | + .labelabove { |
| 72 | + flex-direction: column; |
| 73 | + } |
| 74 | + input { |
| 75 | + flex: 1; |
| 76 | + background-color: var(--form-input-background-color); |
| 77 | + color: var(--form-input-color); |
| 78 | + margin: var(--form-input-margin-y) var(--form-input-margin-x) var(--form-input-margin-y) var(--form-input-margin-x); |
| 79 | + padding: var(--form-input-padding-y) var(--form-input-padding-x) var(--form-input-padding-y) var(--form-input-padding-x); |
| 80 | + font-size: var(--form-input-font-size); |
| 81 | + font-weight: var(--form-input-font-weight); |
| 82 | + line-height: var(--form-input-line-height); |
| 83 | + border: var(--form-input-border); |
| 84 | + } |
| 85 | + input:focus { |
| 86 | + background-color: var(--form-input-background-color-focus); |
| 87 | + color: var(--form-input-color-focus); |
| 88 | + border: var(--form-input-border-focus); |
| 89 | + } |
| 90 | + label { |
| 91 | + display: block; |
| 92 | + min-width: 10em; |
| 93 | + background-color: var(--form-label-background-color); |
| 94 | + color: var(--form-label-color); |
| 95 | + padding: var(--form-label-padding); |
| 96 | + font-size: var(--form-label-font-size); |
| 97 | + font-weight: var(--form-label-font-weight); |
| 98 | + line-height: var(--form-label-line-height); |
| 99 | + border: var(--form-label-border); |
| 100 | + } |
| 101 | + `; |
| 102 | + } |
| 103 | + render() { |
| 104 | + return html` |
| 105 | + <div class="${this.labelabove ? 'labelabove' : 'labelbeside'}"> |
| 106 | + <label for="${this.name}"><slot></slot></label> |
| 107 | + <input type="text" name="${this.name}" ?value="${this.value}" ?required="${this.required}" placeholder="${this.placeholder}" autocomplete="${this.autocomplete ? 'on' : 'off'}" ?pattern="${this.pattern}"> |
| 108 | + </div> |
| 109 | + `; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +customElements.define('wc-form-input', FormInputElement); |
0 commit comments