blob: aa32732ca7d07259f1b1093e4a7f8e00829ddd06 [file] [log] [blame]
Renovate botaa5fb8e2024-02-25 18:10:09 +00001import {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 */
7export 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}
53window.customElements.define('twpt-form-field', TwptFormField);