-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
101 lines (92 loc) · 2.46 KB
/
index.js
File metadata and controls
101 lines (92 loc) · 2.46 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
import React, {useState} from 'react';
import {
View,
TextInput,
StyleSheet,
Text,
TouchableOpacity,
} from 'react-native';
import PropTypes from 'prop-types';
import {v4 as uuid} from 'uuid';
import 'react-native-get-random-values';
const DatalistInput = ({
value: passedValue,
onChangeText: passedOnChangeText,
data = [],
containerStyle = {},
style: inputStyle,
menuStyle: customMenuStyle,
menuItemStyle = {},
...props
}) => {
const [value, setValue] = useState(passedValue);
const [menuVisible, setMenuVisible] = useState(false);
const [filteredData, setFilteredData] = useState([]);
const filterData = text => {
return data?.filter(
val => val?.toLowerCase()?.indexOf(text?.toLowerCase()) > -1,
);
};
return (
<View style={containerStyle ?? styles.containerStyle}>
<TextInput
style={[styles.inputStyle, inputStyle]}
value={value}
onChangeText={text => {
passedOnChangeText && passedOnChangeText(text);
if (text && text.length > 0) {
setFilteredData(filterData(text));
setMenuVisible(true);
} else {
setMenuVisible(false);
}
setValue(text);
}}
{...props}
/>
{menuVisible &&
Boolean(filteredData.length) &&
filteredData.map(title =>
React.cloneElement(
<TouchableOpacity
key={uuid()}
style={[styles.menuStyle, customMenuStyle]}
onPress={() => {
setValue(title);
setMenuVisible(false);
}}>
<Text style={[styles.menuItemStyle, menuItemStyle]}>{title}</Text>
</TouchableOpacity>,
),
)}
</View>
);
};
export default DatalistInput;
DatalistInput.propTypes = {
value: PropTypes.string,
onChangeText: PropTypes.func,
data: PropTypes.array,
containerStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
menuItemStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
menuStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
};
const styles = StyleSheet.create({
containerStyle: {
minWidth: '90%',
},
menuStyle: {
backgroundColor: '#cdcdcd',
padding: 10,
},
menuItemStyle: {
color: '#2c2c2c',
},
inputStyle: {
borderColor: '#cdcdcd',
borderWidth: 1,
fontSize: 16,
padding: 10,
},
});