refactor(admin): 用已安装的 file-saver/fast-deep-equal 替代手写下载与 JSON 比较
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { useCallback, useRef } from 'react';
|
||||
import { App } from 'antd';
|
||||
import type { FormInstance } from 'antd';
|
||||
import equal from 'fast-deep-equal';
|
||||
|
||||
/**
|
||||
* 弹窗「未保存内容」保护:关闭弹窗时若表单值已被修改,先确认再关闭,
|
||||
@@ -15,16 +16,16 @@ import type { FormInstance } from 'antd';
|
||||
*/
|
||||
export function useDirtyGuard(form: FormInstance) {
|
||||
const { modal } = App.useApp();
|
||||
const pristineRef = useRef<string>('');
|
||||
const pristineRef = useRef<unknown>(null);
|
||||
|
||||
/** 记录当前表单值为「未修改」基准;打开弹窗/回填后调用 */
|
||||
const snapshot = useCallback(() => {
|
||||
pristineRef.current = JSON.stringify(form.getFieldsValue());
|
||||
pristineRef.current = form.getFieldsValue();
|
||||
}, [form]);
|
||||
|
||||
/** 表单是否有未保存修改(与 snapshot 时对比) */
|
||||
const isDirty = useCallback(() => {
|
||||
return JSON.stringify(form.getFieldsValue()) !== pristineRef.current;
|
||||
return !equal(form.getFieldsValue(), pristineRef.current);
|
||||
}, [form]);
|
||||
|
||||
const confirmClose = useCallback(
|
||||
|
||||
Reference in New Issue
Block a user