forked from mantinedev/mantine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checkbox.tsx
135 lines (116 loc) · 3.44 KB
/
Checkbox.tsx
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, { forwardRef } from 'react';
import {
DefaultProps,
MantineSize,
MantineColor,
Selectors,
extractSystemStyles,
MantineNumberSize,
useComponentDefaultProps,
} from '@mantine/styles';
import { ForwardRefWithStaticComponents } from '@mantine/utils';
import { useId } from '@mantine/hooks';
import { Box } from '../Box';
import { CheckboxIcon } from './CheckboxIcon';
import { CheckboxGroup } from './CheckboxGroup/CheckboxGroup';
import { useCheckboxGroupContext } from './CheckboxGroup.context';
import useStyles, { CheckboxStylesParams } from './Checkbox.styles';
export type CheckboxStylesNames = Selectors<typeof useStyles>;
export interface CheckboxProps
extends DefaultProps<CheckboxStylesNames, CheckboxStylesParams>,
Omit<React.ComponentPropsWithoutRef<'input'>, 'type' | 'size'> {
/** Key of theme.colors */
color?: MantineColor;
/** Key of theme.radius or number to set border-radius in px */
radius?: MantineNumberSize;
/** Predefined label font-size and checkbox width and height in px */
size?: MantineSize;
/** Checkbox label */
label?: React.ReactNode;
/** Indeterminate state of checkbox, overwrites checked */
indeterminate?: boolean;
/** Props spread to wrapper element */
wrapperProps?: Record<string, any>;
/** id to connect label with input */
id?: string;
/** Transition duration in ms */
transitionDuration?: number;
/** Icon rendered when checkbox has checked or indeterminate state */
icon?: React.FC<{ indeterminate: boolean; className: string }>;
}
const defaultProps: Partial<CheckboxProps> = {
size: 'sm',
transitionDuration: 100,
icon: CheckboxIcon,
};
type CheckboxComponent = ForwardRefWithStaticComponents<
HTMLInputElement,
CheckboxProps,
{ Group: typeof CheckboxGroup }
>;
export const Checkbox: CheckboxComponent = forwardRef<HTMLInputElement, CheckboxProps>(
(props, ref) => {
const {
className,
style,
sx,
checked,
color,
label,
indeterminate,
id,
size,
radius,
wrapperProps,
children,
classNames,
styles,
transitionDuration,
icon: Icon,
unstyled,
...others
} = useComponentDefaultProps('Checkbox', defaultProps, props);
const ctx = useCheckboxGroupContext();
const uuid = useId(id);
const { systemStyles, rest } = extractSystemStyles(others);
const { classes, cx } = useStyles(
{ size: ctx?.size || size, radius, color, transitionDuration },
{ name: 'Checkbox', classNames, styles, unstyled }
);
const contextProps = ctx
? {
checked: ctx.value.includes(rest.value as string),
onChange: ctx.onChange,
}
: {};
return (
<Box
className={cx(classes.root, className)}
style={style}
sx={sx}
{...systemStyles}
{...wrapperProps}
>
<div className={classes.inner}>
<input
id={uuid}
ref={ref}
type="checkbox"
className={classes.input}
checked={indeterminate || checked}
{...rest}
{...contextProps}
/>
<Icon indeterminate={indeterminate} className={classes.icon} />
</div>
{label && (
<label className={classes.label} htmlFor={uuid}>
{label}
</label>
)}
</Box>
);
}
) as any;
Checkbox.displayName = '@mantine/core/Checkbox';
Checkbox.Group = CheckboxGroup;