Skip to content

Commit

Permalink
simplified the structure and reorganized the rendering in the List view
Browse files Browse the repository at this point in the history
  • Loading branch information
GrannyYetta committed Aug 30, 2024
1 parent 7d31273 commit 26e7688
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/components/SearchForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ function SearchForm() {
<div className="search-input-container">
<label htmlFor="search-input">Search items:</label>
<input
value={searchItem}
type="search"
id="search-input"
placeholder="Item from list"
placeholder="Search list..."
onChange={handleSearch}
required
name="search-input"
/>
<button type="button" onClick={clearSearch} className="clear-button">
x
Expand Down
51 changes: 40 additions & 11 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
import { ListItem } from '../components';
import SearchForm from '../components/SearchForm';
// import SearchForm from '../components/SearchForm';
import { useState } from 'react';

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

const [searchTerm, setSearchTerm] = useState('');
const handleSearch = (e) => {
e.preventDefault();
setSearchItem(e.target.value);
};

const filteredData = data.filter((item) =>
item.name.toLowerCase().includes(searchTerm.toLowerCase()),
const clearSearch = () => {
setSearchItem('');
};

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

return (
<>
<p>
Hello from the <code>/list</code> page!
</p>
<SearchForm onClick={handleSearch} />
<form onSubmit={handleSearch}>
<div>
<label htmlFor="search-input"> 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>
{searchTerm
? filteredItems.map((item) => (
<ListItem key={item.id} name={item.name} />
))
: data.map((item) => <ListItem key={item.id} name={item.name} />)}
{data.map((item) => (
<ListItem key={item.id} name={item.name} />
))}
</ul>
</>
);
Expand Down

0 comments on commit 26e7688

Please sign in to comment.