|
| 1 | +import { |
| 2 | + customElement, |
| 3 | + html, |
| 4 | + LitElement, |
| 5 | + property, |
| 6 | + query, |
| 7 | + state, |
| 8 | + TemplateResult, |
| 9 | +} from 'lit-element'; |
| 10 | + |
| 11 | +import '@material/mwc-formfield'; |
| 12 | +import '@material/mwc-switch'; |
| 13 | +import '@material/mwc-checkbox'; |
| 14 | +import { Checkbox } from '@material/mwc-checkbox'; |
| 15 | +import { Switch } from '@material/mwc-switch'; |
| 16 | + |
| 17 | +/** A potentially `nullable` labelled checkbox. */ |
| 18 | +@customElement('wizard-checkbox') |
| 19 | +export class WizardCheckbox extends LitElement { |
| 20 | + @property({ type: String }) |
| 21 | + label = ''; |
| 22 | + /** Parenthetical information rendered after the label: `label (helper)` */ |
| 23 | + @property({ type: String }) |
| 24 | + helper = ''; |
| 25 | + /** Whether [[`maybeValue`]] may be `null` */ |
| 26 | + @property({ type: Boolean }) |
| 27 | + nullable = false; |
| 28 | + /** The default `checked` state while [[`maybeValue`]] is `null`. */ |
| 29 | + @property({ type: Boolean }) |
| 30 | + defaultChecked = false; |
| 31 | + /** Is `"true"` when checked, `"false"` un-checked, `null` if [[`nullable`]]. */ |
| 32 | + @property({ type: String }) |
| 33 | + get maybeValue(): string | null { |
| 34 | + return this.null ? null : this.checked ? 'true' : 'false'; |
| 35 | + } |
| 36 | + set maybeValue(check: string | null) { |
| 37 | + if (check === null) this.null = true; |
| 38 | + else { |
| 39 | + this.null = false; |
| 40 | + this.checked = check === 'true' ? true : false; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private isNull = false; |
| 45 | + |
| 46 | + @state() |
| 47 | + private get null(): boolean { |
| 48 | + return this.nullable && this.isNull; |
| 49 | + } |
| 50 | + private set null(value: boolean) { |
| 51 | + if (!this.nullable || value === this.isNull) return; |
| 52 | + this.isNull = value; |
| 53 | + if (this.null) this.disable(); |
| 54 | + else this.enable(); |
| 55 | + } |
| 56 | + |
| 57 | + private initChecked = false; |
| 58 | + |
| 59 | + @state() |
| 60 | + get checked(): boolean { |
| 61 | + return this.checkbox?.checked ?? this.initChecked; |
| 62 | + } |
| 63 | + set checked(value: boolean) { |
| 64 | + if (this.checkbox) this.checkbox.checked = value; |
| 65 | + else this.initChecked = value; |
| 66 | + } |
| 67 | + |
| 68 | + @state() |
| 69 | + disabled = false; |
| 70 | + @state() |
| 71 | + get formfieldLabel(): string { |
| 72 | + return this.helper ? `${this.label} (${this.helper})` : this.label; |
| 73 | + } |
| 74 | + |
| 75 | + @query('mwc-switch') nullSwitch?: Switch; |
| 76 | + @query('mwc-checkbox') checkbox?: Checkbox; |
| 77 | + |
| 78 | + private nulled: boolean | null = null; |
| 79 | + |
| 80 | + private enable(): void { |
| 81 | + if (this.nulled === null) return; |
| 82 | + this.checked = this.nulled; |
| 83 | + this.nulled = null; |
| 84 | + this.disabled = false; |
| 85 | + } |
| 86 | + |
| 87 | + private disable(): void { |
| 88 | + if (this.nulled !== null) return; |
| 89 | + this.nulled = this.checked; |
| 90 | + this.checked = this.defaultChecked; |
| 91 | + this.disabled = true; |
| 92 | + } |
| 93 | + |
| 94 | + firstUpdated(): void { |
| 95 | + this.requestUpdate(); |
| 96 | + } |
| 97 | + |
| 98 | + renderSwitch(): TemplateResult { |
| 99 | + if (this.nullable) { |
| 100 | + return html`<mwc-switch |
| 101 | + style="margin-left: 12px;" |
| 102 | + ?checked=${!this.null} |
| 103 | + @change=${() => { |
| 104 | + this.null = !this.nullSwitch!.checked; |
| 105 | + }} |
| 106 | + ></mwc-switch>`; |
| 107 | + } |
| 108 | + return html``; |
| 109 | + } |
| 110 | + |
| 111 | + render(): TemplateResult { |
| 112 | + return html` |
| 113 | + <div style="display: flex; flex-direction: row;"> |
| 114 | + <div style="flex: auto;"> |
| 115 | + <mwc-formfield |
| 116 | + label="${this.formfieldLabel}" |
| 117 | + style="${this.disabled |
| 118 | + ? `--mdc-theme-text-primary-on-background:rgba(0, 0, 0, 0.38)` |
| 119 | + : ``}" |
| 120 | + ><mwc-checkbox |
| 121 | + ?checked=${this.initChecked} |
| 122 | + ?disabled=${this.disabled} |
| 123 | + ></mwc-checkbox |
| 124 | + ></mwc-formfield> |
| 125 | + </div> |
| 126 | + <div style="display: flex; align-items: center;"> |
| 127 | + ${this.renderSwitch()} |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + `; |
| 131 | + } |
| 132 | +} |
0 commit comments