@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
120 lines (115 loc) • 5.11 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
import { k as a11yHint, H as watchJsonArrayString, s as setState } from './common-Cx_AGO_M.js';
import { h as hasEnoughReadableChars, c as containsOnlyNumbers } from './label-Fyi30H9j.js';
import { v as validateRequired } from './required-DFzRSWky.js';
import { v as validateOrientation } from './orientation-CjNmUJp-.js';
import { I as InputController } from './FormFieldStateWrapper-BWTMRKXT.js';
const validateInputSelectOptions = (option) => {
if (typeof option === 'object' && option !== null) {
if (typeof option.label === 'string' && option.label.length > 0) {
option.disabled = option.disabled === true;
option.label = `${option.label}`.trim();
if (hasEnoughReadableChars(option.label, 3) === false && containsOnlyNumbers(option.label) === false) {
a11yHint(`A differing Aria-Label (${option.label}) is inaccessible. A differing Aria-Label should consist of at least three readable characters.`);
}
if (Array.isArray(option.options)) {
return (option.options.find((item) => {
return validateInputSelectOptions(item) === false;
}) === undefined);
}
return true;
}
else if (typeof option.label === 'number') {
return true;
}
}
return false;
};
const validateOptions = (component, value, options = {}) => {
watchJsonArrayString(component, '_options', (item) => typeof item === 'object' && item !== null && typeof item.label === 'string' && item.label.length > 0, value, undefined, options);
};
const validateOptionsWithOptgroup = (component, value, options = {}) => {
watchJsonArrayString(component, '_options', validateInputSelectOptions, value, undefined, options);
};
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);
}
}
});
};
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);
}
}
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);
}
}
export { InputCheckboxRadioController as I, InputRadioController as a, validateOptions as b, fillKeyOptionMap as f, validateOptionsWithOptgroup as v };
//# sourceMappingURL=controller-DKPaX3Tu.js.map
//# sourceMappingURL=controller-DKPaX3Tu.js.map