Skip to content

Commit b27e33d

Browse files
feat: create radio button
1 parent 5ce0c42 commit b27e33d

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { forwardRef, useCallback, useMemo } from 'react'
2+
3+
import { Box } from './Box'
4+
import { Text } from './Text'
5+
import { Touchable, TouchableRef } from './Touchables'
6+
import { RadioButtonProps } from './types'
7+
8+
const radioSizes = {
9+
sm: {
10+
boxSize: 4,
11+
},
12+
md: {
13+
boxSize: 5,
14+
},
15+
} as const
16+
17+
export const RadioButton = forwardRef<TouchableRef, RadioButtonProps>(
18+
({ isSelected, isDisabled, isError, onChange, label, size = 'md', pb }, ref) => {
19+
const checkboxSize = useMemo(() => radioSizes[size], [size])
20+
21+
const getBorderColor = useCallback(
22+
(isSelected?: boolean): ColorNames | undefined => {
23+
if (isDisabled) {
24+
return 'border.disabled'
25+
}
26+
if (isError) {
27+
return 'border.error'
28+
}
29+
30+
if (isSelected) {
31+
return 'bg.brand.solid'
32+
}
33+
34+
return 'border.primary'
35+
},
36+
[isDisabled, isError]
37+
)
38+
39+
return (
40+
<Touchable
41+
ref={ref}
42+
onPress={() => onChange(!isSelected)}
43+
alignItems="center"
44+
flexDirection="row"
45+
pb={pb}
46+
>
47+
<Box
48+
alignItems="center"
49+
borderRadius={50}
50+
height={checkboxSize.boxSize}
51+
width={checkboxSize.boxSize}
52+
justifyContent="center"
53+
borderColor={getBorderColor(isSelected)}
54+
borderWidth={isSelected ? 5 : 1}
55+
/>
56+
<Text ml={4}>{label}</Text>
57+
</Touchable>
58+
)
59+
}
60+
)

0 commit comments

Comments
 (0)