<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<div id="root"></div>
<script>
const data = [
{
id: "1001",
first_name: "Alez",
last_name: "Dara",
Department: "IT",
Supervisor: "Supervisor A"
},
{
id: "1002",
first_name: "Baro",
last_name: "bara",
Department: "Accounting",
Supervisor: "Supervisor A"
},
{
id: "1003",
first_name: "Tata",
last_name: "uara",
Department: "IT",
Supervisor: "Supervisor A"
},
{
id: "1004",
first_name: "test 4",
last_name: "Mara",
Department: "Sales",
Supervisor: "Supervisor C"
},
{
id: "1005",
first_name: "alex",
last_name: "gara",
Department: "Sales",
Supervisor: "Supervisor C"
},
{
id: "1006",
first_name: "ki",
last_name: "tara",
Department: "IT",
Supervisor: "Supervisor A"
}
];
</script>
<script type="text/babel">
class App extends React.Component {
constructor() {
super();
this.state = {
data: data,
filteredData: data,
filterKey: "all",
searchValue: ""
};
}
changeFilter = filterKey => {
this.setState(
{
filterKey: filterKey
},
() => this.onSearchTextChange(this.state.searchValue)
);
};
onSearchTextChange = searchText => {
if (this.state.filterKey && this.state.filterKey !== "all") {
this.setState({
filteredData: this.state.data.filter(
item =>
typeof item[this.state.filterKey] === "string" &&
item[this.state.filterKey]
.toLowerCase()
.includes(searchText.toLowerCase())
),
searchValue: searchText
});
return;
}
const newData = this.state.data.filter(
item =>
(typeof item.first_name === "string" &&
item.first_name.toLowerCase().includes(searchText.toLowerCase())) ||
(typeof item.last_name === "string" &&
item.last_name.toLowerCase().includes(searchText.toLowerCase())) ||
(typeof item.Department === "string" &&
item.Department.toLowerCase().includes(searchText.toLowerCase())) ||
(typeof item.Supervisor === "string" &&
item.Supervisor.toLowerCase().includes(searchText.toLowerCase()))
);
this.setState({
filteredData: newData,
searchValue: searchText
});
};
render() {
return (
<div>
<div className="menubar">
<button onClick={() => this.changeFilter("all")}>All</button>
<button onClick={() => this.changeFilter("last_name")}>
Last Name
</button>
<button onClick={() => this.changeFilter("Department")}>
Department
</button>
</div>
{this.state.filteredData.map((person, i) => (
<div className="profilecard" key={i}>
<div className="profilecontent">
<ul>
<li>
<strong>First Name:</strong> {person.first_name}
</li>
<li>
<strong>Last Name:</strong> {person.last_name}
</li>
<li>
<strong>Department:</strong> {person.Department}
</li>
<li>
<strong>Supervisor:</strong> {person.Supervisor}
</li>
</ul>
</div>
</div>
))}
<input
placeholder={`search ${this.state.filterKey}`}
onChange={e => this.onSearchTextChange(e.target.value)}
value={this.state.searchValue}
/>
</div>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
</script>