Skip to content

Commit

Permalink
fix(core): fix array exchange with children (#1697)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Jun 30, 2021
1 parent a6b8104 commit bc99462
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
9 changes: 9 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
coverage:
status:
project:
default:
threshold: 0.1%
patch:
default:
threshold: 0.1%
target: 95%
41 changes: 41 additions & 0 deletions packages/core/src/__tests__/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,44 @@ test('fault tolerance', () => {
array2.moveDown(1)
array2.moveDown(2)
})

test('array field move api with children', async () => {
const form = attach(createForm())
attach(
form.createField({
name: 'other',
})
)
const array = attach(
form.createArrayField({
name: 'array',
})
)
attach(
form.createArrayField({
name: '0',
basePath: 'array',
})
)
attach(
form.createArrayField({
name: '1',
basePath: 'array',
})
)
attach(
form.createArrayField({
name: '2',
basePath: 'array',
})
)
attach(
form.createArrayField({
name: 'name',
basePath: 'array.2',
})
)
await array.move(0, 2)
expect(form.fields['array.0.name']).not.toBeUndefined()
expect(form.fields['array.2.name']).toBeUndefined()
})
20 changes: 8 additions & 12 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,15 @@ export const exchangeArrayState = (
identifier.indexOf(address) === 0 && identifier.length > address.length
)
}
const isCrossNode = (identifier: string) => {

const isFromOrToNode = (identifier: string) => {
const afterStr = identifier.slice(address.length)
const number = afterStr.match(/^\.(\d+)/)?.[1]
if (number === undefined) return false
const index = Number(number)
return (
(index <= toIndex && index >= fromIndex) ||
(index >= toIndex && index <= fromIndex)
)
return index === toIndex || index === fromIndex
}

const moveIndex = (identifier: string) => {
const preStr = identifier.slice(0, address.length)
const afterStr = identifier.slice(address.length)
Expand All @@ -322,19 +321,16 @@ export const exchangeArrayState = (
if (index === fromIndex) {
index = toIndex
} else {
if (fromIndex < toIndex) {
index--
} else {
index++
}
index = fromIndex
}

return `${preStr}${afterStr.replace(/^\.\d+/, `.${index}`)}`
}

batch(() => {
each(fields, (field, identifier) => {
if (isArrayChildren(identifier)) {
if (isCrossNode(identifier)) {
if (isFromOrToNode(identifier)) {
const newIdentifier = moveIndex(identifier)
fieldPatches.push({
type: 'update',
Expand All @@ -344,7 +340,7 @@ export const exchangeArrayState = (
if (!fields[newIdentifier]) {
fieldPatches.push({
type: 'remove',
address: moveIndex(newIdentifier),
address: identifier,
})
}
}
Expand Down

0 comments on commit bc99462

Please sign in to comment.