-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(core): nest array remove initial-value logic in destory #4052
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -514,6 +514,7 @@ test('array field remove memo leak', async () => { | |
expect(initialValuesChange).toBeCalledTimes(0) | ||
}) | ||
|
||
// add sandbox https://codesandbox.io/p/devbox/lingering-violet-jwr565 | ||
test('nest array remove', async () => { | ||
const form = attach(createForm()) | ||
|
||
|
@@ -588,11 +589,84 @@ test('nest array remove', async () => { | |
expect(form.fields['metrics.0.content.0.attr']).not.toBeUndefined() | ||
await metrics.remove(1) | ||
expect(form.fields['metrics.0.content.0.attr']).not.toBeUndefined() | ||
// TODO!! 测试不通过 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是按照我当前的修复方案,form.initialValues 会被重置, 所以这个测试语句不通过 |
||
expect( | ||
form.initialValues.metrics?.[1]?.content?.[0]?.attr | ||
).not.toBeUndefined() | ||
}) | ||
|
||
|
||
test('nest array remove for #4024', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 情况二:针对 #4024 中反馈的问题, 我补充了一个测试用例 |
||
const form = attach(createForm()) | ||
const arr1 = attach( | ||
form.createArrayField({ | ||
name: 'aa', | ||
initialValue: [{}], | ||
}) | ||
) | ||
|
||
attach( | ||
form.createArrayField({ | ||
name: 'bb', | ||
basePath: 'aa.0', | ||
initialValue: [{}], | ||
}) | ||
) | ||
|
||
attach( | ||
form.createField({ | ||
name: 'cc', | ||
basePath: 'aa.0.bb.0', | ||
initialValue: true, | ||
}) | ||
) | ||
|
||
expect(form.initialValues).toEqual({ | ||
aa: [{ bb: [{ cc: true }] }], | ||
}) | ||
|
||
// 模拟两次 antd/ArrayBase.Addation 点击 | ||
attach( | ||
form.createField({ | ||
name: 'cc', | ||
basePath: 'aa.0.bb.1', | ||
initialValue: true, | ||
}) | ||
) | ||
attach( | ||
form.createField({ | ||
name: 'cc', | ||
basePath: 'aa.0.bb.2', | ||
initialValue: true, | ||
}) | ||
) | ||
// 符合 formily DevTools 表现 | ||
expect(form.initialValues).toEqual({ | ||
aa: [{ bb: [{ cc: true }, { cc: true }, { cc: true }] }], | ||
}) | ||
// 模拟 antd/ArrayBase.Remove 点击 | ||
arr1.remove(0) | ||
|
||
// 模拟一次外部数组点击 antd/ArrayBase.Addation 点击 | ||
attach( | ||
form.createArrayField({ | ||
name: 'bb', | ||
basePath: 'aa.0', | ||
initialValue: [{}], | ||
}) | ||
) | ||
attach( | ||
form.createField({ | ||
name: 'cc', | ||
basePath: 'aa.0.bb.0', | ||
initialValue: true, | ||
}) | ||
) | ||
expect(form.initialValues).toEqual({ | ||
aa: [{ bb: [{ cc: true }] }], | ||
}) | ||
}) | ||
|
||
test('indexes: nest path need exclude incomplete number', () => { | ||
const form = attach(createForm()) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,11 +176,17 @@ export const destroy = ( | |
) => { | ||
const field = target[address] | ||
field?.dispose() | ||
if (isDataField(field) && forceClear) { | ||
if (isDataField(field)) { | ||
const form = field.form | ||
const path = field.path | ||
form.deleteValuesIn(path) | ||
form.deleteInitialValuesIn(path) | ||
if (forceClear) { | ||
form.deleteValuesIn(path) | ||
} | ||
// 在 schema 上有定义 initialValue (JSX prop name: default) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 当前方案: 根据 schema 上的initialValues,在销毁时对 form.initialValues 进行现场恢复 |
||
const shouldClearInitial = forceClear || !isUndef(field.props.initialValue); | ||
if (shouldClearInitial) { | ||
form.deleteInitialValuesIn(path) | ||
} | ||
} | ||
delete target[address] | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
场景一:嵌套数组删除逻辑测试代码, 其中深层的数组的 initialValue 属性会改变 form.initialValues 值,针对这个测试, 我补充了一个 sandbox