Passed
Pull Request — main (#353)
by Alejandro
41:20 queued 16:16
created

src/utils/BooleanControl.tsx   A

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 37
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
eloc 30
mnd 1
bc 1
fnc 0
dl 0
loc 37
ccs 6
cts 6
cp 1
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { ChangeEvent, FC, useRef } from 'react';
2
import classNames from 'classnames';
3
import { v4 as uuid } from 'uuid';
4
import { identity } from 'ramda';
5
6
export interface BooleanControlProps {
7
  checked?: boolean;
8
  onChange?: (checked: boolean, e: ChangeEvent<HTMLInputElement>) => void;
9
  className?: string;
10
  inline?: boolean;
11
}
12
13
interface BooleanControlWithTypeProps extends BooleanControlProps {
14
  type: 'switch' | 'checkbox';
15
}
16
17 6
const BooleanControl: FC<BooleanControlWithTypeProps> = (
18
  { checked = false, onChange = identity, className, children, type, inline = false },
19
) => {
20 11
  const { current: id } = useRef(uuid());
21 11
  const onChecked = (e: ChangeEvent<HTMLInputElement>) => onChange(e.target.checked, e);
22 11
  const typeClasses = {
23
    'custom-switch': type === 'switch',
24
    'custom-checkbox': type === 'checkbox',
25
  };
26 11
  const style = inline ? { display: 'inline-block' } : {};
27
28 11
  return (
29
    <span className={classNames('custom-control', typeClasses, className)} style={style}>
30
      <input type="checkbox" className="custom-control-input" id={id} checked={checked} onChange={onChecked} />
31
      <label className="custom-control-label" htmlFor={id}>{children}</label>
32
    </span>
33
  );
34
};
35
36
export default BooleanControl;
37