Skip to content

Commit a218946

Browse files
authored
fix(core): make exchangeArrayState be right when move (#2357)
* fix(core): make exchangeArrayState be right when move * fix(chore): old unit logic error
1 parent e878103 commit a218946

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

packages/core/src/__tests__/array.spec.ts

+57-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,61 @@ test('array field children state exchanges', () => {
140140
expect(form.query('array.2.value').get('value')).toEqual(55)
141141
})
142142

143+
test('array field move up/down then fields move', () => {
144+
const form = attach(createForm())
145+
const array = attach(
146+
form.createArrayField({
147+
name: 'array',
148+
})
149+
)
150+
attach(
151+
form.createField({
152+
name: 'value',
153+
basePath: 'array.0',
154+
})
155+
)
156+
attach(
157+
form.createField({
158+
name: 'value',
159+
basePath: 'array.1',
160+
})
161+
)
162+
attach(
163+
form.createField({
164+
name: 'value',
165+
basePath: 'array.2',
166+
})
167+
)
168+
attach(
169+
form.createField({
170+
name: 'value',
171+
basePath: 'array.3',
172+
})
173+
)
174+
const line0 = form.fields['array.0.value']
175+
const line1 = form.fields['array.1.value']
176+
const line2 = form.fields['array.2.value']
177+
const line3 = form.fields['array.3.value']
178+
179+
array.push({ value: '0' }, { value: '1' }, { value: '2' }, { value: '3' })
180+
181+
array.move(0, 3)
182+
183+
// 1,2,3,0
184+
expect(form.fields['array.0.value']).toBe(line1)
185+
expect(form.fields['array.1.value']).toBe(line2)
186+
expect(form.fields['array.2.value']).toBe(line3)
187+
expect(form.fields['array.3.value']).toBe(line0)
188+
189+
array.move(3, 1)
190+
191+
// 1,0,2,3
192+
expect(form.fields['array.0.value']).toBe(line1)
193+
expect(form.fields['array.1.value']).toBe(line0)
194+
expect(form.fields['array.2.value']).toBe(line2)
195+
expect(form.fields['array.3.value']).toBe(line3)
196+
})
197+
143198
test('void children', () => {
144199
const form = attach(createForm())
145200
const array = attach(
@@ -356,8 +411,9 @@ test('array field move api with children', async () => {
356411
})
357412
)
358413
await array.move(0, 2)
359-
expect(form.fields['array.0.name']).not.toBeUndefined()
414+
expect(form.fields['array.0.name']).toBeUndefined()
360415
expect(form.fields['array.2.name']).toBeUndefined()
416+
expect(form.fields['array.1.name']).not.toBeUndefined()
361417
})
362418

363419
test('array field remove memo leak', async () => {

packages/core/src/shared/internals.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,24 @@ export const exchangeArrayState = (
423423
return identifier.indexOf(address) === 0 && identifier.length > addrLength
424424
}
425425

426-
const isFromOrToNode = (identifier: string) => {
426+
const isDown = fromIndex < toIndex
427+
428+
const isMoveNode = (identifier: string) => {
429+
const afterStr = identifier.slice(address.length)
430+
const number = afterStr.match(NumberIndexReg)?.[1]
431+
if (number === undefined) return false
432+
const index = Number(number)
433+
return isDown
434+
? index > fromIndex && index <= toIndex
435+
: index < fromIndex && index >= toIndex
436+
}
437+
438+
const isFromNode = (identifier: string) => {
427439
const afterStr = identifier.substring(addrLength)
428440
const number = afterStr.match(NumberIndexReg)?.[1]
429441
if (number === undefined) return false
430442
const index = Number(number)
431-
return index === toIndex || index === fromIndex
443+
return index === fromIndex
432444
}
433445

434446
const moveIndex = (identifier: string) => {
@@ -440,7 +452,7 @@ export const exchangeArrayState = (
440452
if (index === fromIndex) {
441453
index = toIndex
442454
} else {
443-
index = fromIndex
455+
index += isDown ? -1 : 1
444456
}
445457

446458
return `${preStr}${afterStr.replace(/^\.\d+/, `.${index}`)}`
@@ -449,7 +461,7 @@ export const exchangeArrayState = (
449461
batch(() => {
450462
each(fields, (field, identifier) => {
451463
if (isArrayChildren(identifier)) {
452-
if (isFromOrToNode(identifier)) {
464+
if (isMoveNode(identifier) || isFromNode(identifier)) {
453465
const newIdentifier = moveIndex(identifier)
454466
fieldPatches.push({
455467
type: 'update',

0 commit comments

Comments
 (0)