From e36161cc277e27ca9e7b4aaaa114e49291588b9b Mon Sep 17 00:00:00 2001 From: Irori235 Date: Thu, 1 Jun 2023 17:45:26 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E3=82=AB=E3=83=86=E3=82=B4?= =?UTF-8?q?=E3=83=AA=E3=81=AE=E4=B8=8B=E3=81=AB=E7=B7=8F=E5=92=8C=E3=82=92?= =?UTF-8?q?=E5=87=BA=E3=81=99=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/BudgetTable.tsx | 91 +++++++++++-------- .../foundation/Dropdown/Dropdown.tsx | 4 +- 2 files changed, 55 insertions(+), 40 deletions(-) diff --git a/src/components/BudgetTable.tsx b/src/components/BudgetTable.tsx index bf432c3..38b0971 100644 --- a/src/components/BudgetTable.tsx +++ b/src/components/BudgetTable.tsx @@ -17,68 +17,83 @@ const BudgetTable: React.FC = ({ return null; } - const totalCost = budget.categories - .flatMap((category) => category.items) - .reduce((sum, item) => sum + item.cost, 0); + const calculateSum = (items: Item[]) => + items.reduce((sum, item) => sum + item.cost, 0); + const totalCost = calculateSum( + budget.categories.flatMap((category) => category.items) + ); const diff = budget.monthBudget - totalCost; - const symbol = diff >= 0 ? "▲" : "▼"; - const color = diff >= 0 ? "text-green-500" : "text-pink-500"; + + const getColor = (value: number) => + value >= 0 ? "text-green-500" : "text-pink-500"; + const getSymbol = (value: number) => (value >= 0 ? "▲" : "▼"); return ( <> - + - {budget.categories.map((category) => ( - - {category.items.map((item) => ( - - - {item.name === category.items[0].name ? ( - + + {index === 0 && ( + + )} + + - ) : null} - - - - ))} - - ))} + + ))} + + ); + })} diff --git a/src/components/foundation/Dropdown/Dropdown.tsx b/src/components/foundation/Dropdown/Dropdown.tsx index 96114fd..030975e 100644 --- a/src/components/foundation/Dropdown/Dropdown.tsx +++ b/src/components/foundation/Dropdown/Dropdown.tsx @@ -7,7 +7,7 @@ interface DropdownProps { const Dropdown: React.FC = ({ options }) => { return ( -
+
{({ open }) => ( <> @@ -28,7 +28,7 @@ const Dropdown: React.FC = ({ options }) => { {open && ( - + {options.map((option, index) => ( {({ active }) => (
- category + Category - name + Name - cost + Cost
- handleSelectItem(item, e.target.checked)} - /> - - {category.name} + {budget.categories.map((category) => { + const categorySum = calculateSum(category.items); + return ( + + {category.items.map((item, index) => ( +
+ + handleSelectItem(item, e.target.checked) + } + /> + + {category.name ?

{category.name}

:

-

} + +

+ {categorySum} +

+
+ {item.name} + + {item.cost} - {item.name} - - {item.cost} -
- {symbol} {Math.abs(diff)} + {getSymbol(diff)} {Math.abs(diff)}