Repository navigation
Expand file tree
/
Copy pathCheckbox.tsx
More file actions
58 lines (51 loc) · 1.67 KB
/
Copy pathCheckbox.tsx
File metadata and controls
58 lines (51 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { Icon } from '@iconify/react';
import classNames from 'classnames';
import React from 'react';
import styles from './Checkbox.module.scss';
export type PrimitiveCheckboxProps = Omit<
React.InputHTMLAttributes<HTMLInputElement>,
'checked' | 'defaultChecked' | 'type'
> & {
className?: string;
boxClassName?: string;
};
/**
* Controlled props: requires `checked`, forbids `defaultChecked`.
*/
interface ControlledCheckboxProps extends PrimitiveCheckboxProps {
checked: boolean;
defaultChecked?: never;
}
/**
* Uncontrolled props: requires `defaultChecked`, forbids `checked`.
*/
interface UncontrolledCheckboxProps extends PrimitiveCheckboxProps {
checked?: never;
defaultChecked?: boolean;
}
/**
* Union type for either controlled or uncontrolled usage.
*/
export type CheckboxProps = ControlledCheckboxProps | UncontrolledCheckboxProps;
export const Checkbox = React.forwardRef<HTMLInputElement, CheckboxProps>(
({ className, boxClassName, checked, defaultChecked, onChange, disabled, ...props }, ref) => {
const isControlled = checked !== undefined;
return (
<label className={styles.checkbox}>
<input
ref={ref}
type="checkbox"
className={classNames(styles.checkbox__input, className)}
checked={isControlled ? checked : undefined}
defaultChecked={!isControlled ? defaultChecked : undefined}
onChange={onChange}
disabled={disabled}
{...props}
/>
<div className={classNames(styles.checkbox__box, boxClassName)} />
<Icon icon="fluent:checkmark-12-filled" className={styles.check} />
</label>
);
},
);
Checkbox.displayName = 'Checkbox';