Total Complexity | 3 |
Complexity/F | 0 |
Lines of Code | 46 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import * as React from "react"; |
||
2 | import { FieldProps } from "formik"; |
||
3 | import Checkbox, { CheckboxProps } from "../Checkbox"; |
||
4 | |||
5 | interface CheckboxInputProps |
||
6 | extends Exclude<CheckboxProps, "name" | "value" | "onChange" | "onBlur"> { |
||
7 | /** Formik field prop of the shape { name, value, onChange, onBlur } */ |
||
8 | field: FieldProps["field"]; |
||
9 | /** Formik form prop of the shape { errors } */ |
||
10 | form: FieldProps["form"]; |
||
11 | } |
||
12 | |||
13 | const CheckboxInput: React.FunctionComponent<CheckboxInputProps> = ({ |
||
14 | id, |
||
15 | label, |
||
16 | grid, |
||
17 | required, |
||
18 | field: { name, value, onChange, onBlur }, |
||
19 | form: { errors, touched }, |
||
20 | ...props |
||
21 | }): React.ReactElement => { |
||
22 | const specificError = errors ? errors[name] : null; |
||
23 | const errorText = specificError ? specificError.toString() : undefined; |
||
24 | const invalid = touched[name] && errors[name] ? true : null; |
||
25 | // Workaround for new TS error https://github.com/microsoft/TypeScript/issues/37559 |
||
26 | const { name: passedName, onChange: passedChange, ...otherProps } = props; |
||
27 | return ( |
||
28 | <Checkbox |
||
29 | id={id} |
||
30 | name={name} |
||
31 | label={label} |
||
32 | value={value} |
||
33 | grid={grid} |
||
34 | checked={value} |
||
35 | required={required} |
||
36 | errorText={errorText} |
||
37 | invalid={invalid} |
||
38 | onChange={onChange} |
||
39 | onBlur={onBlur} |
||
40 | {...otherProps} |
||
41 | /> |
||
42 | ); |
||
43 | }; |
||
44 | |||
45 | export default CheckboxInput; |
||
46 |