Skip to content

Commit 37821c3

Browse files
committed
new: Fundamental library is here and tested on example project
1 parent 36e79e1 commit 37821c3

File tree

7 files changed

+103
-134
lines changed

7 files changed

+103
-134
lines changed

lib/BouncyCheckboxGroup.style.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ViewStyle, StyleSheet } from "react-native";
2+
3+
interface Style {
4+
container: ViewStyle;
5+
}
6+
7+
export default StyleSheet.create<Style>({
8+
container: {
9+
flexDirection: "row",
10+
},
11+
});

lib/BouncyCheckboxGroup.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as React from "react";
2+
import { View, StyleProp, ViewStyle } from "react-native";
3+
import BouncyCheckbox, {
4+
IBouncyCheckboxProps,
5+
} from "react-native-bouncy-checkbox";
6+
/**
7+
* ? Local Imports
8+
*/
9+
import styles from "./BouncyCheckboxGroup.style";
10+
import useStateWithCallback from "./helpers/useStateWithCallback";
11+
12+
type CustomStyleProp = StyleProp<ViewStyle> | Array<StyleProp<ViewStyle>>;
13+
14+
export interface ICheckboxButton extends IBouncyCheckboxProps {
15+
id: number;
16+
}
17+
18+
interface IBouncyCheckboxGroupProps {
19+
style?: CustomStyleProp;
20+
initial?: number;
21+
data: ICheckboxButton[];
22+
onChange: (selectedItem: ICheckboxButton) => void;
23+
}
24+
25+
const BouncyCheckboxGroup: React.FC<IBouncyCheckboxGroupProps> = ({
26+
style,
27+
initial,
28+
data,
29+
onChange,
30+
}) => {
31+
const [selectedItem, setSelectedItem] = useStateWithCallback<
32+
ICheckboxButton | undefined
33+
>(undefined);
34+
35+
const handleItemPress = (item: ICheckboxButton) => {
36+
setSelectedItem(item, (newItem) => onChange && onChange(newItem));
37+
};
38+
39+
return (
40+
<View style={[styles.container, style]}>
41+
{data &&
42+
data.map((item: ICheckboxButton) => {
43+
const isActive =
44+
item.id === (selectedItem ? selectedItem?.id : initial);
45+
return (
46+
<BouncyCheckbox
47+
{...item}
48+
key={item.id}
49+
disableBuiltInState
50+
isChecked={isActive}
51+
onPress={() => handleItemPress(item)}
52+
/>
53+
);
54+
})}
55+
</View>
56+
);
57+
};
58+
59+
export default BouncyCheckboxGroup;

lib/FunctionalComponent/Size.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/FunctionalComponent/Text.tsx

Lines changed: 0 additions & 40 deletions
This file was deleted.

lib/StateFulComponent/Hello.tsx

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {useRef, useState, useEffect, useCallback, SetStateAction} from 'react';
2+
3+
type Callback<T> = (value?: any) => void;
4+
type DispatchWithCallback<T> = (value: any, callback?: Callback<any>) => void;
5+
6+
function useStateWithCallback<T>(
7+
initialState: any | (() => any),
8+
): [any, DispatchWithCallback<SetStateAction<any>>] {
9+
const [state, _setState] = useState(initialState);
10+
11+
const callbackRef = useRef<Callback<any>>();
12+
const isFirstCallbackCall = useRef<boolean>(true);
13+
14+
const setState = useCallback(
15+
(setStateAction: SetStateAction<any>, callback?: Callback<any>): void => {
16+
callbackRef.current = callback;
17+
_setState(setStateAction);
18+
},
19+
[],
20+
);
21+
22+
useEffect(() => {
23+
if (isFirstCallbackCall.current) {
24+
isFirstCallbackCall.current = false;
25+
return;
26+
}
27+
callbackRef.current?.(state);
28+
}, [state]);
29+
30+
return [state, setState];
31+
}
32+
33+
export default useStateWithCallback;

lib/index.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)