diff --git a/examples/demo/src/components/modals/MultiPairInputModal.tsx b/examples/demo/src/components/modals/MultiPairInputModal.tsx index 03a5f5f..9a96b6b 100644 --- a/examples/demo/src/components/modals/MultiPairInputModal.tsx +++ b/examples/demo/src/components/modals/MultiPairInputModal.tsx @@ -4,7 +4,10 @@ import { MdClose } from 'react-icons/md'; import ModalShell from './ModalShell'; -type Row = { key: string; value: string }; +type Row = { id: number; key: string; value: string }; + +let nextRowId = 0; +const createRow = (): Row => ({ id: nextRowId++, key: '', value: '' }); interface MultiPairInputModalProps { open: boolean; @@ -23,11 +26,11 @@ const MultiPairInputModal: FC = ({ onClose, onSubmit, }) => { - const [rows, setRows] = useState([{ key: '', value: '' }]); + const [rows, setRows] = useState(() => [createRow()]); useEffect(() => { if (open) { - setRows([{ key: '', value: '' }]); + setRows([createRow()]); } }, [open]); @@ -57,14 +60,14 @@ const MultiPairInputModal: FC = ({ >

{title}

{rows.map((row, index) => ( -
+
setRows((prev) => - prev.map((entry, entryIndex) => - entryIndex === index ? { ...entry, key: event.target.value } : entry, + prev.map((entry) => + entry.id === row.id ? { ...entry, key: event.target.value } : entry, ), ) } @@ -75,8 +78,8 @@ const MultiPairInputModal: FC = ({ value={row.value} onChange={(event) => setRows((prev) => - prev.map((entry, entryIndex) => - entryIndex === index ? { ...entry, value: event.target.value } : entry, + prev.map((entry) => + entry.id === row.id ? { ...entry, value: event.target.value } : entry, ), ) } @@ -87,9 +90,7 @@ const MultiPairInputModal: FC = ({ @@ -101,7 +102,7 @@ const MultiPairInputModal: FC = ({ @@ -101,7 +102,7 @@ const MultiPairInputModal: FC = ({