Skip to content

Commit cd3f0ff

Browse files
committed
feat: languageItemProps for proper prop drilling
1 parent c71ae4b commit cd3f0ff

18 files changed

+270
-22
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const data: ILanguagePicker[] = [
8989
| itemContainer | default | default | change the language item container style |
9090
| imageComponent | default | React.ReactNode / React.ReactNode[] | change the language image component |
9191
| checkComponent | default | React.ReactNode / React.ReactNode[] | change the check component |
92+
| languageItemProps | default | ILanguageItemProps | change the language item props |
9293

9394
## Future Plans
9495

example/App.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const App = () => {
4242
alignItems: 'center',
4343
paddingTop: 50,
4444
backgroundColor: '#EFEFEF',
45-
}}>
45+
}}
46+
>
4647
<Text
4748
style={{
4849
textAlign: 'center',
@@ -51,7 +52,8 @@ const App = () => {
5152
color: '#454A62',
5253
marginTop: 32,
5354
marginBottom: 30,
54-
}}>
55+
}}
56+
>
5557
Language
5658
</Text>
5759
<Text
@@ -62,13 +64,16 @@ const App = () => {
6264
fontSize: 13,
6365
color: '#2F3452',
6466
marginBottom: 40,
65-
}}>
67+
}}
68+
>
6669
You can choose the language and costumise your application in the
6770
language you want
6871
</Text>
6972
<LanguagePicker
7073
initialIndex={1}
71-
activeBorderColor="red"
74+
languageItemProps={{
75+
activeBorderColor: 'red',
76+
}}
7277
data={data}
7378
onSelect={(selectedItem: ILanguagePicker) => {
7479
console.log(selectedItem);

example/lib/LanguagePicker.style.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Dimensions, StyleSheet } from "react-native";
2+
3+
const windowWidth = Dimensions.get("window").width;
4+
const windowHeight = Dimensions.get("window").height;
5+
6+
export const _container = (width: number, height: number) => ({
7+
width: width,
8+
height: height,
9+
});
10+
11+
export default StyleSheet.create({});

example/lib/LanguagePicker.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React, {useState} from 'react';
2+
import {Dimensions, FlatList, ViewStyle} from 'react-native';
3+
/**
4+
* ? Local Imports
5+
*/
6+
import {_container} from './LanguagePicker.style';
7+
import LanguageItem, {ILanguageItemProps} from './language-item/LanguageItem';
8+
9+
const windowWidth = Dimensions.get('window').width;
10+
const windowHeight = Dimensions.get('window').height;
11+
12+
export interface ILanguagePicker {
13+
title: string;
14+
imageSource: any;
15+
language?: string;
16+
}
17+
18+
export interface ILanguagePickerProps {
19+
data: ILanguagePicker[];
20+
flatListStyle?: ViewStyle;
21+
containerWidth?: number;
22+
containerHeight?: number;
23+
initialIndex?: number;
24+
languageItemProps?: ILanguageItemProps;
25+
onSelect?: (selectedItem: ILanguagePicker) => void;
26+
}
27+
28+
const LanguagePicker: React.FC<ILanguagePickerProps> = ({
29+
data,
30+
flatListStyle,
31+
initialIndex = -1,
32+
containerWidth = windowWidth * 0.9,
33+
containerHeight = windowHeight * 0.7,
34+
onSelect,
35+
languageItemProps,
36+
...rest
37+
}) => {
38+
const [selectedItem, setSelectedItem] = useState<ILanguagePicker | undefined>(
39+
data[initialIndex],
40+
);
41+
42+
const handleOnSelectItem = (item: ILanguagePicker) => {
43+
setSelectedItem(item);
44+
onSelect && onSelect(item);
45+
};
46+
47+
const renderItem = (item: ILanguagePicker) => (
48+
<LanguageItem
49+
{...languageItemProps}
50+
onSelect={handleOnSelectItem}
51+
isActive={selectedItem === item}
52+
item={item}
53+
/>
54+
);
55+
56+
return (
57+
<FlatList
58+
{...rest}
59+
data={data}
60+
style={[_container(containerWidth, containerHeight), flatListStyle]}
61+
renderItem={({item}) => renderItem(item)}
62+
keyExtractor={item => item.title}
63+
/>
64+
);
65+
};
66+
67+
export default LanguagePicker;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { StyleSheet, Dimensions, ViewStyle, TextStyle } from "react-native";
2+
const windowWidth = Dimensions.get("window").width;
3+
const windowHeight = Dimensions.get("window").height;
4+
5+
export const _itemContainer = (
6+
backgroundColor: string,
7+
borderColor: string,
8+
width: number,
9+
height: number,
10+
): ViewStyle => ({
11+
borderColor: borderColor,
12+
backgroundColor: backgroundColor,
13+
borderWidth: 2,
14+
borderRadius: 20,
15+
height: height,
16+
width: width,
17+
marginBottom: 12,
18+
flexDirection: "row",
19+
alignItems: "center",
20+
paddingLeft: 24,
21+
});
22+
23+
export const _titleStyle = (color: string): TextStyle => ({
24+
fontWeight: "600",
25+
color: color,
26+
fontSize: 16,
27+
});
28+
29+
export default StyleSheet.create({
30+
imageStyle: {
31+
width: 40,
32+
height: 40,
33+
marginRight: 18,
34+
},
35+
checkImageStyle: {
36+
position: "absolute",
37+
right: 24,
38+
width: 20,
39+
height: 20,
40+
color: "red",
41+
},
42+
});
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React from 'react';
2+
import {
3+
Dimensions,
4+
Image,
5+
ImageSourcePropType,
6+
Text,
7+
ViewStyle,
8+
} from 'react-native';
9+
import RNBounceable from '@freakycoder/react-native-bounceable';
10+
/**
11+
* ? Local Imports
12+
*/
13+
import {ILanguagePicker} from '../LanguagePicker';
14+
import styles, {_itemContainer, _titleStyle} from './LanguageItem.style';
15+
16+
const windowWidth = Dimensions.get('window').width;
17+
18+
interface ILanguageItemRequiredProps extends ILanguageItemProps {
19+
item: ILanguagePicker;
20+
isActive: boolean;
21+
}
22+
23+
export interface ILanguageItemProps {
24+
width?: number;
25+
height?: number;
26+
backgroundColor?: string;
27+
activeBorderColor?: string;
28+
textColor?: string;
29+
itemContainer?: ViewStyle;
30+
imageComponent?: React.ReactNode | React.ReactNode[];
31+
checkComponent?: React.ReactNode | React.ReactNode[];
32+
rightImageSource?: ImageSourcePropType;
33+
onSelect?: (selectedItem: ILanguagePicker) => void;
34+
}
35+
36+
const LanguageItem: React.FC<ILanguageItemRequiredProps> = ({
37+
itemContainer,
38+
item,
39+
width = windowWidth * 0.9,
40+
height = 80,
41+
isActive,
42+
backgroundColor = '#FFFFFF',
43+
textColor = '#2F3452',
44+
imageComponent,
45+
checkComponent,
46+
activeBorderColor = '#504ED9',
47+
rightImageSource,
48+
onSelect,
49+
}) => {
50+
const borderColor = isActive ? activeBorderColor : backgroundColor;
51+
return (
52+
<RNBounceable
53+
style={[
54+
_itemContainer(backgroundColor, borderColor, width, height),
55+
itemContainer,
56+
]}
57+
onPress={() => onSelect && onSelect(item)}
58+
>
59+
{imageComponent || (
60+
<Image source={item.imageSource} style={styles.imageStyle} />
61+
)}
62+
<Text style={_titleStyle(textColor)}>{item.title}</Text>
63+
{isActive &&
64+
(checkComponent || (
65+
<Image
66+
source={rightImageSource || require('../local-assets/check.png')}
67+
style={styles.checkImageStyle}
68+
/>
69+
))}
70+
</RNBounceable>
71+
);
72+
};
73+
74+
export default LanguageItem;

example/lib/local-assets/america.png

28.2 KB
Loading

example/lib/local-assets/check.png

13.5 KB
Loading

example/lib/local-assets/germany.png

11.4 KB
Loading

example/lib/local-assets/italy.png

16.1 KB
Loading

0 commit comments

Comments
 (0)