Skip to content

Commit

Permalink
Merge pull request #22 from the-collab-lab/rt-tc-search-item-in-list
Browse files Browse the repository at this point in the history
Rt tc search item in list
  • Loading branch information
GrannyYetta authored Aug 31, 2024
2 parents 7424b50 + db8ee22 commit 10286eb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Empty file added src/components/SearchForm.jsx
Empty file.
41 changes: 41 additions & 0 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
import { ListItem } from '../components';
import { useState } from 'react';

export function List({ data }) {
const [searchItem, setSearchItem] = useState('');

const handleSearch = (e) => {
e.preventDefault();
setSearchItem(e.target.value);
};

const clearSearch = () => {
setSearchItem('');
};

const filterItems = data.filter((item) =>
item.name.toLowerCase().includes(searchItem.toLocaleLowerCase()),
);

return (
<>
<p>
Hello from the <code>/list</code> page!
</p>
<form onSubmit={handleSearch}>
<div>
<label htmlFor="search-item-in-list"> Search items:</label>
<input
onChange={handleSearch}
type="search"
id="search-item-in-list"
value={searchItem}
placeholder="Search item..."
/>
{searchItem && (
<button type="button" onClick={clearSearch}>
x
</button>
)}
{searchItem && (
<ul>
{filterItems.map((item) => (
<ListItem key={item.id} name={item.name} />
))}
</ul>
)}
</div>
</form>

<ul>
{data.map((item) => (
<ListItem key={item.id} name={item.name} />
Expand Down

0 comments on commit 10286eb

Please sign in to comment.