1
1
import { useMemo } from 'react' ;
2
- import useIsFocused from '../../hooks/useIsFocused ' ;
3
- import useIsHovered from '../../hooks/useIsHovered ' ;
4
- import useIsPressed from '../../hooks/useIsPressed ' ;
5
- import { useComponentPropsResolver } from '../../../hooks ' ;
6
- import type { ICheckboxProps } from './types' ;
7
- import type { IIconProps } from '../Icon /types' ;
8
- import type { IBoxProps } from '../Box/types ' ;
9
- import type { ITextProps } from '../Text/types ' ;
2
+ import { useIsFocused , useIsHovered , useIsPressed } from '../../../hooks ' ;
3
+ import { useComponentPropsResolver } from '../../../../hooks ' ;
4
+ import type { ICheckboxProps } from '../types ' ;
5
+ import type { IIconProps } from '../../Icon/types ' ;
6
+ import type { IBoxProps } from '../../Box /types' ;
7
+ import type { ITextProps } from '../../Text /types' ;
8
+ import useCheckboxStateFromGroup from './useCheckboxStateFromGroup ' ;
9
+ import useCheckboxIcon from './useCheckboxIcon ' ;
10
10
11
11
interface IUseCheckboxPropsResolverReturnType {
12
+ icon : JSX . Element ,
12
13
iconProps ?: Omit < IIconProps , 'name' > ,
13
14
iconContainerProps ?: Omit < IBoxProps , 'children' > ,
14
15
labelProps ?: Omit < ITextProps , 'children' > ,
15
16
containerProps : Omit < ICheckboxProps , '__icon' >
16
17
}
17
18
18
- export const useCheckboxPropsResolver = ( {
19
- selected = false ,
19
+ const useCheckboxPropsResolver = ( {
20
+ value,
21
+ checkedIcon,
22
+ uncheckedIcon,
23
+ indeterminatedIcon,
24
+ selected : selectedProp = false ,
20
25
indeterminated = false ,
26
+ onPress : onPressProp ,
21
27
...props
22
28
} : ICheckboxProps
23
29
) : IUseCheckboxPropsResolverReturnType => {
@@ -26,6 +32,10 @@ export const useCheckboxPropsResolver = ( {
26
32
const { isHovered, onHoverIn, onHoverOut } = useIsHovered ( props ) ;
27
33
const { isFocused, onFocus, onBlur } = useIsFocused ( props ) ;
28
34
35
+ const groupState = useCheckboxStateFromGroup ( value ) ;
36
+ const selected = groupState ?. selected || selectedProp ;
37
+ const onPress = groupState ?. onPress || onPressProp ;
38
+
29
39
const state = useMemo ( ( ) => ( {
30
40
isSelected : selected ,
31
41
isIndeterminated : indeterminated ,
@@ -42,7 +52,15 @@ export const useCheckboxPropsResolver = ( {
42
52
...containerProps
43
53
} = useComponentPropsResolver ( 'Checkbox' , props , state ) as ICheckboxProps ;
44
54
45
- containerProps . onPress = props . onPress ;
55
+ const icon = useCheckboxIcon ( {
56
+ selected,
57
+ indeterminated,
58
+ checkedIcon,
59
+ uncheckedIcon,
60
+ indeterminatedIcon
61
+ } ) ;
62
+
63
+ containerProps . onPress = onPress ;
46
64
containerProps . onPressIn = onPressIn ;
47
65
containerProps . onPressOut = onPressOut ;
48
66
containerProps . onHoverIn = onHoverIn ;
@@ -51,6 +69,12 @@ export const useCheckboxPropsResolver = ( {
51
69
containerProps . onBlur = onBlur ;
52
70
53
71
return {
54
- iconProps, iconContainerProps, labelProps, containerProps
72
+ icon,
73
+ iconProps,
74
+ iconContainerProps,
75
+ labelProps,
76
+ containerProps
55
77
} ;
56
78
} ;
79
+
80
+ export default useCheckboxPropsResolver ;
0 commit comments