Skip to content

Commit 9a5eb76

Browse files
chore: improve radio, select and checkbox components
1 parent 938e4d6 commit 9a5eb76

7 files changed

Lines changed: 135 additions & 172 deletions

File tree

src/components/molecules/Field/Radio.tsx

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@ import {
22
FormErrorMessage,
33
FormLabel,
44
Box,
5-
Touchable,
6-
Text,
75
TouchableRef,
6+
RadioButton,
87
} from '@baca/design-system/components'
9-
import { forwardRef, useCallback, useMemo } from 'react'
8+
import { forwardRef, useMemo } from 'react'
109

1110
import { FieldRadioProps } from './types'
1211

13-
const radioSizes = {
14-
sm: {
15-
boxSize: 4,
16-
},
17-
md: {
18-
boxSize: 5,
19-
},
20-
} as const
21-
2212
export const Radio = forwardRef<TouchableRef, FieldRadioProps>(
2313
(
2414
{
@@ -27,60 +17,32 @@ export const Radio = forwardRef<TouchableRef, FieldRadioProps>(
2717
radioOptions,
2818
errorMessage,
2919
isError,
30-
size = 'sm',
20+
size = 'md',
3121
onChange,
3222
label,
3323
labelStyle,
3424
isDisabled,
3525
},
3626
ref
3727
) => {
38-
const checkboxSize = useMemo(() => radioSizes[size], [size])
39-
40-
const getBorderColor = useCallback(
41-
(isSelected: boolean): ColorNames | undefined => {
42-
if (isDisabled) {
43-
return 'border.disabled'
44-
}
45-
if (isError) {
46-
return 'border.error'
47-
}
48-
49-
if (isSelected) {
50-
return 'bg.brand.solid'
51-
}
52-
53-
return 'border.primary'
54-
},
55-
[isDisabled, isError]
56-
)
57-
5828
const renderRadioButtons = useMemo(
5929
() =>
6030
radioOptions?.map((item: string, index: number) => {
6131
return (
62-
<Touchable
32+
<RadioButton
6333
ref={ref}
6434
key={index}
65-
onPress={() => onChange(item)}
66-
alignItems="center"
67-
flexDirection="row"
68-
height={8}
69-
>
70-
<Box
71-
alignItems="center"
72-
borderRadius={50}
73-
height={checkboxSize.boxSize}
74-
width={checkboxSize.boxSize}
75-
justifyContent="center"
76-
borderColor={getBorderColor(item === value)}
77-
borderWidth={item === value ? 5 : 1}
78-
/>
79-
<Text ml={4}>{item}</Text>
80-
</Touchable>
35+
isDisabled={isDisabled}
36+
isError={isError}
37+
isSelected={item === value}
38+
onChange={() => onChange(item)}
39+
pb={2}
40+
label={item}
41+
size={size}
42+
/>
8143
)
8244
}),
83-
[radioOptions, ref, checkboxSize.boxSize, getBorderColor, value, onChange]
45+
[radioOptions, ref, isDisabled, isError, value, size, onChange]
8446
)
8547

8648
return (

src/components/molecules/Field/Select.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const Select = <T extends SelectKey>({
5858
<Box {...layoutProps} width="100%" mb={2}>
5959
<Pressable>
6060
<FormLabel label={label} isRequired={isRequired} labelStyle={labelStyle} />
61-
<CustomSelect {...props} />
61+
<CustomSelect label={label} {...props} />
6262
<FormErrorMessage errorMessage={errorMessage} />
6363
</Pressable>
6464
</Box>

src/components/molecules/Field/types.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
CheckboxProps,
3-
FormLabelProps,
4-
InputProps,
5-
RadioProps,
6-
SelectProps,
7-
} from '@baca/design-system'
1+
import { CheckboxProps, FormLabelProps, InputProps, SelectProps } from '@baca/design-system'
82

93
export type FieldInputProps = InputProps &
104
FormLabelProps & {
@@ -14,14 +8,18 @@ export type FieldInputProps = InputProps &
148
onFocus?: () => void
159
}
1610

17-
export type FieldRadioProps = RadioProps &
18-
FormLabelProps & {
19-
radioOptions?: string[]
20-
errorMessage?: string
21-
isInvalid?: boolean
22-
isDisabled?: boolean
23-
name: string
24-
}
11+
export type FieldRadioProps = FormLabelProps & {
12+
radioOptions?: string[]
13+
errorMessage?: string
14+
isInvalid?: boolean
15+
isDisabled?: boolean
16+
name: string
17+
onChange: (val: string) => void
18+
label?: string
19+
isError?: boolean
20+
value?: string | number
21+
size?: 'sm' | 'md'
22+
}
2523

2624
export type FieldSelectProps<T> = SelectProps<T> &
2725
FormLabelProps & {

src/design-system/components/Checkbox.tsx

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { forwardRef, useCallback, useMemo } from 'react'
22
import { TouchableOpacity, StyleSheet } from 'react-native'
33

4+
import { Box } from './Box'
45
import { Center } from './Center'
56
import { Icon } from './Icon'
67
import { Text } from './Text'
@@ -32,8 +33,9 @@ export const Checkbox = forwardRef<TouchableOpacity, CheckboxProps>(
3233
isChecked,
3334
isError,
3435
onChange,
35-
size = 'sm',
36+
size = 'md',
3637
value,
38+
pb,
3739
...props
3840
},
3941
ref
@@ -80,28 +82,30 @@ export const Checkbox = forwardRef<TouchableOpacity, CheckboxProps>(
8082
}, [isChecked, isError, disabled])
8183

8284
return (
83-
<TouchableOpacity
84-
{...{ disabled, hitSlop, ref }}
85-
activeOpacity={0.5}
86-
onPress={handleValueChange}
87-
style={styles.mainContainer}
88-
>
89-
<Center
90-
bg={bgColor}
91-
borderColor={borderColor}
92-
borderRadius={4}
93-
borderWidth={1}
94-
height={checkboxSize.boxSize}
95-
mr={2}
96-
width={checkboxSize.boxSize}
97-
{...{ ...props }}
85+
<Box pb={pb}>
86+
<TouchableOpacity
87+
{...{ disabled, hitSlop, ref }}
88+
activeOpacity={0.5}
89+
onPress={handleValueChange}
90+
style={styles.mainContainer}
9891
>
99-
{isChecked ? (
100-
<Icon color={iconColor} name="check-line" size={checkboxSize.iconSize} />
101-
) : null}
102-
</Center>
103-
<Text.SmRegular>{checkboxText}</Text.SmRegular>
104-
</TouchableOpacity>
92+
<Center
93+
bg={bgColor}
94+
borderColor={borderColor}
95+
borderRadius={4}
96+
borderWidth={1}
97+
height={checkboxSize.boxSize}
98+
mr={2}
99+
width={checkboxSize.boxSize}
100+
{...{ ...props }}
101+
>
102+
{isChecked ? (
103+
<Icon color={iconColor} name="check-line" size={checkboxSize.iconSize} />
104+
) : null}
105+
</Center>
106+
<Text.SmRegular>{checkboxText}</Text.SmRegular>
107+
</TouchableOpacity>
108+
</Box>
105109
)
106110
}
107111
)

0 commit comments

Comments
 (0)