|
| 1 | +import { defineElement } from '@umbraco-ui/uui-base/lib/registration'; |
| 2 | +import { UUIBooleanInputEvent } from '@umbraco-ui/uui-boolean-input/lib'; |
| 3 | +import { html, LitElement } from 'lit'; |
| 4 | +import { property } from 'lit/decorators.js'; |
| 5 | +import { repeat } from 'lit/directives/repeat.js'; |
| 6 | + |
| 7 | +import '@umbraco-ui/uui-checkbox/lib'; |
| 8 | + |
| 9 | +export type Option = { |
| 10 | + value: string; |
| 11 | + label: string; |
| 12 | +}; |
| 13 | + |
| 14 | +@defineElement('uui-checkbox-indeterminate-example') |
| 15 | +export default class UUICheckboxIndeterminateExample extends LitElement { |
| 16 | + @property() |
| 17 | + label = 'Indeterminate'; |
| 18 | + |
| 19 | + @property({ attribute: false }) |
| 20 | + parent!: Option; |
| 21 | + |
| 22 | + @property({ type: Array, attribute: false }) |
| 23 | + options!: Option[]; |
| 24 | + |
| 25 | + @property({ type: Array }) |
| 26 | + values: string[] = []; |
| 27 | + |
| 28 | + private _handleParentChange(e: Event) { |
| 29 | + e.stopPropagation(); |
| 30 | + const parent = e.target as HTMLInputElement; |
| 31 | + let values: string[] = []; |
| 32 | + if (parent.checked) { |
| 33 | + values = this.options.map(option => option.value); |
| 34 | + } |
| 35 | + this.values = values; |
| 36 | + this.dispatchEvent(new UUIBooleanInputEvent(UUIBooleanInputEvent.CHANGE)); |
| 37 | + } |
| 38 | + |
| 39 | + private _handleOptionChange(e: Event) { |
| 40 | + e.stopPropagation(); |
| 41 | + const option = e.target as HTMLInputElement; |
| 42 | + let values = this.values; |
| 43 | + if (option.checked) { |
| 44 | + values = values.concat(option.value); |
| 45 | + } else { |
| 46 | + values = values.filter(v => v !== option.value); |
| 47 | + } |
| 48 | + this.values = values; |
| 49 | + this.dispatchEvent(new UUIBooleanInputEvent(UUIBooleanInputEvent.CHANGE)); |
| 50 | + } |
| 51 | + |
| 52 | + render() { |
| 53 | + return html` |
| 54 | + <fieldset name="Indeterminate" style="border: none;"> |
| 55 | + <legend>${this.label}</legend> |
| 56 | + <uui-checkbox |
| 57 | + value=${this.parent.value} |
| 58 | + label=${this.parent.label} |
| 59 | + @change=${this._handleParentChange} |
| 60 | + name="indeterminate-parent" |
| 61 | + ?indeterminate=${this.values.length > 0 && |
| 62 | + this.values.length < this.options.length} |
| 63 | + ?checked=${this.values.length === this.options.length}></uui-checkbox> |
| 64 | + <ul style="list-style: none; margin: 0;"> |
| 65 | + ${repeat( |
| 66 | + this.options, |
| 67 | + option => option.value, |
| 68 | + option => |
| 69 | + html` <li> |
| 70 | + <uui-checkbox |
| 71 | + value=${option.value} |
| 72 | + label=${option.label} |
| 73 | + @change=${this._handleOptionChange} |
| 74 | + name="indeterminate-child" |
| 75 | + ?checked=${this.values.includes(option.value)}></uui-checkbox> |
| 76 | + </li>`, |
| 77 | + )} |
| 78 | + </ul> |
| 79 | + </fieldset> |
| 80 | + `; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +declare global { |
| 85 | + interface HTMLElementTagNameMap { |
| 86 | + 'uui-checkbox-indeterminate-example': UUICheckboxIndeterminateExample; |
| 87 | + } |
| 88 | +} |
0 commit comments