Skip to content

Commit 50c182d

Browse files
committed
Enhance components Checkbox
1 parent 8beed20 commit 50c182d

File tree

1 file changed

+55
-20
lines changed
  • packages/components/src/components/Checkbox

1 file changed

+55
-20
lines changed

packages/components/src/components/Checkbox/index.tsx

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
'use client'
2+
13
import { Box, css, Flex, Input, Text } from '@devup-ui/react'
2-
import { ComponentProps, useId } from 'react'
4+
import { ComponentProps, useId, useState } from 'react'
35

46
import { CheckIcon } from './CheckIcon'
57

@@ -20,22 +22,47 @@ export function Checkbox({
2022
children,
2123
disabled,
2224
checked,
25+
defaultChecked = false,
2326
colors,
2427
onChange,
2528
...props
2629
}: CheckboxProps) {
2730
const generateId = useId()
31+
const [innerChecked, setInnerChecked] = useState(defaultChecked)
32+
const isChecked = checked ?? innerChecked
33+
34+
const handleChange = (value: boolean) => {
35+
setInnerChecked(value)
36+
onChange?.(value)
37+
}
38+
2839
return (
29-
<Flex alignItems="center" gap="8px">
30-
<Box h="18px" pos="relative" w="fit-content">
40+
<Flex
41+
className={css({
42+
alignItems: 'center',
43+
gap: '8px',
44+
})}
45+
>
46+
<label
47+
className={css({
48+
position: 'relative',
49+
display: 'flex',
50+
alignItems: 'center',
51+
justifyContent: 'center',
52+
width: '16px',
53+
height: '16px',
54+
cursor: disabled ? 'not-allowed' : 'pointer',
55+
})}
56+
htmlFor={generateId}
57+
>
3158
<Input
3259
_active={
3360
!disabled && {
3461
bg: 'light-dark(color-mix(in srgb, var(--primary, #6159D4) 20%, #FFF 80%), color-mix(in srgb, var(--primary, #6670F9) 30%, #000 70%))',
3562
}
3663
}
3764
_checked={{
38-
bg: 'light-dark(var(--primary, #6159D4), var(--primary, #6670F9))',
65+
bg: 'var(--primary, light-dark(#6159D4, #6670F9))',
3966
border: 'none',
4067
_hover: !disabled && {
4168
bg: 'light-dark(color-mix(in srgb, var(--primary, #6159D4) 100%, #000 15%), color-mix(in srgb, var(--primary, #6670F9) 100%, #FFF 15%))',
@@ -50,25 +77,32 @@ export function Checkbox({
5077
_hover={
5178
!disabled && {
5279
bg: 'light-dark(color-mix(in srgb, var(--primary, #6159D4) 10%, #FFF 90%), color-mix(in srgb, var(--primary, #6670F9) 20%, #000 80%))',
53-
border:
54-
'1px solid light-dark(var(--primary, #6159D4), var(--primary, #6670F9))',
80+
border: '1px solid var(--primary, light-dark(#6159D4, #6670F9))',
5581
}
5682
}
57-
accentColor="light-dark(var(--primary, #6159D4), var(--primary, #6670F9))"
83+
accentColor="var(--primary, light-dark(#6159D4, #6670F9))"
5884
appearance="none"
59-
bg="light-dark(#FFF, var(--inputBg, #2E2E2E))"
60-
border="1px solid light-dark(var(--border, #E0E0E0), var(--border, #333333))"
85+
bg="var(--inputBg, light-dark(#FFF, #2E2E2E))"
86+
border="1px solid var(--border, light-dark(#E0E0E0, #333333))"
6187
borderRadius="2px"
62-
boxSize="16px"
6388
checked={checked}
89+
className={css({
90+
position: 'absolute',
91+
top: 0,
92+
left: 0,
93+
width: '100%',
94+
height: '100%',
95+
opacity: 1,
96+
zIndex: 0,
97+
pointerEvents: 'none',
98+
})}
6499
cursor={disabled ? 'not-allowed' : 'pointer'}
65100
disabled={disabled}
101+
display="block"
66102
id={generateId}
67103
m="0"
68104
onChange={
69-
disabled || !onChange
70-
? undefined
71-
: (e) => onChange(e.target.checked)
105+
disabled ? undefined : (e) => handleChange(e.target.checked)
72106
}
73107
styleOrder={1}
74108
styleVars={{
@@ -81,24 +115,25 @@ export function Checkbox({
81115
type="checkbox"
82116
{...props}
83117
/>
84-
{checked && (
118+
{isChecked && (
85119
<Box
86120
as={CheckIcon}
87121
props={{
88122
color: disabled
89123
? 'light-dark(#D6D7DE, #373737)'
90124
: 'var(--checkIcon, #FFF)',
91125
className: css({
92-
left: '50%',
93126
pointerEvents: 'none',
94-
pos: 'absolute',
95-
top: '60%',
96-
transform: 'translate(-50%, -50%)',
127+
opacity: 1,
128+
zIndex: 1,
97129
}),
98130
}}
131+
styleVars={{
132+
checkIcon: colors?.checkIcon,
133+
}}
99134
/>
100135
)}
101-
</Box>
136+
</label>
102137

103138
<label
104139
className={css({
@@ -111,7 +146,7 @@ export function Checkbox({
111146
color={
112147
disabled
113148
? 'light-dark(#D6D7DE, #373737)'
114-
: 'light-dark(var(--text, #2F2F2F), var(--text, #EDEDED))'
149+
: 'var(--text, light-dark(#2F2F2F, #EDEDED))'
115150
}
116151
fontSize="14px"
117152
userSelect="none"

0 commit comments

Comments
 (0)