-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectOption.tsx
More file actions
151 lines (140 loc) · 5.54 KB
/
SelectOption.tsx
File metadata and controls
151 lines (140 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { useState } from 'react';
import {
View,
StyleSheet,
Modal,
Text,
TouchableOpacity,
FlatList,
TextInput
} from 'react-native';
import colors from "./includes/colors";
import { borderRadius, size } from "./includes/constants";
interface ListItem {
[key: string]: string;
}
interface SelectOptionProps {
listItems: ListItem[];
selectedValueState: { state: ListItem | null; setState: (item: ListItem | null) => void };
placeHolder: string;
selectButtonStyle?: object;
dispatch?: ((action: () => void) => void) | null;
showLabel?: string;
itemsPerPage?: number;
}
const SelectOption: React.FC<SelectOptionProps> = ({
listItems,
selectedValueState,
placeHolder,
selectButtonStyle = {
alignItems: 'center', justifyContent: 'center',
backgroundColor: colors.lightGrey,
borderRadius: borderRadius, elevation: 5, paddingVertical: 7.5, height: '35%'
},
dispatch = null,
showLabel = 'name',
itemsPerPage = 100
}) => {
const [visible, setVisible] = useState<boolean>(false);
const [searchQuery, setSearchQuery] = useState<string>('');
const [pageNumber, setPageNumber] = useState<number>(1);
const toggleModal = () => setVisible(!visible);
const handleItemPress = (item: ListItem | null) => {
const updateDis = () => selectedValueState.setState(item);
dispatch !== null ? dispatch(updateDis) : updateDis();
toggleModal();
};
const loadMoreItems = () => setPageNumber(pageNumber + 1);
const filteredItems = listItems
.filter(item => item[showLabel]?.toLowerCase().includes(searchQuery.toLowerCase()))
.slice(0, pageNumber * itemsPerPage);
return (
<View>
<TouchableOpacity style={selectButtonStyle} onPress={toggleModal}>
<Text style={{ color: colors.dark, fontSize: size.text, fontWeight: 'normal' }}>
{(selectedValueState.state === null
|| typeof selectedValueState.state === 'number' )?
placeHolder :
selectedValueState.state[showLabel]}
</Text>
</TouchableOpacity>
<Modal visible={visible} transparent={true} onRequestClose={toggleModal}>
<View style={styles.modalContainer}>
<View style={styles.modalContent}>
<TextInput
style={styles.searchInput}
placeholder={`Search...`}
onChangeText={(text) => setSearchQuery(text)}
value={searchQuery}
/>
<TouchableOpacity onPress={() => handleItemPress(null)} style={styles.listItem}>
<Text style={styles.itemText}>-- {placeHolder} --</Text>
</TouchableOpacity>
<FlatList
data={filteredItems}
renderItem={({ item }) => (
<TouchableOpacity onPress={() => handleItemPress(item)} style={styles.listItem}>
<Text style={styles.itemText}>{item[showLabel]}</Text>
</TouchableOpacity>
)}
keyExtractor={(item, index) => index.toString()}
onEndReached={loadMoreItems}
onEndReachedThreshold={0.1}
/>
<TouchableOpacity style={styles.closeButton} onPress={toggleModal}>
<Text style={styles.closeButtonText}>Close</Text>
</TouchableOpacity>
</View>
</View>
</Modal>
</View>
);
};
const styles = StyleSheet.create({
modalContainer: {
flex: 8,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
modalContent: {
backgroundColor: 'white',
minHeight: '50%',
maxHeight: '80%',
minWidth: "80%",
maxWidth: "90%",
borderRadius: 30,
padding: '5%',
},
searchInput: {
borderWidth: 1,
borderColor: colors.dark,
borderRadius: borderRadius * 0.4,
padding: 8,
marginBottom: 16,
},
listItem: {
paddingVertical: 10,
borderBottomWidth: 0.5,
borderBottomColor: colors.grey,
},
itemText: {
fontSize: size.text,
color: colors.dark,
},
closeButton: {
height: "10%",
width: "88%",
backgroundColor: colors.light,
borderRadius: 10,
alignItems: 'center',
justifyContent: 'center',
marginTop: 20,
alignSelf: 'center',
},
closeButtonText: {
fontSize: size.label,
color: colors.red,
},
});
export default SelectOption;