refactor: improve deep equals check

This commit is contained in:
chenos 2021-01-06 15:40:27 +08:00
parent 7a4e047910
commit 8274fbad21
3 changed files with 44 additions and 12 deletions

View File

@ -37,8 +37,10 @@
"@umijs/test": "^3.2.23",
"ahooks": "^2.9.3",
"array-move": "^3.0.1",
"clean-deep": "^3.4.0",
"concurrently": "^5.3.0",
"lint-staged": "^10.0.7",
"marked": "^1.2.7",
"nodemon": "^2.0.6",
"prettier": "^1.19.1",
"react": "16.14.0",

View File

@ -18,6 +18,7 @@ import { QuestionCircleOutlined } from '@ant-design/icons';
import { useRequest } from 'umi';
import api from '@/api-client';
import { Spin } from '@nocobase/client';
import cleanDeep from 'clean-deep';
const actions = createFormActions();
@ -54,16 +55,29 @@ export default forwardRef((props: any, ref) => {
width={'40%'}
onClose={() => {
actions.getFormState(state => {
if (isEqual(state.initialValues, state.values)) {
const values = cleanDeep(state.values);
const others = Object.keys(data).length ? cleanDeep({...data}) : cleanDeep(state.initialValues);
if (isEqual(values, others)) {
setVisible(false);
return;
}
for (const key in values) {
if (Object.prototype.hasOwnProperty.call(values, key)) {
const value = values[key];
const other = others[key];
if (!isEqual(value, other)) {
// console.log(value, other, values, others, state.initialValues);
Modal.confirm({
title: '表单内容发生变化,确定不保存吗?',
onOk() {
setVisible(false);
}
});
return;
}
}
}
setVisible(false);
});
}}
title={title}

View File

@ -20,6 +20,9 @@ import { useRequest } from 'umi';
import api from '@/api-client';
import { Spin } from '@nocobase/client';
import isEqual from 'lodash/isEqual';
import isEmpty from 'lodash/isEmpty';
import get from 'lodash/get';
import cleanDeep from 'clean-deep';
const actions = createFormActions();
@ -64,16 +67,29 @@ export const DrawerForm = forwardRef((props: any, ref) => {
className={'noco-drawer'}
onClose={() => {
actions.getFormState(state => {
if (isEqual(state.initialValues, state.values)) {
const values = cleanDeep(state.values);
const others = Object.keys(data).length ? cleanDeep({...data, associatedKey}) : cleanDeep(state.initialValues);
if (isEqual(values, others)) {
setVisible(false);
return;
}
for (const key in values) {
if (Object.prototype.hasOwnProperty.call(values, key)) {
const value = values[key];
const other = others[key];
if (!isEqual(value, other)) {
// console.log(value, other, values, others, state.initialValues);
Modal.confirm({
title: '表单内容发生变化,确定不保存吗?',
onOk() {
setVisible(false);
}
});
return;
}
}
}
setVisible(false);
});
}}
title={title}