Download react-hook-form/src/logic/getCheckboxValue.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/logic/getCheckboxValue.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/logic/getCheckboxValue.ts
-
curl -L -o getCheckboxValue.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-hook-form/src/logic/getCheckboxValue.ts
1.04 kB
| 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; | |
| }; | |