-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathcheckbox.tsx
65 lines (58 loc) · 2.11 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
'use client';
import * as React from 'react';
import classNames from 'classnames';
import { Checkbox as CheckboxPrimitive } from 'radix-ui';
import { useControllableState } from 'radix-ui/internal';
import { checkboxPropDefs } from './checkbox.props.js';
import { ThickCheckIcon, ThickDividerHorizontalIcon } from './icons.js';
import { extractProps } from '../helpers/extract-props.js';
import { marginPropDefs } from '../props/margin.props.js';
import type { MarginProps } from '../props/margin.props.js';
import type { ComponentPropsWithout } from '../helpers/component-props.js';
import type { GetPropDefTypes } from '../props/prop-def.js';
type CheckboxElement = React.ElementRef<typeof CheckboxPrimitive.Root>;
type CheckboxOwnProps = GetPropDefTypes<typeof checkboxPropDefs>;
interface CheckboxProps
extends ComponentPropsWithout<
typeof CheckboxPrimitive.Root,
'asChild' | 'color' | 'defaultValue' | 'children'
>,
MarginProps,
CheckboxOwnProps {}
const Checkbox = React.forwardRef<CheckboxElement, CheckboxProps>((props, forwardedRef) => {
const {
className,
color,
checked: checkedProp,
defaultChecked: defaultCheckedProp,
onCheckedChange,
...checkboxProps
} = extractProps(props, checkboxPropDefs, marginPropDefs);
const [checked, setChecked] = useControllableState({
prop: checkedProp,
defaultProp: defaultCheckedProp,
onChange: onCheckedChange,
});
return (
<CheckboxPrimitive.Root
data-accent-color={color}
{...checkboxProps}
defaultChecked={defaultCheckedProp}
checked={checked}
onCheckedChange={setChecked}
asChild={false}
ref={forwardedRef}
className={classNames('rt-reset', 'rt-BaseCheckboxRoot', 'rt-CheckboxRoot', className)}
>
<CheckboxPrimitive.Indicator
asChild
className="rt-BaseCheckboxIndicator rt-CheckboxIndicator"
>
{checked === 'indeterminate' ? <ThickDividerHorizontalIcon /> : <ThickCheckIcon />}
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
);
});
Checkbox.displayName = 'Checkbox';
export { Checkbox };
export type { CheckboxProps };