Skip to content
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): initial value should clear in destroy #4053

Open
wants to merge 1 commit into
base: formily_next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions packages/core/src/__tests__/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down Expand Up @@ -588,11 +589,83 @@ 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!! 测试不通过
expect(
form.initialValues.metrics?.[1]?.content?.[0]?.attr
).not.toBeUndefined()
})

test('nest array remove for #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())

Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,18 @@ 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)
const shouldClearInitial = forceClear || !isUndef(field.props.initialValue)
if (shouldClearInitial) {
form.deleteInitialValuesIn(path)
}
}
delete target[address]
}
Expand Down
Loading