summaryrefslogtreecommitdiff
path: root/src/components/ModalSourceIP.tsx
diff options
context:
space:
mode:
authorLarvan2 <[email protected]>2026-03-15 15:01:57 +0800
committerLarvan2 <[email protected]>2026-03-15 15:01:57 +0800
commit0e420859f5f7011ba124c965d8319bf3bf4c5fe3 (patch)
tree2fc344b757e119ebae6e0b6243121fddba61603c /src/components/ModalSourceIP.tsx
parent17c4d2855ffb6914fcbece27367bafdd27a4c182 (diff)
refactor: reorganize code
Diffstat (limited to 'src/components/ModalSourceIP.tsx')
-rw-r--r--src/components/ModalSourceIP.tsx19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/components/ModalSourceIP.tsx b/src/components/ModalSourceIP.tsx
index a406601..accb846 100644
--- a/src/components/ModalSourceIP.tsx
+++ b/src/components/ModalSourceIP.tsx
@@ -10,8 +10,19 @@ import s from './ModalSourceIP.module.scss';
export default function ModalSourceIP({ isOpen, onRequestClose, sourceMap, setSourceMap }) {
const { t } = useTranslation();
const setSource = (key, index, val) => {
- sourceMap[index][key] = val;
- setSourceMap(Array.from(sourceMap));
+ setSourceMap((prev) =>
+ prev.map((source, currentIndex) =>
+ currentIndex === index ? { ...source, [key]: val } : source
+ )
+ );
+ };
+
+ const removeSource = (index) => {
+ setSourceMap((prev) => prev.filter((_, currentIndex) => currentIndex !== index));
+ };
+
+ const addSource = () => {
+ setSourceMap((prev) => [...prev, { reg: '', name: '' }]);
};
return (
@@ -45,7 +56,7 @@ export default function ModalSourceIP({ isOpen, onRequestClose, sourceMap, setSo
/>
</td>
<td>
- <Button onClick={() => sourceMap.splice(index, 1)}>{t('delete')}</Button>
+ <Button onClick={() => removeSource(index)}>{t('delete')}</Button>
</td>
</tr>
))}
@@ -53,7 +64,7 @@ export default function ModalSourceIP({ isOpen, onRequestClose, sourceMap, setSo
</table>
<div>
<div className={s.iptableTipContainer}>{t('sourceip_tip')}</div>
- <Button onClick={() => sourceMap.push({ reg: '', name: '' })}>{t('add_tag')}</Button>
+ <Button onClick={addSource}>{t('add_tag')}</Button>
</div>
</BaseModal>
);