@@ -2,16 +2,15 @@ import React from 'react';
2
2
import { createCheckbox } from '@gluestack-ui/checkbox' ;
3
3
import { View , Pressable , Text } from 'react-native' ;
4
4
import {
5
- cn ,
6
5
withStates ,
7
6
withStyleContextAndStates ,
8
7
useStyleContext ,
9
8
tva ,
10
9
withStyleContext ,
11
10
cssInterop ,
11
+ VariantProps ,
12
12
} from '@gluestack-ui/nativewind-utils' ;
13
13
import { Platform } from 'react-native' ;
14
-
15
14
import { Check } from 'lucide-react-native' ;
16
15
17
16
const UICheckbox = createCheckbox ( {
@@ -20,10 +19,10 @@ const UICheckbox = createCheckbox({
20
19
Platform . OS === 'web'
21
20
? withStyleContext ( View )
22
21
: withStyleContextAndStates ( Pressable ) ,
23
- Group : Platform . OS === 'web' ? View : withStates ( View ) ,
24
- Icon : Platform . OS === 'web' ? Check : withStates ( Check ) ,
25
- Label : Platform . OS === 'web' ? Text : withStates ( Text ) ,
26
- Indicator : Platform . OS === 'web' ? View : withStates ( View ) ,
22
+ Group : withStates ( View ) ,
23
+ Icon : withStates ( Check ) ,
24
+ Label : withStates ( Text ) ,
25
+ Indicator : withStates ( View ) ,
27
26
} ) ;
28
27
29
28
cssInterop ( UICheckbox , { className : 'style' } ) ;
@@ -32,35 +31,62 @@ cssInterop(UICheckbox.Icon, { className: 'style' });
32
31
cssInterop ( UICheckbox . Label , { className : 'style' } ) ;
33
32
cssInterop ( UICheckbox . Indicator , { className : 'style' } ) ;
34
33
35
- const checkboxIndicator = tva ( {
36
- base : 'justify-center items-center border-outline-400 rounded-sm data-[focus=true]:outline-none data-[focus-visible=true]:ring-2 data-[focus-visible=true]:ring-primary-700 data-[focus-visible=true]:ring-offset-1 overflow-hidden data-[checked=true]:border-primary-600' ,
34
+ const checkboxStyle = tva ( {
35
+ base : 'group/checkbox flex-row items-center justify-start gap-2 web:cursor-pointer data-[disabled=true]:cursor-not-allowed' ,
36
+ } ) ;
37
+
38
+ const checkboxIndicatorStyle = tva ( {
39
+ base : 'group/checkboxInd justify-center items-center border-outline-400 bg-transparent rounded web:data-[focus-visible=true]:outline-none web:data-[focus-visible=true]:ring-2 web:data-[focus-visible=true]:ring-primary-700 data-[checked=true]:bg-primary-600 group-hover/checkbox:border-outline-500 group-hover/checkbox:bg-transparent group-hover/checkbox:data-[invalid=true]:border-error-700 group-hover/checkbox:data-[checked=true]:bg-primary-700 group-hover/checkbox:data-[checked=true]:data-[disabled=true]:border-primary-600 group-hover/checkbox:data-[checked=true]:data-[disabled=true]:bg-primary-600 group-hover/checkbox:data-[checked=true]:data-[disabled=true]:opacity-40 group-hover/checkbox:data-[checked=true]:data-[disabled=true]:data-[invalid=true]:border-error-700 group-hover/checkbox:data-[disabled=true]:border-outline-400 group-hover/checkbox:data-[disabled=true]:data-[invalid=true]:border-error-700 active:data-[checked=true]:bg-primary-800 active:data-[checked=true]:border-primary-800 data-[invalid=true]:border-error-700 data-[disabled=true]:opacity-40' ,
37
40
parentVariants : {
38
41
size : {
39
- lg : 'w-6 h-6 border-4 ' ,
42
+ lg : 'w-6 h-6 border-[3px] ' ,
40
43
md : 'w-5 h-5 border-2' ,
41
44
sm : 'w-4 h-4 border-2' ,
42
45
} ,
43
46
} ,
44
47
} ) ;
45
48
46
- const checkboxLabel = tva ( {
47
- base : 'text-typography-600' ,
49
+ const checkboxLabelStyle = tva ( {
50
+ base : 'text-typography-600 data-[checked=true]:text-typography-900 group-hover/checkbox:text-typography-900 group-hover/checkbox:data-[checked=true]:text-typography-900 group-hover/checkbox:data-[checked=true]:data-[disabled=true]:text-typography-900 group-hover/checkbox:data-[disabled=true]:text-typography-600 active:text-typography-900 active:data-[checked=true]:text-typography-900 data-[disabled=true]:opacity-40 web:select-none ' ,
48
51
parentVariants : {
49
52
size : {
50
53
lg : 'text-lg' ,
51
- md : 'text-md ' ,
54
+ md : 'text-base ' ,
52
55
sm : 'text-sm' ,
53
56
} ,
54
57
} ,
55
58
} ) ;
56
59
60
+ const checkboxIconStyle = tva ( {
61
+ base : 'group-data-[checked=true]/checkboxInd:text-typography-0 data-[disabled=true]:opacity-40' ,
62
+
63
+ parentVariants : {
64
+ size : {
65
+ sm : 'h-3 w-3' ,
66
+ md : 'h-4 w-4' ,
67
+ lg : 'h-5 w-5' ,
68
+ } ,
69
+ } ,
70
+ } ) ;
71
+
57
72
const CheckboxGroup = UICheckbox . Group ;
58
73
74
+ type ICheckboxProps = React . ComponentProps < typeof UICheckbox > &
75
+ VariantProps < typeof checkboxStyle > ;
59
76
const Checkbox = React . forwardRef (
60
- ( { className, size = 'sm' , ...props } : any , ref ) => {
77
+ (
78
+ {
79
+ className,
80
+ size = 'md' ,
81
+ ...props
82
+ } : { className ?: string } & ICheckboxProps ,
83
+ ref
84
+ ) => {
61
85
return (
62
86
< UICheckbox
63
- className = { cn ( 'flex-row items-center justify-start gap-2' , className ) }
87
+ className = { checkboxStyle ( {
88
+ class : className ,
89
+ } ) }
64
90
{ ...props }
65
91
context = { {
66
92
size,
@@ -71,13 +97,20 @@ const Checkbox = React.forwardRef(
71
97
}
72
98
) ;
73
99
100
+ type ICheckboxIndicatorProps = React . ComponentProps <
101
+ typeof UICheckbox . Indicator
102
+ > &
103
+ VariantProps < typeof checkboxIndicatorStyle > ;
74
104
const CheckboxIndicator = React . forwardRef (
75
- ( { className, ...props } : any , ref ) => {
105
+ (
106
+ { className, ...props } : { className ?: string } & ICheckboxIndicatorProps ,
107
+ ref
108
+ ) => {
76
109
const { size : parentSize } = useStyleContext ( ) ;
77
110
78
111
return (
79
112
< UICheckbox . Indicator
80
- className = { checkboxIndicator ( {
113
+ className = { checkboxIndicatorStyle ( {
81
114
parentVariants : {
82
115
size : parentSize ,
83
116
} ,
@@ -90,34 +123,72 @@ const CheckboxIndicator = React.forwardRef(
90
123
}
91
124
) ;
92
125
93
- const CheckboxLabel = React . forwardRef ( ( { className, ...props } : any , ref ) => {
94
- const { size : parentSize } = useStyleContext ( ) ;
95
- return (
96
- < UICheckbox . Label
97
- className = { checkboxLabel ( {
98
- parentVariants : {
99
- size : parentSize ,
100
- } ,
101
- class : className ,
102
- } ) }
103
- { ...props }
104
- ref = { ref }
105
- />
106
- ) ;
107
- } ) ;
126
+ type ICheckboxLabelProps = React . ComponentProps < typeof UICheckbox . Label > &
127
+ VariantProps < typeof checkboxLabelStyle > ;
128
+ const CheckboxLabel = React . forwardRef (
129
+ (
130
+ { className, ...props } : { className ?: string } & ICheckboxLabelProps ,
131
+ ref
132
+ ) => {
133
+ const { size : parentSize } = useStyleContext ( ) ;
134
+ return (
135
+ < UICheckbox . Label
136
+ className = { checkboxLabelStyle ( {
137
+ parentVariants : {
138
+ size : parentSize ,
139
+ } ,
140
+ class : className ,
141
+ } ) }
142
+ { ...props }
143
+ ref = { ref }
144
+ />
145
+ ) ;
146
+ }
147
+ ) ;
108
148
109
- const CheckboxIcon = React . forwardRef ( ( { className, ...props } : any , ref ) => {
110
- return (
111
- < UICheckbox . Icon
112
- className = { cn (
113
- 'w-full h-full bg-primary-600 stroke-typography-0 color-typography-0' ,
114
- className
115
- ) }
116
- { ...props }
117
- ref = { ref }
118
- />
119
- ) ;
120
- } ) ;
149
+ type ICheckboxIconProps = React . ComponentProps < typeof UICheckbox . Icon > & {
150
+ as ?: any ;
151
+ } ;
152
+ const CheckboxIcon = React . forwardRef (
153
+ (
154
+ {
155
+ className,
156
+ as : AsComp ,
157
+ ...props
158
+ } : ICheckboxIconProps & { className ?: any } ,
159
+ ref
160
+ ) => {
161
+ const { size : parentSize } = useStyleContext ( ) ;
162
+ if ( AsComp ) {
163
+ return (
164
+ < UICheckbox . Icon >
165
+ < AsComp
166
+ { ...props }
167
+ className = { checkboxIconStyle ( {
168
+ parentVariants : {
169
+ size : parentSize ,
170
+ } ,
171
+ class : className ,
172
+ } ) }
173
+ />
174
+ </ UICheckbox . Icon >
175
+ ) ;
176
+ }
177
+
178
+ return (
179
+ < UICheckbox . Icon
180
+ className = { checkboxIconStyle ( {
181
+ parentVariants : {
182
+ size : parentSize ,
183
+ } ,
184
+ class : className ,
185
+ } ) }
186
+ { ...props }
187
+ ref = { ref }
188
+ />
189
+ ) ;
190
+ }
191
+ ) ;
121
192
122
193
// Assign display names
123
194
Checkbox . displayName = 'Checkbox' ;
0 commit comments