Skip to content

Commit 6748d32

Browse files
authored
Merge pull request #102 from husseinmansour6/multiple-checkboxes
Added multiple checkboxes component
2 parents 5f34be1 + bac35e1 commit 6748d32

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

example/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { View, Text, StatusBar, SafeAreaView, Pressable } from "react-native";
33
import AppleHeader from "react-native-apple-header";
44
import BottomSearchBar from "react-native-bottom-search-bar";
55
import BouncyCheckbox from "react-native-bouncy-checkbox";
6+
import MultipleCheckboxes from "./multipleCheckboxes/multiple-checkboxes.component";
67

78
export default class App extends React.Component {
89
render() {
@@ -12,6 +13,7 @@ export default class App extends React.Component {
1213
<SafeAreaView style={{ flex: 1 }}>
1314
<AppleHeader />
1415
<View style={{ margin: 16 }}>
16+
<MultipleCheckboxes />
1517
<View style={{ margin: 8 }}>
1618
<BouncyCheckbox
1719
size={25}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import React, { useEffect, useState } from "react";
2+
import { SafeAreaView, FlatList, Text, View } from "react-native";
3+
import BouncyCheckbox from "react-native-bouncy-checkbox";
4+
5+
type ICheckboxComponent = {
6+
id: string;
7+
title: string;
8+
};
9+
10+
type IItem = {
11+
item: ICheckboxComponent;
12+
};
13+
14+
const generateToggleState = (
15+
array: ICheckboxComponent[],
16+
key: string,
17+
value: boolean,
18+
) => {
19+
if (!array) return [];
20+
const initialValue = {};
21+
return array.reduce((obj, item) => {
22+
// @ts-ignore
23+
return item[key]
24+
? {
25+
...obj,
26+
// @ts-ignore
27+
[item[key].replace(/\s/, "")]: value,
28+
}
29+
: obj;
30+
}, initialValue);
31+
};
32+
33+
const MultipleCheckboxes = () => {
34+
const itemList = [
35+
{ id: "checkbox-1", title: "First checkbox" },
36+
{ id: "checkbox-2", title: "Second checkbox" },
37+
{ id: "checkbox-3", title: "Third checkbox" },
38+
{ id: "checkbox-4", title: "Forth checkbox" },
39+
];
40+
41+
const [toggleCheckbox, setToggleCheckbox] = useState<any>({});
42+
43+
const handleToggleState = (item: ICheckboxComponent) =>
44+
setToggleCheckbox({
45+
...toggleCheckbox,
46+
[item.id]: !toggleCheckbox[item.id],
47+
});
48+
49+
useEffect(() => {
50+
setToggleCheckbox(generateToggleState(itemList, "id", false));
51+
}, []);
52+
53+
const CheckboxComponent = ({ item }: IItem) => {
54+
const { id, title } = item;
55+
return (
56+
<BouncyCheckbox
57+
disableBuiltInState
58+
fillColor="red"
59+
iconStyle={{ borderColor: "red" }}
60+
isChecked={toggleCheckbox[id]}
61+
key={id}
62+
onPress={() => handleToggleState({ id, title })}
63+
style={{ paddingBottom: 10 }}
64+
text={title}
65+
textStyle={{ textDecorationLine: "none" }}
66+
/>
67+
);
68+
};
69+
70+
const renderItem = ({ item }: IItem) => <CheckboxComponent item={item} />;
71+
72+
return (
73+
<View>
74+
<Text>Toggle multiple checkboxes</Text>
75+
<SafeAreaView
76+
style={{
77+
marginTop: 10,
78+
height: 140,
79+
}}
80+
>
81+
<FlatList
82+
data={itemList}
83+
renderItem={renderItem}
84+
keyExtractor={(item) => item.id}
85+
style={{ padding: 2 }}
86+
/>
87+
</SafeAreaView>
88+
</View>
89+
);
90+
};
91+
92+
export default MultipleCheckboxes;

0 commit comments

Comments
 (0)