forked from Ajith-Pandian/SearchableFlatlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
27 lines (25 loc) · 857 Bytes
/
index.js
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
import React, { Component } from "react";
import { FlatList } from "react-native";
import PropTypes from "prop-types";
export default class SearchableFlatlist extends Component {
static INCLUDES = "includes";
static WORDS = "words";
getFilteredResults() {
let { data, type, searchProperty, searchTerm } = this.props;
return data.filter(
item =>
type && type === SearchableFlatlist.WORDS
? new RegExp(`\\b${searchTerm}`, "gi").test(item[searchProperty])
: new RegExp(`${searchTerm}`, "gi").test(item[searchProperty])
);
}
render() {
return <FlatList {...this.props} data={this.getFilteredResults()} />;
}
}
SearchableFlatlist.propTypes = {
data: PropTypes.array.isRequired,
searchProperty: PropTypes.string.isRequired,
searchTerm: PropTypes.string.isRequired,
type: PropTypes.string
};