UNPKG

@public-ui/components

Version:

Contains all web components that belong to KoliBri - The accessible HTML-Standard.

85 lines (84 loc) 3.34 kB
/*! * KoliBri - The accessible HTML-Standard */ import { setState, validateOptions, validateRequired } from "../../schema"; import { validateOrientation } from "../../schema/props/orientation"; import { InputController } from "../@deprecated/input/controller"; export const fillKeyOptionMap = (keyOptionMap, options, preKey = '') => { options.forEach((option, index) => { const key = `${preKey}-${index}`; if (typeof option === 'object' && option !== null && typeof option.label === 'string' && option.label.length > 0) { if (Array.isArray(option.options)) { fillKeyOptionMap(keyOptionMap, option.options, key); } else { keyOptionMap.set(key, option); } } }); }; export class InputCheckboxRadioController extends InputController { constructor(component, name, host) { super(component, name, host); this.component = component; } validateRequired(value) { validateRequired(this.component, value); } componentWillLoad() { super.componentWillLoad(); this.validateRequired(this.component._required); } } export class InputRadioController extends InputCheckboxRadioController { constructor(component, name, host) { super(component, name, host); this.keyOptionMap = new Map(); this.getOptionByKey = (key) => this.keyOptionMap.get(key); this.afterPatchOptions = (value, _state, _component, key) => { if (key === '_value') { this.setFormAssociatedValue(value); } }; this.beforePatchOptions = (_value, nextState) => { const options = nextState.has('_options') ? nextState.get('_options') : this.component.state._options; if (Array.isArray(options) && options.length > 0) { this.keyOptionMap.clear(); const normalizedOptions = options.map((option) => { var _a; if (typeof option === 'object' && option !== null && typeof option.label === 'string') { return Object.assign(Object.assign({}, option), { value: (_a = option.value) !== null && _a !== void 0 ? _a : option.label }); } return option; }); fillKeyOptionMap(this.keyOptionMap, normalizedOptions); } }; this.component = component; } validateOrientation(value) { validateOrientation(this.component, value, 'vertical'); } validateOptions(value) { validateOptions(this.component, value, { hooks: { afterPatch: this.afterPatchOptions, beforePatch: this.beforePatchOptions, }, }); } validateValue(value) { value = Array.isArray(value) ? value[0] : value; setState(this.component, '_value', value, { afterPatch: this.afterPatchOptions, beforePatch: this.beforePatchOptions, }); } componentWillLoad() { super.componentWillLoad(); this.validateOrientation(this.component._orientation); this.validateOptions(this.component._options); this.validateValue(this.component._value); } } //# sourceMappingURL=controller.js.map