Skip to content

Commit 808ba07

Browse files
bug fix
1 parent 6c27eca commit 808ba07

File tree

5 files changed

+385
-0
lines changed

5 files changed

+385
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

index.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import React, { Component } from 'react';
2+
import {
3+
StyleSheet,
4+
Text,
5+
View,
6+
FlatList,
7+
TextInput,
8+
TouchableOpacity,
9+
Keyboard,
10+
RefreshControl,
11+
KeyboardAvoidingView,
12+
} from 'react-native';
13+
import PropTypes from 'prop-types';
14+
15+
class CommonFlatList extends Component {
16+
constructor(props) {
17+
super(props);
18+
19+
this.filterText = this.filterText.bind(this);
20+
this.refresh = this.refresh.bind(this);
21+
22+
this.state = {
23+
behavior: 'padding',
24+
refreshing: false,
25+
searchText: '',
26+
};
27+
}
28+
29+
onRefresh() {
30+
this.props.pullToRefreshCallback();
31+
this.setState({ refreshing: true });
32+
setTimeout(() => {
33+
this.setState({ refreshing: false });
34+
}, 7000);
35+
}
36+
37+
refresh() {
38+
if (this.props.data.length === 0) {
39+
filtereddata = [{ type: 'emptyrow', name: 'No data available' }];
40+
}
41+
filtereddata = this.props.data;
42+
this.setState({ refreshing: false, data: filtereddata });
43+
}
44+
45+
filterText() {
46+
const { data, searchKey } = this.props;
47+
const { searchText } = this.state;
48+
49+
if (searchText === '') {
50+
return data;
51+
}
52+
53+
const filteredData = [];
54+
for (let d = 0; d < data.length; d++) {
55+
dt = data[d];
56+
for (let s = 0; s < searchKey.length; s++) {
57+
sk = searchKey[s];
58+
const target = dt[sk];
59+
if (target.toLowerCase().indexOf(searchText.toLowerCase()) !== -1) {
60+
filteredData.push(dt);
61+
}
62+
}
63+
}
64+
65+
return filteredData;
66+
}
67+
68+
render() {
69+
const { renderItem, renderSeparator, pullToRefreshCallback } = this.props;
70+
const filteredData = this.filterText();
71+
72+
const searchbar = (
73+
<View style={styles.searchBarContainer}>
74+
<TextInput
75+
style={styles.searchBar}
76+
placeholder="Search ..."
77+
clearButtonMode="while-editing"
78+
placeholderTextColor="#919188"
79+
underlineColorAndroid="transparent"
80+
autoCapitalize="none"
81+
keyboardType="email-address"
82+
onChangeText={text =>
83+
this.setState({
84+
searchText: text.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, ''),
85+
})
86+
}
87+
value={this.state.searchText}
88+
maxLength={100}
89+
/>
90+
</View>
91+
);
92+
93+
const refreshcontrol =
94+
pullToRefreshCallback == null ? null : (
95+
<RefreshControl
96+
refreshing={this.state.refreshing}
97+
onRefresh={this.onRefresh.bind(this)}
98+
/>
99+
);
100+
101+
return (
102+
<KeyboardAvoidingView
103+
behavior={this.state.behavior}
104+
style={styles.container}
105+
>
106+
{this.props.searchKey.length > 0 && searchbar}
107+
<FlatList
108+
refreshControl={refreshcontrol}
109+
data={filteredData}
110+
renderItem={item => renderItem(item.item)}
111+
style={styles.flatList}
112+
ItemSeparatorComponent={renderSeparator}
113+
/>
114+
</KeyboardAvoidingView>
115+
);
116+
}
117+
}
118+
119+
CommonFlatList.propTypes = {
120+
searchKey: PropTypes.array,
121+
data: PropTypes.array,
122+
renderItem: PropTypes.func,
123+
renderSeparator: PropTypes.func,
124+
pullToRefreshCallback: PropTypes.func,
125+
};
126+
CommonFlatList.defaultProps = {
127+
searchKey: [],
128+
data: [],
129+
renderItem: null,
130+
renderSeparator: () => <View style={styles.defaultSeparator} />,
131+
pullToRefreshCallback: null,
132+
};
133+
134+
const styles = StyleSheet.create({
135+
searchBarContainer: {
136+
justifyContent: 'center',
137+
padding: 10,
138+
backgroundColor: '#a7a7a8',
139+
width: '100%',
140+
},
141+
searchBar: {
142+
borderRadius: 5,
143+
// borderWidth: 1,
144+
backgroundColor: 'white',
145+
height: 44,
146+
width: '100%',
147+
paddingHorizontal: 10,
148+
},
149+
container: {
150+
flex: 1,
151+
justifyContent: 'center',
152+
alignItems: 'center',
153+
backgroundColor: 'white',
154+
},
155+
defaultSeparator: { height: 1, backgroundColor: '#CCCCCC' },
156+
flatList: { height: '100%', width: '100%', backgroundColor: 'transparent' },
157+
});
158+
159+
export default CommonFlatList;

package-lock.json

Lines changed: 195 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "react-native-complete-flatlist",
3+
"version": "1.0.0",
4+
"description": "A complete flatlist with search bar etc is ready to use",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha --reporter spec"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/fattahmuhyiddeen/react-native-complete-flatlist.git"
12+
},
13+
"keywords": [
14+
"flatlist"
15+
],
16+
"author": "Fattah Muhyiddeen <fattahmuhyiddeen@gmail.com>",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/fattahmuhyiddeen/react-native-complete-flatlist/issues"
20+
},
21+
"homepage": "https://github.com/fattahmuhyiddeen/react-native-complete-flatlist#readme",
22+
"devDependencies": {
23+
"mocha": "^5.0.0"
24+
},
25+
"peerDependencies": {
26+
"react-native": "*"
27+
},
28+
"dependencies": {
29+
"prop-types": "^15.5.10"
30+
}
31+
}

test/test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)