Skip to content

Commit

Permalink
feat: made the list searchable
Browse files Browse the repository at this point in the history
  • Loading branch information
jrammmy committed Mar 22, 2021
1 parent 4526671 commit 4028f09
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/components/dropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Platform,
ViewPropTypes,
I18nManager,
TextInput,
} from 'react-native';
import Ripple from 'react-native-material-ripple';
import { TextField } from 'react-native-material-textfield';
Expand Down Expand Up @@ -185,6 +186,7 @@ export default class Dropdown extends PureComponent {
selected: -1,
modal: false,
value,
data: this.props.data
};
}

Expand Down Expand Up @@ -340,21 +342,26 @@ export default class Dropdown extends PureComponent {

onSelect(index) {
let {
data,
valueExtractor,
onChangeText,
animationDuration,
rippleDuration,
} = this.props;

let data = this.state.data;


let value = valueExtractor(data[index], index);
let delay = Math.max(0, rippleDuration - animationDuration);

if ('function' === typeof onChangeText) {
onChangeText(value, index, data);
}

setTimeout(() => this.onClose(value), delay);
setTimeout(() => {
this.onClose(value);
this.setState({data: this.props.data});
}, delay);
}

onLayout(event) {
Expand Down Expand Up @@ -562,6 +569,18 @@ export default class Dropdown extends PureComponent {
);
}

searchFilterFunction(text) {
let arrayholder = this.props.data
const newData = arrayholder.filter(item => {
const itemData = `${item.value.toUpperCase()}`;
const textData = text.toUpperCase();

return itemData.indexOf(textData) > -1;
});

this.setState({ data: newData });
};

renderItem({ item, index }) {
if (null == item) {
return null;
Expand Down Expand Up @@ -748,9 +767,14 @@ export default class Dropdown extends PureComponent {
style={[styles.picker, pickerStyle, pickerStyleOverrides]}
onStartShouldSetResponder={() => true}
>
<TextInput
style={styles.input}
placeholder={'Search here'}
onChangeText={text => this.searchFilterFunction(text)}
/>
<FlatList
ref={this.updateScrollRef}
data={data}
data={this.state.data}
style={styles.scroll}
renderItem={this.renderItem}
keyExtractor={this.keyExtractor}
Expand Down
7 changes: 7 additions & 0 deletions src/components/dropdown/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ export default StyleSheet.create({
scrollContainer: {
paddingVertical: 8,
},

input: {
height: 40,
margin: 12,
borderBottomWidth: 0.5,
},

});

0 comments on commit 4028f09

Please sign in to comment.