Skip to content

Commit

Permalink
fix(core): make exchangeArrayState be right when move (#2357)
Browse files Browse the repository at this point in the history
* fix(core): make exchangeArrayState be right when move

* fix(chore): old unit logic error
  • Loading branch information
xyuanbuilds authored Oct 27, 2021
1 parent e878103 commit a218946
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
58 changes: 57 additions & 1 deletion packages/core/src/__tests__/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,61 @@ test('array field children state exchanges', () => {
expect(form.query('array.2.value').get('value')).toEqual(55)
})

test('array field move up/down then fields move', () => {
const form = attach(createForm())
const array = attach(
form.createArrayField({
name: 'array',
})
)
attach(
form.createField({
name: 'value',
basePath: 'array.0',
})
)
attach(
form.createField({
name: 'value',
basePath: 'array.1',
})
)
attach(
form.createField({
name: 'value',
basePath: 'array.2',
})
)
attach(
form.createField({
name: 'value',
basePath: 'array.3',
})
)
const line0 = form.fields['array.0.value']
const line1 = form.fields['array.1.value']
const line2 = form.fields['array.2.value']
const line3 = form.fields['array.3.value']

array.push({ value: '0' }, { value: '1' }, { value: '2' }, { value: '3' })

array.move(0, 3)

// 1,2,3,0
expect(form.fields['array.0.value']).toBe(line1)
expect(form.fields['array.1.value']).toBe(line2)
expect(form.fields['array.2.value']).toBe(line3)
expect(form.fields['array.3.value']).toBe(line0)

array.move(3, 1)

// 1,0,2,3
expect(form.fields['array.0.value']).toBe(line1)
expect(form.fields['array.1.value']).toBe(line0)
expect(form.fields['array.2.value']).toBe(line2)
expect(form.fields['array.3.value']).toBe(line3)
})

test('void children', () => {
const form = attach(createForm())
const array = attach(
Expand Down Expand Up @@ -356,8 +411,9 @@ test('array field move api with children', async () => {
})
)
await array.move(0, 2)
expect(form.fields['array.0.name']).not.toBeUndefined()
expect(form.fields['array.0.name']).toBeUndefined()
expect(form.fields['array.2.name']).toBeUndefined()
expect(form.fields['array.1.name']).not.toBeUndefined()
})

test('array field remove memo leak', async () => {
Expand Down
20 changes: 16 additions & 4 deletions packages/core/src/shared/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,24 @@ export const exchangeArrayState = (
return identifier.indexOf(address) === 0 && identifier.length > addrLength
}

const isFromOrToNode = (identifier: string) => {
const isDown = fromIndex < toIndex

const isMoveNode = (identifier: string) => {
const afterStr = identifier.slice(address.length)
const number = afterStr.match(NumberIndexReg)?.[1]
if (number === undefined) return false
const index = Number(number)
return isDown
? index > fromIndex && index <= toIndex
: index < fromIndex && index >= toIndex
}

const isFromNode = (identifier: string) => {
const afterStr = identifier.substring(addrLength)
const number = afterStr.match(NumberIndexReg)?.[1]
if (number === undefined) return false
const index = Number(number)
return index === toIndex || index === fromIndex
return index === fromIndex
}

const moveIndex = (identifier: string) => {
Expand All @@ -440,7 +452,7 @@ export const exchangeArrayState = (
if (index === fromIndex) {
index = toIndex
} else {
index = fromIndex
index += isDown ? -1 : 1
}

return `${preStr}${afterStr.replace(/^\.\d+/, `.${index}`)}`
Expand All @@ -449,7 +461,7 @@ export const exchangeArrayState = (
batch(() => {
each(fields, (field, identifier) => {
if (isArrayChildren(identifier)) {
if (isFromOrToNode(identifier)) {
if (isMoveNode(identifier) || isFromNode(identifier)) {
const newIdentifier = moveIndex(identifier)
fieldPatches.push({
type: 'update',
Expand Down

0 comments on commit a218946

Please sign in to comment.