Skip to content

Commit f5d751c

Browse files
committed
Add custom icon for checkbox component
(cherry picked from commit 407ba80) (cherry picked from commit 1341418)
1 parent 2d48520 commit f5d751c

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
@@ -99,6 +99,10 @@ export type Props = {
9999
* testID to be used on tests.
100100
*/
101101
testID?: string;
102+
/**
103+
* custom icon.
104+
*/
105+
icon?: (props: { size: number; color: string }) => JSX.Element;
102106
/**
103107
* Checkbox control position.
104108
*/
@@ -142,6 +146,7 @@ const CheckboxItem = ({
142146
labelStyle,
143147
theme: themeOverrides,
144148
testID,
149+
icon,
145150
mode,
146151
position = 'trailing',
147152
accessibilityLabel = label,
@@ -154,7 +159,7 @@ const CheckboxItem = ({
154159
...props
155160
}: Props) => {
156161
const theme = useInternalTheme(themeOverrides);
157-
const checkboxProps = { ...props, status, theme, disabled };
162+
const checkboxProps = { ...props, status, theme, disabled, icon };
158163
const isLeading = position === 'leading';
159164
let checkbox;
160165

0 commit comments

Comments
 (0)