I have project component that I'm duplicating according to a project list. Once the page is rendered and all Project components are displayed I want to enable a search functionality.
When I perform the search and I filter the project list, the old results are displayed near the new results after search!
How to cause it to display only search results?
Updated code (Working):
class ProjectsList extends Component {
state = {
projectsDetails: [......],
filterKey: null
}
componentWillMount() {
//service.getProjectDetails();
}
handleSearchChange(e) {
this.setState({ filterKey: e.target.value });
}
render() {
const { filterKey, projectsDetails } = this.state;
const filteredProjects = filterKey
? projectsDetails.filter(item =>
item.Name.toLowerCase().includes(filterKey.toLowerCase()) && item.FabricationConfigs !== null
)
: projectsDetails.filter(item =>
item.FabricationConfigs !== null
);
return (
<table width="100%">
<tbody>
<tr>
<td>
<div className="Toolbar2">
<table width="100%">
<tbody>
<tr>
<td>
<h1>Projects</h1>
</td>
<td width="20%" align="right">
<Search labelText="" id="search-1" onChange={ this.handleSearchChange.bind(this) }/>
</td>
</tr>
</tbody>
</table>
<hr></hr>
</div>
<div className="inline">
{filteredProjects.map(item =>
<Project
key={item.Name}
name={item.Name}
description={item.Description}
configurations={item.FabricationConfigs}/>)}
</div>
</td>
</tr>
</tbody>
</table>
);
}
}