Passed
Push — task/combine-skills-modal-and-... ( 3a1f45...db170f )
by Yonathan
05:32
created

resources/assets/js/components/Form/CheckboxInput.tsx   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 46
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 33
c 0
b 0
f 0
dl 0
loc 46
rs 10
mnd 3
bc 3
fnc 0
bpm 0
cpm 0
noi 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