@cdx-ui/primitives 0.0.1-alpha.5 → 0.0.1-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/button/createButtonRoot.js +3 -3
- package/lib/commonjs/button/createButtonRoot.js.map +1 -1
- package/lib/commonjs/checkbox/context.js +14 -0
- package/lib/commonjs/checkbox/context.js.map +1 -0
- package/lib/commonjs/checkbox/createCheckboxGroup.js +44 -0
- package/lib/commonjs/checkbox/createCheckboxGroup.js.map +1 -0
- package/lib/commonjs/checkbox/createCheckboxIcon.js +28 -0
- package/lib/commonjs/checkbox/createCheckboxIcon.js.map +1 -0
- package/lib/commonjs/checkbox/createCheckboxIndicator.js +47 -0
- package/lib/commonjs/checkbox/createCheckboxIndicator.js.map +1 -0
- package/lib/commonjs/checkbox/createCheckboxLabel.js +46 -0
- package/lib/commonjs/checkbox/createCheckboxLabel.js.map +1 -0
- package/lib/commonjs/checkbox/createCheckboxRoot.js +99 -0
- package/lib/commonjs/checkbox/createCheckboxRoot.js.map +1 -0
- package/lib/commonjs/checkbox/createCheckboxRoot.web.js +88 -0
- package/lib/commonjs/checkbox/createCheckboxRoot.web.js.map +1 -0
- package/lib/commonjs/checkbox/index.js +32 -0
- package/lib/commonjs/checkbox/index.js.map +1 -0
- package/lib/commonjs/checkbox/types.js +6 -0
- package/lib/commonjs/checkbox/types.js.map +1 -0
- package/lib/commonjs/checkbox/useCheckboxRoot.js +82 -0
- package/lib/commonjs/checkbox/useCheckboxRoot.js.map +1 -0
- package/lib/commonjs/index.js +13 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/types.js +2 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/button/createButtonRoot.js +3 -3
- package/lib/module/button/createButtonRoot.js.map +1 -1
- package/lib/module/checkbox/context.js +7 -0
- package/lib/module/checkbox/context.js.map +1 -0
- package/lib/module/checkbox/createCheckboxGroup.js +39 -0
- package/lib/module/checkbox/createCheckboxGroup.js.map +1 -0
- package/lib/module/checkbox/createCheckboxIcon.js +23 -0
- package/lib/module/checkbox/createCheckboxIcon.js.map +1 -0
- package/lib/module/checkbox/createCheckboxIndicator.js +42 -0
- package/lib/module/checkbox/createCheckboxIndicator.js.map +1 -0
- package/lib/module/checkbox/createCheckboxLabel.js +41 -0
- package/lib/module/checkbox/createCheckboxLabel.js.map +1 -0
- package/lib/module/checkbox/createCheckboxRoot.js +94 -0
- package/lib/module/checkbox/createCheckboxRoot.js.map +1 -0
- package/lib/module/checkbox/createCheckboxRoot.web.js +83 -0
- package/lib/module/checkbox/createCheckboxRoot.web.js.map +1 -0
- package/lib/module/checkbox/index.js +27 -0
- package/lib/module/checkbox/index.js.map +1 -0
- package/lib/module/checkbox/types.js +4 -0
- package/lib/module/checkbox/types.js.map +1 -0
- package/lib/module/checkbox/useCheckboxRoot.js +78 -0
- package/lib/module/checkbox/useCheckboxRoot.js.map +1 -0
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/button/context.d.ts +1 -1
- package/lib/typescript/button/context.d.ts.map +1 -1
- package/lib/typescript/button/createButtonRoot.d.ts +1 -1
- package/lib/typescript/button/createButtonRoot.d.ts.map +1 -1
- package/lib/typescript/button/types.d.ts +0 -7
- package/lib/typescript/button/types.d.ts.map +1 -1
- package/lib/typescript/checkbox/context.d.ts +18 -0
- package/lib/typescript/checkbox/context.d.ts.map +1 -0
- package/lib/typescript/checkbox/createCheckboxGroup.d.ts +3 -0
- package/lib/typescript/checkbox/createCheckboxGroup.d.ts.map +1 -0
- package/lib/typescript/checkbox/createCheckboxIcon.d.ts +3 -0
- package/lib/typescript/checkbox/createCheckboxIcon.d.ts.map +1 -0
- package/lib/typescript/checkbox/createCheckboxIndicator.d.ts +5 -0
- package/lib/typescript/checkbox/createCheckboxIndicator.d.ts.map +1 -0
- package/lib/typescript/checkbox/createCheckboxLabel.d.ts +5 -0
- package/lib/typescript/checkbox/createCheckboxLabel.d.ts.map +1 -0
- package/lib/typescript/checkbox/createCheckboxRoot.d.ts +2 -0
- package/lib/typescript/checkbox/createCheckboxRoot.d.ts.map +1 -0
- package/lib/typescript/checkbox/createCheckboxRoot.web.d.ts +2 -0
- package/lib/typescript/checkbox/createCheckboxRoot.web.d.ts.map +1 -0
- package/lib/typescript/checkbox/index.d.ts +10 -0
- package/lib/typescript/checkbox/index.d.ts.map +1 -0
- package/lib/typescript/checkbox/types.d.ts +57 -0
- package/lib/typescript/checkbox/types.d.ts.map +1 -0
- package/lib/typescript/checkbox/useCheckboxRoot.d.ts +147 -0
- package/lib/typescript/checkbox/useCheckboxRoot.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +3 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/select/types.d.ts +2 -7
- package/lib/typescript/select/types.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +8 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/package.json +6 -2
- package/src/button/context.tsx +1 -1
- package/src/button/createButtonRoot.tsx +5 -4
- package/src/button/types.ts +0 -8
- package/src/checkbox/context.tsx +18 -0
- package/src/checkbox/createCheckboxGroup.tsx +32 -0
- package/src/checkbox/createCheckboxIcon.tsx +18 -0
- package/src/checkbox/createCheckboxIndicator.tsx +43 -0
- package/src/checkbox/createCheckboxLabel.tsx +42 -0
- package/src/checkbox/createCheckboxRoot.tsx +100 -0
- package/src/checkbox/createCheckboxRoot.web.tsx +77 -0
- package/src/checkbox/index.ts +43 -0
- package/src/checkbox/types.ts +74 -0
- package/src/checkbox/useCheckboxRoot.ts +85 -0
- package/src/index.ts +3 -1
- package/src/select/types.ts +2 -10
- package/src/types.ts +7 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { forwardRef } from 'react';
|
|
2
|
+
import { composeEventHandlers } from '@cdx-ui/utils';
|
|
3
|
+
import { useFocus } from '@react-native-aria/focus';
|
|
4
|
+
import { usePress } from '@react-native-aria/interactions';
|
|
5
|
+
import { dataAttributes } from '../utils/dataAttributes';
|
|
6
|
+
import { CheckboxProvider } from './context';
|
|
7
|
+
import type { ICheckboxProps } from './types';
|
|
8
|
+
import { useCheckboxRoot } from './useCheckboxRoot';
|
|
9
|
+
|
|
10
|
+
export const createCheckboxRoot = <T,>(BaseCheckbox: React.ComponentType<T>) =>
|
|
11
|
+
forwardRef(
|
|
12
|
+
(
|
|
13
|
+
{
|
|
14
|
+
onPressIn,
|
|
15
|
+
onPressOut,
|
|
16
|
+
onHoverIn,
|
|
17
|
+
onHoverOut,
|
|
18
|
+
onFocus,
|
|
19
|
+
onBlur,
|
|
20
|
+
children,
|
|
21
|
+
...props
|
|
22
|
+
}: ICheckboxProps,
|
|
23
|
+
ref?: React.Ref<T>,
|
|
24
|
+
) => {
|
|
25
|
+
const {
|
|
26
|
+
isHovered: isHoveredProp,
|
|
27
|
+
isChecked: isCheckedProp,
|
|
28
|
+
isDisabled: isDisabledProp,
|
|
29
|
+
isInvalid: isInvalidProp,
|
|
30
|
+
isReadOnly: isReadOnlyProp,
|
|
31
|
+
isPressed: isPressedProp,
|
|
32
|
+
isFocused: isFocusedProp,
|
|
33
|
+
isIndeterminate: isIndeterminateProp,
|
|
34
|
+
isFocusVisible,
|
|
35
|
+
} = props;
|
|
36
|
+
|
|
37
|
+
const {
|
|
38
|
+
combinedProps,
|
|
39
|
+
isInvalid,
|
|
40
|
+
isReadOnly,
|
|
41
|
+
isIndeterminate,
|
|
42
|
+
groupItemInputProps,
|
|
43
|
+
isChecked,
|
|
44
|
+
isDisabled,
|
|
45
|
+
isHovered,
|
|
46
|
+
hoverProps,
|
|
47
|
+
mergedRef,
|
|
48
|
+
} = useCheckboxRoot(props, ref);
|
|
49
|
+
|
|
50
|
+
const { focusProps, isFocused } = useFocus();
|
|
51
|
+
|
|
52
|
+
const { pressProps, isPressed } = usePress({
|
|
53
|
+
isDisabled: isDisabled || isDisabledProp,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<BaseCheckbox
|
|
58
|
+
disabled={isDisabled || isDisabledProp}
|
|
59
|
+
{...pressProps}
|
|
60
|
+
{...(combinedProps as T)}
|
|
61
|
+
{...groupItemInputProps}
|
|
62
|
+
ref={mergedRef}
|
|
63
|
+
role="checkbox"
|
|
64
|
+
onPressIn={composeEventHandlers(onPressIn, pressProps.onPressIn)}
|
|
65
|
+
onPressOut={composeEventHandlers(onPressOut, pressProps.onPressOut)}
|
|
66
|
+
onHoverIn={composeEventHandlers(onHoverIn, hoverProps.onHoverIn)}
|
|
67
|
+
onHoverOut={composeEventHandlers(onHoverOut, hoverProps.onHoverOut)}
|
|
68
|
+
onFocus={composeEventHandlers(composeEventHandlers(onFocus, focusProps.onFocus))}
|
|
69
|
+
onBlur={composeEventHandlers(composeEventHandlers(onBlur, focusProps.onBlur))}
|
|
70
|
+
{...dataAttributes({
|
|
71
|
+
checked: isChecked || isCheckedProp,
|
|
72
|
+
disabled: isDisabled || isDisabledProp,
|
|
73
|
+
hover: isHovered || isHoveredProp,
|
|
74
|
+
invalid: isInvalid || isInvalidProp,
|
|
75
|
+
readonly: isReadOnly || isReadOnlyProp,
|
|
76
|
+
active: isPressed,
|
|
77
|
+
focus: isFocused,
|
|
78
|
+
indeterminate: isIndeterminate || isIndeterminateProp,
|
|
79
|
+
focusVisible: isFocusVisible,
|
|
80
|
+
})}
|
|
81
|
+
>
|
|
82
|
+
<CheckboxProvider
|
|
83
|
+
value={{
|
|
84
|
+
isChecked: isChecked || isCheckedProp,
|
|
85
|
+
isDisabled: isDisabled || isDisabledProp,
|
|
86
|
+
isHovered: isHovered || isHoveredProp,
|
|
87
|
+
isInvalid: isInvalid || isInvalidProp,
|
|
88
|
+
isReadOnly: isReadOnly || isReadOnlyProp,
|
|
89
|
+
isPressed: isPressed || isPressedProp,
|
|
90
|
+
isFocused: isFocused || isFocusedProp,
|
|
91
|
+
isIndeterminate: isIndeterminate || isIndeterminateProp,
|
|
92
|
+
isFocusVisible,
|
|
93
|
+
}}
|
|
94
|
+
>
|
|
95
|
+
{children}
|
|
96
|
+
</CheckboxProvider>
|
|
97
|
+
</BaseCheckbox>
|
|
98
|
+
);
|
|
99
|
+
},
|
|
100
|
+
);
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { forwardRef } from 'react';
|
|
2
|
+
import { VisuallyHidden } from '@react-aria/visually-hidden';
|
|
3
|
+
import { useFocusRing } from '@react-native-aria/focus';
|
|
4
|
+
import { dataAttributes } from '../utils/dataAttributes';
|
|
5
|
+
import { CheckboxProvider } from './context';
|
|
6
|
+
import type { ICheckboxProps } from './types';
|
|
7
|
+
import { useCheckboxRoot } from './useCheckboxRoot';
|
|
8
|
+
|
|
9
|
+
export const createCheckboxRoot = <T,>(BaseCheckbox: React.ComponentType<T>) =>
|
|
10
|
+
forwardRef(({ children, ...props }: ICheckboxProps, ref?: React.Ref<T>) => {
|
|
11
|
+
const {
|
|
12
|
+
isHovered: isHoveredProp,
|
|
13
|
+
isFocusVisible: isFocusVisibleProp,
|
|
14
|
+
isChecked: isCheckedProp,
|
|
15
|
+
isDisabled: isDisabledProp,
|
|
16
|
+
isInvalid: isInvalidProp,
|
|
17
|
+
isReadOnly: isReadOnlyProp,
|
|
18
|
+
isIndeterminate: isIndeterminateProp,
|
|
19
|
+
isFocused,
|
|
20
|
+
isPressed,
|
|
21
|
+
} = props;
|
|
22
|
+
|
|
23
|
+
const {
|
|
24
|
+
combinedProps,
|
|
25
|
+
isInvalid,
|
|
26
|
+
isReadOnly,
|
|
27
|
+
isIndeterminate,
|
|
28
|
+
groupItemInputProps,
|
|
29
|
+
isChecked,
|
|
30
|
+
isDisabled,
|
|
31
|
+
isHovered,
|
|
32
|
+
mergedRef,
|
|
33
|
+
inputRef,
|
|
34
|
+
} = useCheckboxRoot(props, ref, { useInputRefForAria: true });
|
|
35
|
+
|
|
36
|
+
const { focusProps, isFocusVisible } = useFocusRing();
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<BaseCheckbox
|
|
40
|
+
{...(combinedProps as T)}
|
|
41
|
+
ref={mergedRef}
|
|
42
|
+
role="label"
|
|
43
|
+
// eslint-disable-next-line react-native-a11y/has-valid-accessibility-role
|
|
44
|
+
accessibilityRole="label"
|
|
45
|
+
{...dataAttributes({
|
|
46
|
+
checked: isChecked || isCheckedProp,
|
|
47
|
+
disabled: isDisabled || isDisabledProp,
|
|
48
|
+
hover: isHovered || isHoveredProp,
|
|
49
|
+
invalid: isInvalid || isInvalidProp,
|
|
50
|
+
readonly: isReadOnly || isReadOnlyProp,
|
|
51
|
+
active: isPressed,
|
|
52
|
+
focus: isFocused,
|
|
53
|
+
indeterminate: isIndeterminate || isIndeterminateProp,
|
|
54
|
+
focusVisible: isFocusVisible,
|
|
55
|
+
})}
|
|
56
|
+
>
|
|
57
|
+
<CheckboxProvider
|
|
58
|
+
value={{
|
|
59
|
+
isChecked: isChecked || isCheckedProp,
|
|
60
|
+
isDisabled: isDisabled || isDisabledProp,
|
|
61
|
+
isFocusVisible: isFocusVisible || isFocusVisibleProp,
|
|
62
|
+
isHovered: isHovered || isHoveredProp,
|
|
63
|
+
isInvalid: isInvalid || isInvalidProp,
|
|
64
|
+
isReadOnly: isReadOnly || isReadOnlyProp,
|
|
65
|
+
isIndeterminate: isIndeterminate || isIndeterminateProp,
|
|
66
|
+
isPressed,
|
|
67
|
+
isFocused,
|
|
68
|
+
}}
|
|
69
|
+
>
|
|
70
|
+
<VisuallyHidden>
|
|
71
|
+
<input {...groupItemInputProps} {...focusProps} ref={inputRef} />
|
|
72
|
+
</VisuallyHidden>
|
|
73
|
+
{children}
|
|
74
|
+
</CheckboxProvider>
|
|
75
|
+
</BaseCheckbox>
|
|
76
|
+
);
|
|
77
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createCheckboxGroup } from './createCheckboxGroup';
|
|
2
|
+
import { createCheckboxIcon } from './createCheckboxIcon';
|
|
3
|
+
import { createCheckboxIndicator } from './createCheckboxIndicator';
|
|
4
|
+
import { createCheckboxLabel } from './createCheckboxLabel';
|
|
5
|
+
import { createCheckboxRoot } from './createCheckboxRoot';
|
|
6
|
+
import type { ICheckboxComponentType } from './types';
|
|
7
|
+
|
|
8
|
+
export type {
|
|
9
|
+
ICheckboxComponentType,
|
|
10
|
+
ICheckboxGroupProps,
|
|
11
|
+
ICheckboxIconProps,
|
|
12
|
+
ICheckboxIndicatorProps,
|
|
13
|
+
ICheckboxLabelProps,
|
|
14
|
+
ICheckboxProps,
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
export const createCheckbox = <Root, Indicator, Icon, Label, Group>({
|
|
18
|
+
Root,
|
|
19
|
+
Indicator,
|
|
20
|
+
Icon,
|
|
21
|
+
Label,
|
|
22
|
+
Group,
|
|
23
|
+
}: {
|
|
24
|
+
Root: React.ComponentType<Root>;
|
|
25
|
+
Indicator: React.ComponentType<Indicator>;
|
|
26
|
+
Icon: React.ComponentType<Icon>;
|
|
27
|
+
Label: React.ComponentType<Label>;
|
|
28
|
+
Group: React.ComponentType<Group>;
|
|
29
|
+
}) => {
|
|
30
|
+
const Checkbox = createCheckboxRoot(Root) as any;
|
|
31
|
+
Checkbox.Indicator = createCheckboxIndicator(Indicator);
|
|
32
|
+
Checkbox.Icon = createCheckboxIcon(Icon);
|
|
33
|
+
Checkbox.Label = createCheckboxLabel(Label);
|
|
34
|
+
Checkbox.Group = createCheckboxGroup(Group);
|
|
35
|
+
|
|
36
|
+
Checkbox.displayName = 'Checkbox';
|
|
37
|
+
Checkbox.Indicator.displayName = 'Checkbox.Indicator';
|
|
38
|
+
Checkbox.Icon.displayName = 'Checkbox.Icon';
|
|
39
|
+
Checkbox.Label.displayName = 'Checkbox.Label';
|
|
40
|
+
Checkbox.Group.displayName = 'Checkbox.Group';
|
|
41
|
+
|
|
42
|
+
return Checkbox as ICheckboxComponentType<Root, Indicator, Icon, Label, Group>;
|
|
43
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { PressableProps } from 'react-native';
|
|
2
|
+
export interface InterfaceCheckbox extends PressableProps {
|
|
3
|
+
value: string;
|
|
4
|
+
onChange?: (isSelected: boolean) => void;
|
|
5
|
+
children?: React.ReactNode;
|
|
6
|
+
defaultIsChecked?: boolean;
|
|
7
|
+
isChecked?: boolean;
|
|
8
|
+
isDisabled?: boolean;
|
|
9
|
+
isInvalid?: boolean;
|
|
10
|
+
isReadOnly?: boolean;
|
|
11
|
+
isHovered?: boolean;
|
|
12
|
+
isFocused?: boolean;
|
|
13
|
+
isPressed?: boolean;
|
|
14
|
+
isRequired?: boolean;
|
|
15
|
+
isIndeterminate?: boolean;
|
|
16
|
+
isFocusVisible?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ICheckboxGroupProps {
|
|
20
|
+
value: string[];
|
|
21
|
+
onChange?: (values: string[]) => void;
|
|
22
|
+
children?: React.ReactNode;
|
|
23
|
+
isDisabled?: boolean;
|
|
24
|
+
isInvalid?: boolean;
|
|
25
|
+
isReadOnly?: boolean;
|
|
26
|
+
isRequired?: boolean;
|
|
27
|
+
'aria-label'?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type ICheckboxComponentType<Root, Indicator, Icon, Label, Group> =
|
|
31
|
+
React.ForwardRefExoticComponent<
|
|
32
|
+
React.RefAttributes<Root> & React.PropsWithoutRef<Root> & InterfaceCheckbox
|
|
33
|
+
> & {
|
|
34
|
+
Indicator: React.ForwardRefExoticComponent<
|
|
35
|
+
React.RefAttributes<Indicator> & React.PropsWithoutRef<Indicator> & ICheckboxIndicatorProps
|
|
36
|
+
>;
|
|
37
|
+
Icon: React.ForwardRefExoticComponent<
|
|
38
|
+
React.RefAttributes<Icon> & React.PropsWithoutRef<Icon> & ICheckboxIconProps
|
|
39
|
+
>;
|
|
40
|
+
Label: React.ForwardRefExoticComponent<
|
|
41
|
+
React.RefAttributes<Label> & React.PropsWithoutRef<Label> & ICheckboxLabelProps
|
|
42
|
+
>;
|
|
43
|
+
Group: React.ForwardRefExoticComponent<
|
|
44
|
+
React.RefAttributes<Group> & React.PropsWithoutRef<Group> & ICheckboxGroupProps
|
|
45
|
+
>;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export interface ICheckboxIndicatorProps {
|
|
49
|
+
children?: React.ReactNode;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ICheckboxIconProps {
|
|
53
|
+
children?: React.ReactNode;
|
|
54
|
+
/** Render the icon even when unchecked */
|
|
55
|
+
forceMount?: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface ICheckboxLabelProps {
|
|
59
|
+
children?: React.ReactNode;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type ICheckboxProps = InterfaceCheckbox;
|
|
63
|
+
|
|
64
|
+
export interface ICheckboxContextValue {
|
|
65
|
+
isChecked?: boolean;
|
|
66
|
+
isDisabled?: boolean;
|
|
67
|
+
isHovered?: boolean;
|
|
68
|
+
isInvalid?: boolean;
|
|
69
|
+
isReadOnly?: boolean;
|
|
70
|
+
isPressed?: boolean;
|
|
71
|
+
isFocused?: boolean;
|
|
72
|
+
isIndeterminate?: boolean;
|
|
73
|
+
isFocusVisible?: boolean;
|
|
74
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { useContext, useRef } from 'react';
|
|
2
|
+
import { mergeRefs, useFormControlContext } from '@cdx-ui/utils';
|
|
3
|
+
import { useCheckbox } from '@react-native-aria/checkbox';
|
|
4
|
+
import { useHover } from '@react-native-aria/interactions';
|
|
5
|
+
import { useToggleState } from '@react-stately/toggle';
|
|
6
|
+
import { CheckboxGroupContext } from './context';
|
|
7
|
+
import type { ICheckboxProps } from './types';
|
|
8
|
+
|
|
9
|
+
interface UseCheckboxRootOptions {
|
|
10
|
+
useInputRefForAria?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function useCheckboxRoot(
|
|
14
|
+
props: ICheckboxProps,
|
|
15
|
+
ref?: React.Ref<unknown>,
|
|
16
|
+
{ useInputRefForAria = false }: UseCheckboxRootOptions = {},
|
|
17
|
+
) {
|
|
18
|
+
const formControlContext = useFormControlContext();
|
|
19
|
+
|
|
20
|
+
const { isInvalid, isReadOnly, isIndeterminate, ...combinedProps } = {
|
|
21
|
+
...formControlContext,
|
|
22
|
+
...props,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const checkboxGroupContext = useContext(CheckboxGroupContext);
|
|
26
|
+
|
|
27
|
+
const state = useToggleState({
|
|
28
|
+
...combinedProps,
|
|
29
|
+
defaultSelected: props.defaultIsChecked,
|
|
30
|
+
isSelected: props.isChecked,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const rootRef = useRef(null);
|
|
34
|
+
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
35
|
+
const mergedRootRef = mergeRefs(ref as any, rootRef as any);
|
|
36
|
+
const ariaLabel = combinedProps['aria-label'] || combinedProps.value || 'Checkbox';
|
|
37
|
+
|
|
38
|
+
const groupToggleState = useToggleState(
|
|
39
|
+
checkboxGroupContext
|
|
40
|
+
? {
|
|
41
|
+
isReadOnly: isReadOnly || checkboxGroupContext.state.isReadOnly,
|
|
42
|
+
isSelected: checkboxGroupContext.state.isSelected(combinedProps.value),
|
|
43
|
+
onChange(isSelected: boolean) {
|
|
44
|
+
if (isSelected) {
|
|
45
|
+
checkboxGroupContext.state.addValue(combinedProps.value);
|
|
46
|
+
} else {
|
|
47
|
+
checkboxGroupContext.state.removeValue(combinedProps.value);
|
|
48
|
+
}
|
|
49
|
+
combinedProps.onChange?.(isSelected);
|
|
50
|
+
},
|
|
51
|
+
}
|
|
52
|
+
: { isSelected: false },
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
const { inputProps: groupItemInputProps } = useCheckbox(
|
|
56
|
+
{
|
|
57
|
+
...combinedProps,
|
|
58
|
+
'aria-label': ariaLabel,
|
|
59
|
+
...(checkboxGroupContext && {
|
|
60
|
+
isReadOnly: isReadOnly || checkboxGroupContext.state.isReadOnly,
|
|
61
|
+
isDisabled: combinedProps.isDisabled || checkboxGroupContext.state.isDisabled,
|
|
62
|
+
}),
|
|
63
|
+
} as any,
|
|
64
|
+
checkboxGroupContext ? groupToggleState : state,
|
|
65
|
+
(useInputRefForAria ? inputRef : rootRef) as any,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const { checked: isChecked, disabled: isDisabled } = groupItemInputProps;
|
|
69
|
+
|
|
70
|
+
const { hoverProps, isHovered } = useHover({}, rootRef);
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
combinedProps,
|
|
74
|
+
isInvalid,
|
|
75
|
+
isReadOnly,
|
|
76
|
+
isIndeterminate,
|
|
77
|
+
groupItemInputProps,
|
|
78
|
+
isChecked,
|
|
79
|
+
isDisabled,
|
|
80
|
+
isHovered,
|
|
81
|
+
hoverProps,
|
|
82
|
+
mergedRef: mergedRootRef,
|
|
83
|
+
inputRef,
|
|
84
|
+
};
|
|
85
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export * from './button';
|
|
2
|
+
export * from './checkbox';
|
|
2
3
|
export * from './input';
|
|
4
|
+
export { type EdgeInsets, OverlayInsetsProvider } from './overlay';
|
|
3
5
|
export * from './select';
|
|
4
|
-
export {
|
|
6
|
+
export type { InteractionState } from './types';
|
package/src/select/types.ts
CHANGED
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
import type { PropsWithoutRef, ReactNode, RefAttributes, RefObject } from 'react';
|
|
2
2
|
import type { PressableProps, ViewProps } from 'react-native';
|
|
3
3
|
import type { EntryOrExitLayoutType } from 'react-native-reanimated';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export interface InteractionState {
|
|
8
|
-
hover: boolean;
|
|
9
|
-
focus: boolean;
|
|
10
|
-
active: boolean;
|
|
11
|
-
disabled: boolean;
|
|
12
|
-
focusVisible: boolean;
|
|
13
|
-
}
|
|
4
|
+
import type { InteractionState } from '../types';
|
|
5
|
+
export type { InteractionState };
|
|
14
6
|
|
|
15
7
|
export interface SelectItemInteractionState extends InteractionState {
|
|
16
8
|
highlighted: boolean;
|