|
| 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; |
0 commit comments