From 7d395f5d1528b549f71666b9649ff451e2e55049 Mon Sep 17 00:00:00 2001 From: kagura Date: Fri, 22 Nov 2024 15:20:47 +0800 Subject: [PATCH] Stage --- src/FoodOrderer.tsx | 95 +++++++++++++++++++++++++++++++++------------ 1 file changed, 71 insertions(+), 24 deletions(-) diff --git a/src/FoodOrderer.tsx b/src/FoodOrderer.tsx index 1724c79..370259d 100644 --- a/src/FoodOrderer.tsx +++ b/src/FoodOrderer.tsx @@ -4,7 +4,9 @@ interface ShopItem { name: string, need: number, // 需求数量 provide: number, // 提供的数量 - imageUrl: string + image: { + src: string + } } const shopItems: ShopItem[] = [ @@ -12,51 +14,96 @@ const shopItems: ShopItem[] = [ name: "Apple", need: 10, provide: 5, - imageUrl: "https://example.com/images/apple.png" + image: { + src: "https://i.imgur.com/FsNUv2x.jpeg" + } }, { - name: "Orange", + name: "Orange Jam", need: 8, provide: 10, - imageUrl: "https://example.com/images/orange.png" + image: { + src: "https://i.imgur.com/hB46Z9s.png" + } }, { - name: "Milk", - need: 15, - provide: 20, - imageUrl: "https://example.com/images/milk.png" + name: "Milk (Gone bad)", + need: 0, + provide: 3, + image: { + src: "https://i.imgur.com/ZV7vzDu.png" + } }, { name: "Bread", need: 12, provide: 6, - imageUrl: "https://example.com/images/bread.png" + image: { + src: "https://i.imgur.com/qBn8jWS.jpeg" + } } ]; -function FoodList({list,setList}:{ -list: ShopItem[], -setList: React.Dispatch> +function FoodList({ list, setList }: { + list: ShopItem[], + setList: React.Dispatch> }) { - - return( -
- {list.map((item) => -
- + enum ButtonOperation {INCREASE , DECREASE} + const handleVolumeButton = (item: ShopItem ,operation: ButtonOperation) => { + const nowNeed = item.need + switch (operation) { + case ButtonOperation.INCREASE: + if (nowNeed >= item.provide){ + return + } + setList(list.map((it)=> + it == item? {...it,need: nowNeed+1}:it + )) + break + case ButtonOperation.DECREASE: + if (nowNeed <= 0){ + return + } + setList(list.map((it)=> + it == item? {...it,need: nowNeed-1}:it + )) + break + } + } + + + return ( +
+ {list.map((item) => +
+ +
+

{item.name}

+
+ +

{item.need}

+ +
+
+
+ )}
- )} -
-)} + ) +} const FoodOrderer = () => { - const [items,setItems] = useState(shopItems) + const [items, setItems] = useState(shopItems) return ( - <> +
+

点些什么

- +
) }