From 5a2d7cfe5441748b0b039ad8791262245cb01c7e Mon Sep 17 00:00:00 2001 From: David Westgate Date: Mon, 10 Feb 2025 21:14:52 -0800 Subject: [PATCH] add initial tests; fix up logic in ChipsSelector Modal --- components/ChipsSelector.tsx | 46 ++++++++----- components/__tests__/ChipsSelector.test.ts | 0 components/__tests__/ChipsSelector.test.tsx | 72 +++++++++++++++++++++ 3 files changed, 102 insertions(+), 16 deletions(-) delete mode 100644 components/__tests__/ChipsSelector.test.ts create mode 100644 components/__tests__/ChipsSelector.test.tsx diff --git a/components/ChipsSelector.tsx b/components/ChipsSelector.tsx index 0a4eca4..e594ffe 100644 --- a/components/ChipsSelector.tsx +++ b/components/ChipsSelector.tsx @@ -3,7 +3,6 @@ import { View, Text, TextInput, - TouchableOpacity, StyleSheet, Button, ColorValue, @@ -25,27 +24,40 @@ const ChipInputModal = ({ const color: ColorValue = useMemo(() => showModal[1], [showModal]); const colorIdx = useMemo(() => colors.indexOf(color), [color]); - const value: number = useMemo( - () => totalChipsCount[colorIdx], - [totalChipsCount, colorIdx] - ); + const [value, setValue] = useState(); // value may be undefined initially + + // Reset the color value when the specific color this modal is for, changes. The same modal is shared/reused in all cases. + useEffect(() => { + setValue(totalChipsCount[colorIdx]); + }, [colorIdx]); return ( setShowModal([false, color])} > - Number of {showModal[1]?.toString()} chips - { - const n = parseInt(v); - update(showModal[1], Number.isNaN(n) ? 0 : n); + {value !== undefined && ( + <> + Number of {showModal[1]?.toString()} chips + { + const dirtyNum: number = parseInt(v); + !isNaN(dirtyNum) ? setValue(dirtyNum) : setValue(0); + }} + testID="modalInput" + /> + + )} +