|
| 1 | +import { BaseInputsList } from '../../partials'; |
| 2 | + |
| 3 | +export enum CheckboxesListFieldAction { |
| 4 | + Check = 'check', |
| 5 | + Uncheck = 'uncheck', |
| 6 | +} |
| 7 | + |
| 8 | +export class CheckboxesListField extends BaseInputsList<string[]> { |
| 9 | + private _itemsContainer: HTMLDivElement; |
| 10 | + |
| 11 | + static EVENTS = { |
| 12 | + ...BaseInputsList.EVENTS, |
| 13 | + CHANGE: 'ids:checkboxes-list-field:change', |
| 14 | + }; |
| 15 | + |
| 16 | + constructor(container: HTMLDivElement) { |
| 17 | + super(container); |
| 18 | + |
| 19 | + const itemsContainer = container.querySelector<HTMLDivElement>('.ids-choice-inputs-list__items'); |
| 20 | + |
| 21 | + if (!itemsContainer) { |
| 22 | + throw new Error('CheckboxesListField: Required elements are missing in the container.'); |
| 23 | + } |
| 24 | + |
| 25 | + this._itemsContainer = itemsContainer; |
| 26 | + |
| 27 | + this.onItemChange = this.onItemChange.bind(this); |
| 28 | + } |
| 29 | + |
| 30 | + getItemsCheckboxes() { |
| 31 | + const itemsCheckboxes = [ |
| 32 | + ...this._itemsContainer.querySelectorAll<HTMLInputElement>('.ids-choice-input-field .ids-input--checkbox'), |
| 33 | + ]; |
| 34 | + |
| 35 | + return itemsCheckboxes; |
| 36 | + } |
| 37 | + |
| 38 | + getValue(): string[] { |
| 39 | + const itemsCheckboxes = this.getItemsCheckboxes(); |
| 40 | + const checkedValues = itemsCheckboxes.reduce((acc: string[], checkbox) => { |
| 41 | + if (checkbox.checked) { |
| 42 | + acc.push(checkbox.value); |
| 43 | + } |
| 44 | + |
| 45 | + return acc; |
| 46 | + }, []); |
| 47 | + |
| 48 | + return checkedValues; |
| 49 | + } |
| 50 | + |
| 51 | + onItemChange(event: Event) { |
| 52 | + if (!(event.target instanceof HTMLInputElement)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const item = event.target; |
| 57 | + const nextValue = this.getValue(); |
| 58 | + const actionPerformed = item.checked ? CheckboxesListFieldAction.Check : CheckboxesListFieldAction.Uncheck; |
| 59 | + |
| 60 | + this.onChange(nextValue, item.value, actionPerformed); |
| 61 | + } |
| 62 | + |
| 63 | + onChange(nextValue: string[], itemValue: string, actionPerformed: CheckboxesListFieldAction) { |
| 64 | + const changeEvent = new CustomEvent(CheckboxesListField.EVENTS.CHANGE, { |
| 65 | + bubbles: true, |
| 66 | + detail: [nextValue, itemValue, actionPerformed], |
| 67 | + }); |
| 68 | + |
| 69 | + this._container.dispatchEvent(changeEvent); |
| 70 | + } |
| 71 | + |
| 72 | + initCheckboxes() { |
| 73 | + const itemsCheckboxes = this.getItemsCheckboxes(); |
| 74 | + |
| 75 | + itemsCheckboxes.forEach((checkbox) => { |
| 76 | + checkbox.addEventListener('change', this.onItemChange, false); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + unbindCheckboxes() { |
| 81 | + const itemsCheckboxes = this.getItemsCheckboxes(); |
| 82 | + |
| 83 | + itemsCheckboxes.forEach((checkbox) => { |
| 84 | + checkbox.removeEventListener('change', this.onItemChange, false); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + reinit() { |
| 89 | + super.reinit(); |
| 90 | + |
| 91 | + this.unbindCheckboxes(); |
| 92 | + this.initCheckboxes(); |
| 93 | + } |
| 94 | + |
| 95 | + init() { |
| 96 | + super.init(); |
| 97 | + |
| 98 | + this.initCheckboxes(); |
| 99 | + } |
| 100 | +} |
0 commit comments