File size: 1,042 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import isUndefined from '../utils/isUndefined';
type CheckboxFieldResult = {
isValid: boolean;
value: string | string[] | boolean | undefined;
};
const defaultResult: CheckboxFieldResult = {
value: false,
isValid: false,
};
const validResult = { value: true, isValid: true };
export default (options?: HTMLInputElement[]): CheckboxFieldResult => {
if (Array.isArray(options)) {
if (options.length > 1) {
const values = options
.filter((option) => option && option.checked && !option.disabled)
.map((option) => option.value);
return { value: values, isValid: !!values.length };
}
return options[0].checked && !options[0].disabled
? // @ts-expect-error expected to work in the browser
options[0].attributes && !isUndefined(options[0].attributes.value)
? isUndefined(options[0].value) || options[0].value === ''
? validResult
: { value: options[0].value, isValid: true }
: validResult
: defaultResult;
}
return defaultResult;
};
|