Passed
Push — feature/checkbox-group-field ( e7fdbd )
by Tristan
07:41 queued 01:19
created

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

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 56
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 42
mnd 1
bc 1
fnc 0
dl 0
loc 56
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { Field, useField } from "formik";
2
import React from "react";
3
import { FormattedMessage } from "react-intl";
4
import { inputMessages } from "./Messages";
5
6
interface CheckboxGroupFieldProps {
7
  groupLabel: string;
8
  name: string;
9
  allBoxes: {
10
    value: string | number;
11
    label: string;
12
  }[];
13
  /** data-clone grid sizing value to be applied to individual boxes; see https://designwithclone.ca/#flexbox-grid */
14
  grid?: string;
15
  required?: boolean;
16
}
17
18
export const CheckboxGroupField: React.FC<CheckboxGroupFieldProps> = ({
19
  groupLabel,
20
  grid,
21
  name,
22
  allBoxes,
23
  required,
24
}) => {
25
  const [field, meta] = useField(name);
26
  return (
27
    <div
28
      data-c-input="checkbox(group)"
29
      data-c-required={required || null}
30
      data-c-invalid={meta.touched && meta.error ? true : null}
31
      role="group"
32
      aria-labelledby="checkbox-group"
33
    >
34
      <label>{groupLabel}</label>
35
      <span>
36
        <FormattedMessage {...inputMessages.required} />
37
      </span>
38
      <div data-c-grid>
39
        {allBoxes.map((box) => {
40
          return (
41
            <div key={box.value} data-c-grid-item={grid}>
42
              <label>
43
                <Field type="checkbox" name={name} value={box.value} />
44
                <span>{box.label}</span>
45
              </label>
46
            </div>
47
          );
48
        })}
49
      </div>
50
      <span>{meta.error}</span>
51
    </div>
52
  );
53
};
54
55
export default CheckboxGroupField;
56