Total Complexity | 1 |
Complexity/F | 0 |
Lines of Code | 37 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 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 |