Renovate bot | aa5fb8e | 2024-02-25 18:10:09 +0000 | [diff] [blame] | 1 | import {css, html, LitElement} from 'lit'; |
| 2 | |
| 3 | /** |
| 4 | * Lite component inspired in the now removed material-web's Formfield. |
| 5 | * https://github.com/material-components/material-web/commit/753a03be963f7b5242e98b73d1309abbe9f5bf51 |
| 6 | */ |
| 7 | export default class TwptFormField extends LitElement { |
| 8 | static styles = [ |
| 9 | css` |
| 10 | .formfield { |
| 11 | align-items: center; |
| 12 | display: inline-flex; |
| 13 | vertical-align: middle; |
| 14 | } |
| 15 | |
| 16 | label { |
| 17 | margin-inline: 0px auto; |
| 18 | order: 0; |
| 19 | padding-inline: 4px 0px; |
| 20 | } |
| 21 | `, |
| 22 | ]; |
| 23 | |
| 24 | render() { |
| 25 | return html` |
| 26 | <span class="formfield"> |
| 27 | <slot></slot> |
| 28 | <label @click="${this._labelClick}"> |
| 29 | <slot name="label"></slot> |
| 30 | </label> |
| 31 | </span> |
| 32 | `; |
| 33 | } |
| 34 | |
| 35 | _labelClick() { |
| 36 | const input = this._input; |
| 37 | if (!input || !this.shadowRoot) return; |
| 38 | |
| 39 | input.focus(); |
| 40 | input.click(); |
| 41 | } |
| 42 | |
| 43 | get _input() { |
| 44 | return this._slottedChildren?.[0] ?? null; |
| 45 | } |
| 46 | |
| 47 | get _slottedChildren() { |
| 48 | if (!this.shadowRoot) return null; |
| 49 | const slot = this.shadowRoot.querySelector('slot'); |
| 50 | return slot.assignedElements({flatten: true}); |
| 51 | } |
| 52 | } |
| 53 | window.customElements.define('twpt-form-field', TwptFormField); |