generated from the-collab-lab/smart-shopping-list
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from the-collab-lab/bo_ce_filter_list
Bo ce filter list
- Loading branch information
Showing
2 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useState } from 'react'; | ||
|
||
const SearchList = ({ data, setNewList }) => { | ||
const [value, setValue] = useState(''); | ||
|
||
const handleFiltering = (e) => { | ||
const userInput = e.target.value.toLowerCase(); | ||
setValue(e.target.value); | ||
const gatherItem = []; | ||
for (let i = 0; i < data.length; i++) { | ||
if (data[i]?.name.toLowerCase().includes(userInput)) { | ||
gatherItem.push(data[i]); | ||
} | ||
} | ||
setNewList(gatherItem); | ||
}; | ||
|
||
const resetInput = (e) => { | ||
e.preventDefault(); | ||
setValue(''); | ||
setNewList(data); | ||
}; | ||
|
||
return ( | ||
<form> | ||
<label htmlFor="search">Search item</label> | ||
<input | ||
id="search" | ||
type="text" | ||
onChange={(e) => handleFiltering(e)} | ||
value={value} | ||
/> | ||
<button onClick={(e) => resetInput(e)} aria-label="clear search bar"> | ||
x | ||
</button> | ||
</form> | ||
); | ||
}; | ||
|
||
export default SearchList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters