I'm developing a React Native application which consists of FlatList. I referred this article, https://medium.com/react-native-development/how-to-use-the-flatlist-component-react-native-basics-92c482816fe6 to use FlatList component to my application.
I would like to implement search bar to search the contents of that list (using the titles of each item) & automatically render the content according to the search text. How can I do this without using any libraries?
Thanks for your help!
Check below example which i created using flatlist and TextInput. Items are displayed in the form of a dropdown list when you search items. i think this will help you.
import React, { Component } from 'react';
import { View, Text, FlatList, TextInput, ListItem } from 'react-native';
class SearchFunction extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
value: '',
};
this.arrayNew = [
{ name: 'Robert' },
{ name: 'Bryan' },
{ name: 'Vicente' },
{ name: 'Tristan' },
{ name: 'Marie' },
{ name: 'Onni' },
{ name: 'sophie' },
{ name: 'Brad' },
{ name: 'Samual' },
{ name: 'Omur' },
{ name: 'Ower' },
{ name: 'Awery' },
{ name: 'Ann' },
{ name: 'Jhone' },
{ name: 'z' },
{ name: 'bb' },
{ name: 'cc' },
{ name: 'd' },
{ name: 'e' },
{ name: 'f' },
{ name: 'g' },
{ name: 'h' },
{ name: 'i' },
{ name: 'j' },
{ name: 'k' },
{ name: 'React' },
{ name: 'React native' },
{ name: 'react navigations' },
];
}
renderSeparator = () => {
return (
<View
style={{
height: 1,
width: '100%',
backgroundColor: '#CED0CE',
}}
/>
);
};
searchItems = text => {
let newData = this.arrayNew.filter(item => {
const itemData = `${item.name.toUpperCase()}`;
const textData = text.toUpperCase();
if(text.length >0 ){
return itemData.indexOf(textData) > -1;
}
});
this.setState({
data: newData,
value: text,
});
};
renderHeader = () => {
return (
<TextInput
style={{ height: 60, borderColor: '#000', borderWidth: 1 }}
placeholder=" Type Name..."
onChangeText={text => this.searchItems(text)}
value={this.state.value}
/>
);
};
render() {
return (
<View
style={{
flex: 1,
padding: 25,
width: '98%',
alignSelf: 'center',
justifyContent: 'center',
}}>
<FlatList
data={this.state.data}
renderItem={({ item }) => (
<Text style={{ padding: 10 }}>{item.name} </Text>
)}
keyExtractor={item => item.name}
ItemSeparatorComponent={this.renderSeparator}
ListHeaderComponent={this.renderHeader}
/>
</View>
);
}
}
export default SearchFunction;
Install the library Searchable Flatlist inside an empty folder, using npm install --save searchable-flatlist
Open the node_module folder created inside the folder above, copy the searchable-flatlist folder and paste inside your project's node_module folder
Then follow this code snippet:
import SearchableFlatlist from "searchable-flatlist";
import {Button, Input, Container, Header, Content, Card, CardItem, Body, Item, Icon} from 'native-base';
export default class App extends Component {
state = { searchTerm: "", data:"empty" };
render() {
if(this.state.data== "empty"){
return (
<View ><ActivityIndicator size="large" color="#0000ff" /> </View>
);
}
else{
return (
<View style={sContainer}>
<Card style={{backgroundColor: "#dddddda1"}}>
<Header searchBar rounded>
<Item>
<Icon name="ios-search" />
<Input placeholder="Search"
onChangeText={searchTerm => this.setState({ searchTerm })}
/>
<Icon name="ios-people" />
</Item>
<Button transparent>
<Text>Search</Text>
</Button>
</Header>
<SearchableFlatlist
searchProperty={"email"}
searchTerm={this.state.searchTerm}
data={this.state.customersBox}
containerStyle={{ flex: 1 }}
renderItem={({ item }) =>
<ListItem
roundAvatar
title={`${item.name.first} ${item.name.last}`}
subtitle={item.email}
avatar={{ uri: item.picture.thumbnail }}
+ containerStyle={{ borderBottomWidth: 0 }}
/>
}
// item.name.first specifies the value to filter with
keyExtractor={item => item.name.first}
ItemSeparatorComponent={Separator}
/>
</Card>
</View>
);
}
}
};
You may need to modify the code a little bit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With