diff --git a/.codecov.yml b/.codecov.yml new file mode 100644 index 00000000000..b30565c612d --- /dev/null +++ b/.codecov.yml @@ -0,0 +1,9 @@ +coverage: + status: + project: + default: + threshold: 0.1% + patch: + default: + threshold: 0.1% + target: 95% diff --git a/packages/core/src/__tests__/array.spec.ts b/packages/core/src/__tests__/array.spec.ts index c7b239a1c3d..989c603a457 100644 --- a/packages/core/src/__tests__/array.spec.ts +++ b/packages/core/src/__tests__/array.spec.ts @@ -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() +}) diff --git a/packages/core/src/shared/internals.ts b/packages/core/src/shared/internals.ts index 5b96c3ee018..40049ed039b 100644 --- a/packages/core/src/shared/internals.ts +++ b/packages/core/src/shared/internals.ts @@ -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) @@ -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', @@ -344,7 +340,7 @@ export const exchangeArrayState = ( if (!fields[newIdentifier]) { fieldPatches.push({ type: 'remove', - address: moveIndex(newIdentifier), + address: identifier, }) } }