Skip to content

Commit

Permalink
fix(core): fix array nested remove (#1989)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Aug 11, 2021
1 parent 1b72d11 commit 2e06559
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
73 changes: 73 additions & 0 deletions packages/core/src/__tests__/array.spec.ts
Expand Up @@ -394,3 +394,76 @@ test('array field remove memo leak', async () => {
expect(handler).toBeCalledTimes(1)
expect(valuesChange).toBeCalledTimes(4)
})

test('nest array remove', async () => {
const form = attach(createForm())

const metrics = attach(
form.createArrayField({
name: 'metrics',
})
)

attach(
form.createObjectField({
name: '0',
basePath: 'metrics',
})
)

attach(
form.createObjectField({
name: '1',
basePath: 'metrics',
})
)

attach(
form.createArrayField({
name: 'content',
basePath: 'metrics.0',
})
)

attach(
form.createArrayField({
name: 'content',
basePath: 'metrics.1',
})
)

attach(
form.createObjectField({
name: '0',
basePath: 'metrics.0.content',
})
)

attach(
form.createObjectField({
name: '0',
basePath: 'metrics.1.content',
})
)

attach(
form.createField({
name: 'attr',
basePath: 'metrics.0.content.0',
initialValue: '123',
})
)

attach(
form.createField({
name: 'attr',
basePath: 'metrics.1.content.0',
initialValue: '123',
})
)

await (form.query('metrics.1.content').take() as any).remove(0)
expect(form.fields['metrics.0.content.0.attr']).not.toBeUndefined()
await metrics.remove(1)
expect(form.fields['metrics.0.content.0.attr']).not.toBeUndefined()
})
11 changes: 6 additions & 5 deletions packages/core/src/shared/internals.ts
Expand Up @@ -255,11 +255,12 @@ export const spliceArrayState = (
const number = afterStr.match(/^\.(\d+)/)?.[1]
if (number === undefined) return false
const index = Number(number)
const target = `${preStr}${afterStr.replace(
/^\.\d+/,
`.${index + deleteCount}`
)}`
return !fields[target]
return (
index >= startIndex &&
!fields[
`${preStr}${afterStr.replace(/^\.\d+/, `.${index + deleteCount}`)}`
]
)
}
const moveIndex = (identifier: string) => {
if (offset === 0) return identifier
Expand Down

0 comments on commit 2e06559

Please sign in to comment.