Skip to content

Commit

Permalink
Merge pull request #23 from the-collab-lab/jo-qg-shopping-list-filter
Browse files Browse the repository at this point in the history
Added SearchBar file to components folder
  • Loading branch information
joriordan332 authored Aug 31, 2024
2 parents 8422653 + 761be2a commit 333739a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
32 changes: 32 additions & 0 deletions src/components/SearchBar.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export function SearchBar({ data, setDisplayData, setSearch, search }) {
const handleInputChange = (e) => {
const searchTerm = e.target.value;
setSearch(searchTerm);

const filteredUsers = data.filter((item) =>
item.name.toLowerCase().includes(searchTerm.trim().toLowerCase()),
);
setDisplayData(filteredUsers);
};

const handleClear = () => {
setDisplayData(data);
setSearch('');
};

return (
<form>
<label htmlFor="item-filter">Search for an item here: </label>
<input
type="text"
id="item-filter"
value={search}
placeholder="Search here"
onChange={handleInputChange}
/>
<button type="button" onClick={handleClear}>
Clear Input
</button>
</form>
);
}
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './ListItem';
export * from './SingleList';
export * from './SearchBar';
3 changes: 1 addition & 2 deletions src/views/Layout.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import { Outlet } from 'react-router-dom';
import { Outlet, NavLink } from 'react-router-dom';

import './Layout.css';
import { useAuth } from '../api';
import { SignInButton, SignOutButton } from '../api/useAuth';
import { NavLink } from 'react-router-dom';
import { Toaster } from 'react-hot-toast';

/**
Expand Down
21 changes: 16 additions & 5 deletions src/views/List.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { ListItem } from '../components';
import { useEffect, useState } from 'react';
import { ListItem, SearchBar } from '../components';

export function List({ data }) {
const [search, setSearch] = useState('');
const [displayData, setDisplayData] = useState([]);

useEffect(() => {
setDisplayData([...data]);
}, [data]);

return (
<>
<p>
Hello from the <code>/list</code> page!
</p>
<SearchBar
setDisplayData={setDisplayData}
data={data}
setSearch={setSearch}
search={search}
/>
<ul>
{data.map((item) => (
{displayData.map((item) => (
<ListItem key={item.id} name={item.name} />
))}
</ul>
Expand Down

0 comments on commit 333739a

Please sign in to comment.