Skip to content

Commit

Permalink
Merge pull request #22 from the-collab-lab/bo_ce_filter_list
Browse files Browse the repository at this point in the history
Bo ce filter list
  • Loading branch information
elitcenk authored Feb 21, 2024
2 parents f5e2906 + 0aa76d9 commit 3c73ee0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
40 changes: 40 additions & 0 deletions src/components/SearchList.jsx
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;
19 changes: 15 additions & 4 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { useState, useEffect } from 'react';
import { ListItem } from '../components';
import SearchList from '../components/SearchList';
import { useParams } from 'react-router-dom';

export function List({ data }) {
const [newList, setNewList] = useState([]);
const { path } = useParams();

useEffect(() => {
setNewList(data);
}, [data]);

return (
<>
<p>
Hello from the <code>/list</code> page!
</p>
<h2>
You are on the <code>{path}</code> list!
</h2>
<SearchList data={data} setNewList={setNewList} />
<ul>
{data.map((item) => (
{newList.map((item) => (
<ListItem key={item.id} name={item.name} />
))}
</ul>
Expand Down

0 comments on commit 3c73ee0

Please sign in to comment.