Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bo ce filter list #22

Merged
merged 6 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading