Skip to content

Commit

Permalink
fix(core): fix array path calculation #1533
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Jun 1, 2021
1 parent ef49b45 commit 2924900
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 5 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"node": "8.x || 9.x || 10.x || 11.x"
},
"workspaces": [
"packages/*",
"designable/*"
"packages/*"
],
"scripts": {
"bootstrap": "lerna bootstrap",
Expand Down
115 changes: 115 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,3 +964,118 @@ test('initialValue', () => {
expect(field.value).toEqual(123)
expect(field.initialValue).toEqual(123)
})

test('array path calculation with none index', async () => {
const form = attach(createForm())
const array = attach(
form.createArrayField({
name: 'array',
})
)
await array.push({})
const input = attach(
form.createField({
name: '0.input',
basePath: 'array',
})
)
expect(input.path.toString()).toEqual('array.0.input')
})

test('array path calculation with none index and void nested', async () => {
const form = attach(createForm())
const array = attach(
form.createArrayField({
name: 'array',
})
)
await array.push({})
attach(
form.createVoidField({
name: '0.column',
basePath: 'array',
})
)
const input = attach(
form.createField({
name: 'input',
basePath: 'array.0.column',
})
)
expect(input.path.toString()).toEqual('array.0.input')
})

test('array path calculation with object index', async () => {
const form = attach(createForm())
const array = attach(
form.createArrayField({
name: 'array',
})
)
await array.push({})
attach(
form.createObjectField({
name: '0',
basePath: 'array',
})
)
const input = attach(
form.createField({
name: 'input',
basePath: 'array.0',
})
)
expect(input.path.toString()).toEqual('array.0.input')
})

test('array path calculation with void index', async () => {
const form = attach(createForm())
const array = attach(
form.createArrayField({
name: 'array',
})
)
await array.push('')
attach(
form.createVoidField({
name: '0',
basePath: 'array',
})
)
const input = attach(
form.createField({
name: 'input',
basePath: 'array.0',
})
)
expect(input.path.toString()).toEqual('array.0')
})

test('array path calculation with void index and void wrapper', async () => {
const form = attach(createForm())
attach(
form.createVoidField({
name: 'layout',
})
)
const array_in_layout = attach(
form.createArrayField({
name: 'array_in_layout',
basePath: 'layout',
})
)
await array_in_layout.push('')
attach(
form.createVoidField({
name: '0',
basePath: 'layout.array_in_layout',
})
)
const input = attach(
form.createField({
name: 'input',
basePath: 'layout.array_in_layout.0',
})
)
expect(input.path.toString()).toEqual('array_in_layout.0')
})
11 changes: 8 additions & 3 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isArr,
isPlainObj,
toArr,
isNumberLike,
} from '@formily/shared'
import { ValidatorTriggerType, validate } from '@formily/validator'
import { action, batch, toJS } from '@formily/reactive'
Expand Down Expand Up @@ -63,16 +64,20 @@ export const buildFieldPath = (field: GeneralField) => {
const currentPath = path.concat([key])
const currentAddress = field.address.slice(0, index + 1)
const current = field.form.fields[currentAddress.toString()]
if (prevArray) {
prevArray = false
return path
}
if (index >= field.address.length - 1) {
if (isVoidField(field)) {
return currentPath
}
if (prevArray) return path
return currentPath
}
if (isVoidField(current)) {
const parent = field.form.fields[path.toString()]
if (isArrayField(parent)) {
const parentAddress = field.address.slice(0, index)
const parent = field.form.fields[parentAddress.toString()]
if (isArrayField(parent) && isNumberLike(key)) {
prevArray = true
return currentPath
}
Expand Down

0 comments on commit 2924900

Please sign in to comment.