Skip to content

Commit bc99462

Browse files
authored
fix(core): fix array exchange with children (#1697)
1 parent a6b8104 commit bc99462

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

.codecov.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
coverage:
2+
status:
3+
project:
4+
default:
5+
threshold: 0.1%
6+
patch:
7+
default:
8+
threshold: 0.1%
9+
target: 95%

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,44 @@ test('fault tolerance', () => {
240240
array2.moveDown(1)
241241
array2.moveDown(2)
242242
})
243+
244+
test('array field move api with children', async () => {
245+
const form = attach(createForm())
246+
attach(
247+
form.createField({
248+
name: 'other',
249+
})
250+
)
251+
const array = attach(
252+
form.createArrayField({
253+
name: 'array',
254+
})
255+
)
256+
attach(
257+
form.createArrayField({
258+
name: '0',
259+
basePath: 'array',
260+
})
261+
)
262+
attach(
263+
form.createArrayField({
264+
name: '1',
265+
basePath: 'array',
266+
})
267+
)
268+
attach(
269+
form.createArrayField({
270+
name: '2',
271+
basePath: 'array',
272+
})
273+
)
274+
attach(
275+
form.createArrayField({
276+
name: 'name',
277+
basePath: 'array.2',
278+
})
279+
)
280+
await array.move(0, 2)
281+
expect(form.fields['array.0.name']).not.toBeUndefined()
282+
expect(form.fields['array.2.name']).toBeUndefined()
283+
})

packages/core/src/shared/internals.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,16 +303,15 @@ export const exchangeArrayState = (
303303
identifier.indexOf(address) === 0 && identifier.length > address.length
304304
)
305305
}
306-
const isCrossNode = (identifier: string) => {
306+
307+
const isFromOrToNode = (identifier: string) => {
307308
const afterStr = identifier.slice(address.length)
308309
const number = afterStr.match(/^\.(\d+)/)?.[1]
309310
if (number === undefined) return false
310311
const index = Number(number)
311-
return (
312-
(index <= toIndex && index >= fromIndex) ||
313-
(index >= toIndex && index <= fromIndex)
314-
)
312+
return index === toIndex || index === fromIndex
315313
}
314+
316315
const moveIndex = (identifier: string) => {
317316
const preStr = identifier.slice(0, address.length)
318317
const afterStr = identifier.slice(address.length)
@@ -322,19 +321,16 @@ export const exchangeArrayState = (
322321
if (index === fromIndex) {
323322
index = toIndex
324323
} else {
325-
if (fromIndex < toIndex) {
326-
index--
327-
} else {
328-
index++
329-
}
324+
index = fromIndex
330325
}
331326

332327
return `${preStr}${afterStr.replace(/^\.\d+/, `.${index}`)}`
333328
}
329+
334330
batch(() => {
335331
each(fields, (field, identifier) => {
336332
if (isArrayChildren(identifier)) {
337-
if (isCrossNode(identifier)) {
333+
if (isFromOrToNode(identifier)) {
338334
const newIdentifier = moveIndex(identifier)
339335
fieldPatches.push({
340336
type: 'update',
@@ -344,7 +340,7 @@ export const exchangeArrayState = (
344340
if (!fields[newIdentifier]) {
345341
fieldPatches.push({
346342
type: 'remove',
347-
address: moveIndex(newIdentifier),
343+
address: identifier,
348344
})
349345
}
350346
}

0 commit comments

Comments
 (0)