fix: repair supplier modal submit flow

destroyOnClose unmounts the Form on close, disconnecting the useForm
instance so validateFields throws on reopen. Replaced with afterOpenChange
to reset fields without unmounting.

Also moved setModalOpen(false) and reload() before message.success so that
antd v5's static message API throwing outside an <App> context no longer
prevents the modal from closing after a successful create or update.
This commit is contained in:
kae 2026-06-18 18:24:51 +09:00
parent a713f944b6
commit 391ea29f1a

View File

@ -30,15 +30,24 @@ const SuppliersPage: React.FC = () => {
const handleSubmit = async () => { const handleSubmit = async () => {
const values = await form.validateFields(); const values = await form.validateFields();
if (editing) { let successMsg = '';
await updateSupplier(editing.supplierId, values); try {
message.success('更新成功'); if (editing) {
} else { const res = await updateSupplier(editing.supplierId, values);
await createSupplier(values); if (res?.code !== 0) { message.error(res?.msg || '更新失败'); return; }
message.success('创建成功'); successMsg = '更新成功';
} else {
const res = await createSupplier(values);
if (res?.code !== 0) { message.error(res?.msg || '创建失败'); return; }
successMsg = '创建成功';
}
} catch {
message.error('操作失败');
return;
} }
setModalOpen(false); setModalOpen(false);
actionRef.current?.reload(); actionRef.current?.reload();
message.success(successMsg);
}; };
const handleDelete = async (id: string) => { const handleDelete = async (id: string) => {
@ -103,7 +112,7 @@ const SuppliersPage: React.FC = () => {
open={modalOpen} open={modalOpen}
onOk={handleSubmit} onOk={handleSubmit}
onCancel={() => setModalOpen(false)} onCancel={() => setModalOpen(false)}
destroyOnClose afterOpenChange={(open) => { if (!open) form.resetFields(); }}
> >
<Form form={form} layout="vertical"> <Form form={form} layout="vertical">
<Form.Item name="supplierName" label="供应商名称" rules={[{ required: true, message: '请输入供应商名称' }]}> <Form.Item name="supplierName" label="供应商名称" rules={[{ required: true, message: '请输入供应商名称' }]}>