Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function App() {
return (
<div className={"App " + (isDarkMode ? "dark" : "light")}>
<Header isDarkMode={isDarkMode} onDarkModeClick={handleDarkModeClick} />
<ShoppingList items={items} />
<ShoppingList items={items} setItems={setItems}/>
</div>
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/components/Filter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from "react";

function Filter({ onCategoryChange }) {
function Filter({ onCategoryChange, selectedCategory, search, onSearchChange }) {

return (
<div className="Filter">
<input type="text" name="search" placeholder="Search..." />
<select name="filter" onChange={onCategoryChange}>
<input type="text" name="search" placeholder="Search..." value={search} onChange={onSearchChange}/>
<select name="filter" value={selectedCategory} onChange={onCategoryChange}>
<option value="All">Filter by category</option>
<option value="Produce">Produce</option>
<option value="Dairy">Dairy</option>
Expand Down
32 changes: 27 additions & 5 deletions src/components/ItemForm.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import React from "react";
import React, {useState} from "react";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import React, {useState} from "react";
import React, { useState } from "react";

import { v4 as uuid } from "uuid";

function ItemForm(props) {
function ItemForm({onItemFormSubmit}) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function ItemForm({onItemFormSubmit}) {
function ItemForm({ onItemFormSubmit }) {

const [name, setName] = useState("")
const [category, setCategory] = useState("Produce")

function handleNameChange(event) {
setName(event.target.value)
}

function handleCategoryChange(event) {
setCategory(event.target.value)
}

function handleFormSubmit(event) {
event.preventDefault()
const newItem ={

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const newItem ={
const newItem = {

id: uuid(),
name: name,
category: category
}
onItemFormSubmit(newItem)
}


return (
<form className="NewItem">
<form className="NewItem" onSubmit={handleFormSubmit}>
<label>
Name:
<input type="text" name="name" />
<input type="text" name="name" onChange={handleNameChange} value={name}/>
</label>

<label>
Category:
<select name="category">
<select name="category" onChange={handleCategoryChange} value={category}>
<option value="Produce">Produce</option>
<option value="Dairy">Dairy</option>
<option value="Dessert">Dessert</option>
Expand Down
21 changes: 14 additions & 7 deletions src/components/ShoppingList.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@ import ItemForm from "./ItemForm";
import Filter from "./Filter";
import Item from "./Item";

function ShoppingList({ items }) {
function ShoppingList({ items, setItems }) {
const [selectedCategory, setSelectedCategory] = useState("All");
const [search, setSearch] = useState("")

function handleCategoryChange(event) {
setSelectedCategory(event.target.value);
}

const itemsToDisplay = items.filter((item) => {
if (selectedCategory === "All") return true;
function handleSearchChange(event) {
setSearch(event.target.value)
}

function handleItemSubmit(newItem) {
setItems([...items, newItem])
}

return item.category === selectedCategory;
});
const itemsToDisplay =
items.filter((item) => item.name.toLowerCase().includes(search.toLocaleLowerCase()))
.filter((item) => (selectedCategory === "All") || item.category === selectedCategory)

return (
<div className="ShoppingList">
<ItemForm />
<Filter onCategoryChange={handleCategoryChange} />
<ItemForm onCategoryChange={handleCategoryChange} onItemFormSubmit={handleItemSubmit}/>
<Filter selectedCategory={selectedCategory} onCategoryChange={handleCategoryChange} search={search} onSearchChange={handleSearchChange}/>
<ul className="Items">
{itemsToDisplay.map((item) => (
<Item key={item.id} name={item.name} category={item.category} />
Expand Down