|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | +import {type ɵControlDirectiveHost as ControlDirectiveHost} from '@angular/core'; |
| 9 | +import { |
| 10 | + bindingUpdated, |
| 11 | + CONTROL_BINDING_NAMES, |
| 12 | + createBindings, |
| 13 | + readFieldStateBindingValue, |
| 14 | + type ControlBindingKey, |
| 15 | +} from './bindings'; |
| 16 | +import type {FormField} from './form_field'; |
| 17 | +import {setNativeDomProperty} from './native'; |
| 18 | +import {observeSelectMutations} from './select'; |
| 19 | + |
| 20 | +export function selectMultipleControlCreate( |
| 21 | + host: ControlDirectiveHost, |
| 22 | + parent: FormField<unknown>, |
| 23 | +): () => void { |
| 24 | + let updateMode = false; |
| 25 | + const select = parent.nativeFormElement as HTMLSelectElement; |
| 26 | + |
| 27 | + host.listenToDom('input', () => |
| 28 | + parent.state().controlValue.set(getSelectMultipleControlValue(select)), |
| 29 | + ); |
| 30 | + host.listenToDom('blur', () => parent.state().markAsTouched()); |
| 31 | + |
| 32 | + parent.registerAsBinding(); |
| 33 | + |
| 34 | + observeSelectMutations( |
| 35 | + select, |
| 36 | + () => { |
| 37 | + if (!updateMode) { |
| 38 | + return; |
| 39 | + } |
| 40 | + setSelectMultipleControlValue(select, parent.state().controlValue()); |
| 41 | + }, |
| 42 | + parent.destroyRef, |
| 43 | + ); |
| 44 | + |
| 45 | + const bindings = createBindings<ControlBindingKey | 'controlValue'>(); |
| 46 | + |
| 47 | + return () => { |
| 48 | + const state = parent.state(); |
| 49 | + const controlValue = state.controlValue(); |
| 50 | + if (bindingUpdated(bindings, 'controlValue', controlValue)) { |
| 51 | + setSelectMultipleControlValue(select, controlValue); |
| 52 | + } |
| 53 | + |
| 54 | + for (const name of CONTROL_BINDING_NAMES) { |
| 55 | + const value = readFieldStateBindingValue(state, name); |
| 56 | + if (bindingUpdated(bindings, name, value)) { |
| 57 | + host.setInputOnDirectives(name, value); |
| 58 | + if (parent.elementAcceptsNativeProperty(name)) { |
| 59 | + setNativeDomProperty(parent.renderer, select, name, value as string | number | undefined); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + updateMode = true; |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +function getSelectMultipleControlValue(select: HTMLSelectElement): string[] { |
| 69 | + const selected: string[] = []; |
| 70 | + const selectedOptions = select.selectedOptions; |
| 71 | + |
| 72 | + if (selectedOptions !== undefined) { |
| 73 | + for (let i = 0; i < selectedOptions.length; i++) { |
| 74 | + selected.push(selectedOptions[i].value); |
| 75 | + } |
| 76 | + return selected; |
| 77 | + } |
| 78 | + |
| 79 | + for (let i = 0; i < select.options.length; i++) { |
| 80 | + const option = select.options[i]; |
| 81 | + if (option.selected) { |
| 82 | + selected.push(option.value); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return selected; |
| 87 | +} |
| 88 | + |
| 89 | +function setSelectMultipleControlValue(select: HTMLSelectElement, value: unknown): void { |
| 90 | + const selectedValues = new Set(Array.isArray(value) ? value : []); |
| 91 | + for (let i = 0; i < select.options.length; i++) { |
| 92 | + const option = select.options[i]; |
| 93 | + option.selected = selectedValues.has(option.value); |
| 94 | + } |
| 95 | +} |
0 commit comments