Skip to content

Commit 407ba80

Browse files
committed
Add custom icon for checkbox component
1 parent af8d9cd commit 407ba80

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

src/components/Checkbox/Checkbox.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export type Props = {
3535
* testID to be used on tests.
3636
*/
3737
testID?: string;
38+
/**
39+
* custom icon.
40+
*/
41+
icon?: (props: { size: number; color: string }) => JSX.Element;
3842
};
3943

4044
/**

src/components/Checkbox/CheckboxAndroid.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export type Props = $RemoveChildren<typeof TouchableRipple> & {
4141
* testID to be used on tests.
4242
*/
4343
testID?: string;
44+
/**
45+
* custom icon.
46+
*/
47+
icon?: (props: { size: number; color: string }) => JSX.Element;
4448
};
4549

4650
// From https://material.io/design/motion/speed.html#duration
@@ -59,6 +63,7 @@ const CheckboxAndroid = ({
5963
disabled,
6064
onPress,
6165
testID,
66+
icon: customIcon,
6267
...rest
6368
}: Props) => {
6469
const theme = useInternalTheme(themeOverrides);
@@ -134,13 +139,15 @@ const CheckboxAndroid = ({
134139
theme={theme}
135140
>
136141
<Animated.View style={{ transform: [{ scale: scaleAnim }] }}>
137-
<MaterialCommunityIcon
138-
allowFontScaling={false}
139-
name={icon}
140-
size={24}
141-
color={selectionControlColor}
142-
direction="ltr"
143-
/>
142+
{customIcon?.({ size: 24, color: selectionControlColor }) || (
143+
<MaterialCommunityIcon
144+
allowFontScaling={false}
145+
name={icon}
146+
size={24}
147+
color={selectionControlColor}
148+
direction="ltr"
149+
/>
150+
)}
144151
<View style={[StyleSheet.absoluteFill, styles.fillContainer]}>
145152
<Animated.View
146153
style={[

src/components/Checkbox/CheckboxIOS.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export type Props = $RemoveChildren<typeof TouchableRipple> & {
3232
* testID to be used on tests.
3333
*/
3434
testID?: string;
35+
/**
36+
* custom icon.
37+
*/
38+
icon?: (props: { size: number; color: string }) => JSX.Element;
3539
};
3640

3741
/**
@@ -47,6 +51,7 @@ const CheckboxIOS = ({
4751
onPress,
4852
theme: themeOverrides,
4953
testID,
54+
icon: customIcon,
5055
...rest
5156
}: Props) => {
5257
const theme = useInternalTheme(themeOverrides);
@@ -77,13 +82,15 @@ const CheckboxIOS = ({
7782
theme={theme}
7883
>
7984
<View style={{ opacity }}>
80-
<MaterialCommunityIcon
81-
allowFontScaling={false}
82-
name={icon}
83-
size={24}
84-
color={checkedColor}
85-
direction="ltr"
86-
/>
85+
{customIcon?.({ size: 24, color: checkedColor }) || (
86+
<MaterialCommunityIcon
87+
allowFontScaling={false}
88+
name={icon}
89+
size={24}
90+
color={checkedColor}
91+
direction="ltr"
92+
/>
93+
)}
8794
</View>
8895
</TouchableRipple>
8996
);

src/components/Checkbox/CheckboxItem.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export type Props = {
9797
* testID to be used on tests.
9898
*/
9999
testID?: string;
100+
/**
101+
* custom icon.
102+
*/
103+
icon?: (props: { size: number; color: string }) => JSX.Element;
100104
/**
101105
* Checkbox control position.
102106
*/
@@ -136,6 +140,7 @@ const CheckboxItem = ({
136140
labelStyle,
137141
theme: themeOverrides,
138142
testID,
143+
icon,
139144
mode,
140145
position = 'trailing',
141146
accessibilityLabel = label,
@@ -147,7 +152,7 @@ const CheckboxItem = ({
147152
...props
148153
}: Props) => {
149154
const theme = useInternalTheme(themeOverrides);
150-
const checkboxProps = { ...props, status, theme, disabled };
155+
const checkboxProps = { ...props, status, theme, disabled, icon };
151156
const isLeading = position === 'leading';
152157
let checkbox;
153158

0 commit comments

Comments
 (0)