Skip to content

Commit

Permalink
eliminated onSearch and simplified the filtering funciton in the List…
Browse files Browse the repository at this point in the history
… view
  • Loading branch information
GrannyYetta committed Aug 30, 2024
1 parent d247f3c commit 7d31273
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
13 changes: 8 additions & 5 deletions src/components/SearchForm.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
function SearchForm({ onSearch }) {
import { useState } from 'react';

function SearchForm() {
const [searchItem, setSearchItem] = useState('');

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

onSearch(value);
setSearchItem(e.target.value);
};

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

return (
<form onSubmit={handleSearch}>
<div className="search-input-container">
<label htmlFor="search-input">Search items:</label>
<input
value={searchItem}
type="search"
id="search-input"
placeholder="Item from list"
Expand Down
19 changes: 4 additions & 15 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,16 @@ export function List({ data }) {

const [searchTerm, setSearchTerm] = useState('');

console.log(data);

const handleSearch = (searchItem) => {
console.log(searchItem);
setSearchTerm(searchItem);
if (searchItem === '') {
setFilteredItems(data);
} else {
const filtered = data.filter((item) =>
item.name.toLowerCase().includes(searchItem.toLowerCase()),
);
setFilteredItems(filtered);
}
};
const filteredData = data.filter((item) =>
item.name.toLowerCase().includes(searchTerm.toLowerCase()),
);

return (
<>
<p>
Hello from the <code>/list</code> page!
</p>
<SearchForm onSearch={handleSearch} />
<SearchForm onClick={handleSearch} />
<ul>
{searchTerm
? filteredItems.map((item) => (
Expand Down

0 comments on commit 7d31273

Please sign in to comment.