diff --git a/src/insert.js b/src/insert.js index 89a0443..a3cbac1 100644 --- a/src/insert.js +++ b/src/insert.js @@ -6,7 +6,7 @@ import { escapeRegexTokens } from './utils' const insert: Mutator<any> = ( [name, index, value]: any[], state: MutableState<any>, - { changeValue }: Tools<any> + { changeValue, resetFieldState }: Tools<any> ) => { changeValue(state, name, (array: ?(any[])): any[] => { const copy = [...(array || [])] @@ -25,6 +25,10 @@ const insert: Mutator<any> = ( // Shift all higher indices up const incrementedKey = `${name}[${fieldIndex + 1}]${tokens[2]}` copyField(state.fields, key, newFields, incrementedKey) + if (fieldIndex === index) { + newFields[key] = state.fields[key] + resetFieldState(key) + } return } } diff --git a/src/insert.test.js b/src/insert.test.js index 41663fd..94992bd 100644 --- a/src/insert.test.js +++ b/src/insert.test.js @@ -154,6 +154,12 @@ describe('insert', () => { touched: true, error: 'A Error' }, + 'foo[1]': { + error: 'B Error', + name: 'foo[1]', + touched: false, + lastFieldState: undefined + }, 'foo[2]': { name: 'foo[2]', touched: true, @@ -234,6 +240,11 @@ describe('insert', () => { touched: true, error: 'A Error' }, + 'foo[0][1]': { + name: 'foo[0][1]', + touched: false, + error: 'B Error' + }, 'foo[0][2]': { name: 'foo[0][2]', touched: true, diff --git a/src/unshift.test.js b/src/unshift.test.js index 0dbf91b..483e847 100644 --- a/src/unshift.test.js +++ b/src/unshift.test.js @@ -118,6 +118,11 @@ describe('unshift', () => { } }, fields: { + 'foo[0]': { + error: 'A Error', + name: 'foo[0]', + touched: false + }, 'foo[1]': { name: 'foo[1]', touched: true,