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
25 changes: 13 additions & 12 deletions examples/demo/src/components/modals/MultiPairInputModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,11 +26,11 @@ const MultiPairInputModal: FC<MultiPairInputModalProps> = ({
onClose,
onSubmit,
}) => {
const [rows, setRows] = useState<Row[]>([{ key: '', value: '' }]);
const [rows, setRows] = useState<Row[]>(() => [createRow()]);

useEffect(() => {
if (open) {
setRows([{ key: '', value: '' }]);
setRows([createRow()]);
}
}, [open]);

Expand Down Expand Up @@ -57,14 +60,14 @@ const MultiPairInputModal: FC<MultiPairInputModalProps> = ({
>
<h3>{title}</h3>
{rows.map((row, index) => (
<div key={`row-${index}`}>
<div key={row.id}>
<div className="inline-fields row-with-remove">
<input
value={row.key}
onChange={(event) =>
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,
),
)
}
Expand All @@ -75,8 +78,8 @@ const MultiPairInputModal: FC<MultiPairInputModalProps> = ({
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,
),
)
}
Expand All @@ -87,9 +90,7 @@ const MultiPairInputModal: FC<MultiPairInputModalProps> = ({
<button
type="button"
className="delete-btn"
onClick={() =>
setRows((prev) => prev.filter((_, entryIndex) => entryIndex !== index))
}
onClick={() => setRows((prev) => prev.filter((entry) => entry.id !== row.id))}
>
<MdClose />
</button>
Expand All @@ -101,7 +102,7 @@ const MultiPairInputModal: FC<MultiPairInputModalProps> = ({
<button
type="button"
className="text-btn text-btn-center"
onClick={() => setRows((prev) => [...prev, { key: '', value: '' }])}
onClick={() => setRows((prev) => [...prev, createRow()])}
data-testid="multipair_add_row_button"
>
+ Add Row
Expand Down
25 changes: 13 additions & 12 deletions examples/demo_pods/src/components/modals/MultiPairInputModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,11 +26,11 @@ const MultiPairInputModal: FC<MultiPairInputModalProps> = ({
onClose,
onSubmit,
}) => {
const [rows, setRows] = useState<Row[]>([{ key: '', value: '' }]);
const [rows, setRows] = useState<Row[]>(() => [createRow()]);

useEffect(() => {
if (open) {
setRows([{ key: '', value: '' }]);
setRows([createRow()]);
}
}, [open]);

Expand Down Expand Up @@ -57,14 +60,14 @@ const MultiPairInputModal: FC<MultiPairInputModalProps> = ({
>
<h3>{title}</h3>
{rows.map((row, index) => (
<div key={`row-${index}`}>
<div key={row.id}>
<div className="inline-fields row-with-remove">
<input
value={row.key}
onChange={(event) =>
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,
),
)
}
Expand All @@ -75,8 +78,8 @@ const MultiPairInputModal: FC<MultiPairInputModalProps> = ({
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,
),
)
}
Expand All @@ -87,9 +90,7 @@ const MultiPairInputModal: FC<MultiPairInputModalProps> = ({
<button
type="button"
className="delete-btn"
onClick={() =>
setRows((prev) => prev.filter((_, entryIndex) => entryIndex !== index))
}
onClick={() => setRows((prev) => prev.filter((entry) => entry.id !== row.id))}
>
<MdClose />
</button>
Expand All @@ -101,7 +102,7 @@ const MultiPairInputModal: FC<MultiPairInputModalProps> = ({
<button
type="button"
className="text-btn text-btn-center"
onClick={() => setRows((prev) => [...prev, { key: '', value: '' }])}
onClick={() => setRows((prev) => [...prev, createRow()])}
data-testid="multipair_add_row_button"
>
+ Add Row
Expand Down
Loading