Skip to content

Commit

Permalink
Merge pull request #23 from the-collab-lab/ar-sm-add-list-filter-form
Browse files Browse the repository at this point in the history
Add list filter form
  • Loading branch information
sar-mko authored Sep 1, 2024
2 parents 819d758 + 09f2d77 commit 3a88def
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
import { ListItem } from '../components';
import { useState, useEffect } from 'react';

export function List({ data }) {
const [filterVal, setFilterVal] = useState('');
const [filteredList, setFilteredList] = useState([]);

const clearInput = (e) => {
e.preventDefault();
setFilterVal('');
};

useEffect(() => {
setFilteredList(
data.filter((item) =>
item.name.toLowerCase().includes(filterVal.toLowerCase()),
),
);
}, [filterVal, data]);

return (
<>
<p>
Hello from the <code>/list</code> page!
</p>

<form onSubmit={clearInput}>
<label htmlFor="item-name"> Item name:</label>
<input
id="item-name"
name="item-name"
type="text"
value={filterVal}
onChange={(e) => setFilterVal(e.target.value)}
/>
{filterVal && <button>Clear</button>}
</form>

<ul>
{data &&
data.map((list) => {
{filteredList &&
filteredList.map((list) => {
return <ListItem key={list.id} name={list.name} />;
})}
</ul>
Expand Down

0 comments on commit 3a88def

Please sign in to comment.