From 1594ebcbec597b75ed4c47f78426058c66483cb5 Mon Sep 17 00:00:00 2001 From: SeqviriouM Date: Sat, 23 May 2020 01:54:59 +0300 Subject: [PATCH 1/3] fix(moveFieldState): get change/blur/focus callbacks from oldState --- src/moveFieldState.js | 12 ++++++------ src/remove.test.js | 27 +++++++++++++-------------- src/removeBatch.test.js | 30 +++++++++++++++--------------- src/swap.js | 5 ++--- 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/moveFieldState.js b/src/moveFieldState.js index c1219ea..136486d 100644 --- a/src/moveFieldState.js +++ b/src/moveFieldState.js @@ -14,19 +14,19 @@ function moveFieldState( // prevent functions from being overwritten // if the state.fields[destKey] does not exist, it will be created // when that field gets registered, with its own change/blur/focus callbacks - change: oldState.fields[destKey] && oldState.fields[destKey].change, - blur: oldState.fields[destKey] && oldState.fields[destKey].blur, - focus: oldState.fields[destKey] && oldState.fields[destKey].focus, + change: oldState.fields[source.name] && oldState.fields[source.name].change, + blur: oldState.fields[source.name] && oldState.fields[source.name].blur, + focus: oldState.fields[source.name] && oldState.fields[source.name].focus, lastFieldState: undefined // clearing lastFieldState forces renotification } if (!state.fields[destKey].change) { - delete state.fields[destKey].change; + delete state.fields[destKey].change } if (!state.fields[destKey].blur) { - delete state.fields[destKey].blur; + delete state.fields[destKey].blur } if (!state.fields[destKey].focus) { - delete state.fields[destKey].focus; + delete state.fields[destKey].focus } } diff --git a/src/remove.test.js b/src/remove.test.js index fd4a685..8746f77 100644 --- a/src/remove.test.js +++ b/src/remove.test.js @@ -137,18 +137,18 @@ describe('remove', () => { }, 'foo[1]': { name: 'foo[1]', - blur: blur1, - change: change1, - focus: focus1, + blur: blur2, + change: change2, + focus: focus2, touched: true, error: 'C Error', lastFieldState: undefined }, 'foo[2]': { name: 'foo[2]', - blur: blur2, - change: change2, - focus: focus2, + blur: blur3, + change: change3, + focus: focus3, touched: false, error: 'D Error', lastFieldState: undefined @@ -160,7 +160,6 @@ describe('remove', () => { } }) }) - it('should remove value from the specified index, and return it (nested arrays)', () => { const array = ['a', 'b', 'c', 'd'] @@ -249,18 +248,18 @@ describe('remove', () => { }, 'foo[0][1]': { name: 'foo[0][1]', - blur: blur1, - change: change1, - focus: focus1, + blur: blur2, + change: change2, + focus: focus2, touched: true, error: 'C Error', lastFieldState: undefined }, 'foo[0][2]': { name: 'foo[0][2]', - blur: blur2, - change: change2, - focus: focus2, + blur: blur3, + change: change3, + focus: focus3, touched: false, error: 'D Error', lastFieldState: undefined @@ -271,7 +270,7 @@ describe('remove', () => { } } }) - }) + }) it('should remove value from the specified index, and handle new fields', () => { const array = ['a', { key: 'val' }] diff --git a/src/removeBatch.test.js b/src/removeBatch.test.js index 15f0c8d..93957c6 100644 --- a/src/removeBatch.test.js +++ b/src/removeBatch.test.js @@ -170,9 +170,9 @@ describe('removeBatch', () => { fields: { 'foo[0]': { name: 'foo[0]', - blur: blur0, - change: change0, - focus: focus0, + blur: blur1, + change: change1, + focus: focus1, touched: false, error: 'Second Error', lastFieldState: undefined @@ -307,18 +307,18 @@ describe('removeBatch', () => { }, 'foo[1]': { name: 'foo[1]', - blur: blur1, - change: change1, - focus: focus1, + blur: blur3, + change: change3, + focus: focus3, touched: false, error: 'D Error', lastFieldState: undefined }, 'foo[2]': { name: 'foo[2]', - blur: blur2, - change: change2, - focus: focus2, + blur: blur4, + change: change4, + focus: focus4, touched: true, error: 'E Error', lastFieldState: undefined @@ -432,18 +432,18 @@ describe('removeBatch', () => { }, 'foo[0][1]': { name: 'foo[0][1]', - blur: blur1, - change: change1, - focus: focus1, + blur: blur3, + change: change3, + focus: focus3, touched: false, error: 'D Error', lastFieldState: undefined }, 'foo[0][2]': { name: 'foo[0][2]', - blur: blur2, - change: change2, - focus: focus2, + blur: blur4, + change: change4, + focus: focus4, touched: true, error: 'E Error', lastFieldState: undefined diff --git a/src/swap.js b/src/swap.js index e51fe25..43413c6 100644 --- a/src/swap.js +++ b/src/swap.js @@ -1,8 +1,7 @@ // @flow import type { MutableState, Mutator, Tools } from 'final-form' -import moveFieldState from './moveFieldState' -import moveFields from './moveFields'; -import restoreFunctions from './restoreFunctions'; +import moveFields from './moveFields' +import restoreFunctions from './restoreFunctions' const TMP: string = 'tmp' From d4fd76f25f8cf6daf764f34ca182badeaf3b73b5 Mon Sep 17 00:00:00 2001 From: Roman Date: Sat, 23 May 2020 16:48:25 +0300 Subject: [PATCH 2/3] fix: tune insert function to fully save backup --- src/insert.js | 4 ++-- src/unshift.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/insert.js b/src/insert.js index 7636cee..68f2535 100644 --- a/src/insert.js +++ b/src/insert.js @@ -14,7 +14,7 @@ const insert: Mutator = ( return copy }) - const backup = { ...state.fields } + const backup = { ...state, fields: { ...state.fields } } // now we have increment any higher indexes const pattern = new RegExp(`^${escapeRegexTokens(name)}\\[(\\d+)\\](.*)`) @@ -31,7 +31,7 @@ const insert: Mutator = ( if (fieldIndex >= index) { // inc index one higher const incrementedKey = `${name}[${fieldIndex + 1}]${tokens[2]}` - moveFieldState(state, backup[key], incrementedKey) + moveFieldState(state, backup.fields[key], incrementedKey, backup) } } }) diff --git a/src/unshift.test.js b/src/unshift.test.js index 0dbf91b..d205788 100644 --- a/src/unshift.test.js +++ b/src/unshift.test.js @@ -81,6 +81,15 @@ describe('unshift', () => { const resetFieldState = name => { state.fields[name].touched = false } + function blur0() {} + function change0() {} + function focus0() {} + function blur1() {} + function change1() {} + function focus1() {} + function blur2() {} + function change2() {} + function focus2() {} const state = { formState: { values: { @@ -90,16 +99,25 @@ describe('unshift', () => { fields: { 'foo[0]': { name: 'foo[0]', + blur: blur0, + change: change0, + focus: focus0, touched: true, error: 'A Error' }, 'foo[1]': { name: 'foo[1]', + blur: blur1, + change: change1, + focus: focus1, touched: false, error: 'B Error' }, 'foo[2]': { name: 'foo[2]', + blur: blur2, + change: change2, + focus: focus2, touched: true, error: 'C Error' } @@ -120,18 +138,27 @@ describe('unshift', () => { fields: { 'foo[1]': { name: 'foo[1]', + blur: blur0, + change: change0, + focus: focus0, touched: true, error: 'A Error', lastFieldState: undefined }, 'foo[2]': { name: 'foo[2]', + blur: blur1, + change: change1, + focus: focus1, touched: false, error: 'B Error', lastFieldState: undefined }, 'foo[3]': { name: 'foo[3]', + blur: blur2, + change: change2, + focus: focus2, touched: true, error: 'C Error', lastFieldState: undefined From f0988133789a39f2993bc6d25d678313a201136e Mon Sep 17 00:00:00 2001 From: Roman Date: Sat, 23 May 2020 16:56:58 +0300 Subject: [PATCH 3/3] feat(insert): tune tests to check callbacks after inserting --- src/insert.test.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/insert.test.js b/src/insert.test.js index d35d094..09347fd 100644 --- a/src/insert.test.js +++ b/src/insert.test.js @@ -94,6 +94,18 @@ describe('insert', () => { const resetFieldState = name => { state.fields[name].touched = false } + function blur0() {} + function change0() {} + function focus0() {} + function blur1() {} + function change1() {} + function focus1() {} + function blur2() {} + function change2() {} + function focus2() {} + function blur3() {} + function change3() {} + function focus3() {} const state = { formState: { values: { @@ -103,21 +115,33 @@ describe('insert', () => { fields: { 'foo[0]': { name: 'foo[0]', + blur: blur0, + change: change0, + focus: focus0, touched: true, error: 'A Error' }, 'foo[1]': { name: 'foo[1]', + blur: blur1, + change: change1, + focus: focus1, touched: true, error: 'B Error' }, 'foo[2]': { name: 'foo[2]', + blur: blur2, + change: change2, + focus: focus2, touched: true, error: 'C Error' }, 'foo[3]': { name: 'foo[3]', + blur: blur3, + change: change3, + focus: focus3, touched: false, error: 'D Error' } @@ -138,23 +162,35 @@ describe('insert', () => { fields: { 'foo[0]': { name: 'foo[0]', + blur: blur0, + change: change0, + focus: focus0, touched: true, error: 'A Error' }, 'foo[2]': { name: 'foo[2]', + blur: blur1, + change: change1, + focus: focus1, touched: true, error: 'B Error', lastFieldState: undefined }, 'foo[3]': { name: 'foo[3]', + blur: blur2, + change: change2, + focus: focus2, touched: true, error: 'C Error', lastFieldState: undefined }, 'foo[4]': { name: 'foo[4]', + blur: blur3, + change: change3, + focus: focus3, touched: false, error: 'D Error', lastFieldState: undefined @@ -174,6 +210,18 @@ describe('insert', () => { const resetFieldState = name => { state.fields[name].touched = false } + function blur0() {} + function change0() {} + function focus0() {} + function blur1() {} + function change1() {} + function focus1() {} + function blur2() {} + function change2() {} + function focus2() {} + function blur3() {} + function change3() {} + function focus3() {} const state = { formState: { values: { @@ -183,21 +231,33 @@ describe('insert', () => { fields: { 'foo[0][0]': { name: 'foo[0][0]', + blur: blur0, + change: change0, + focus: focus0, touched: true, error: 'A Error' }, 'foo[0][1]': { name: 'foo[0][1]', + blur: blur1, + change: change1, + focus: focus1, touched: true, error: 'B Error' }, 'foo[0][2]': { name: 'foo[0][2]', + blur: blur2, + change: change2, + focus: focus2, touched: true, error: 'C Error' }, 'foo[0][3]': { name: 'foo[0][3]', + blur: blur3, + change: change3, + focus: focus3, touched: false, error: 'D Error' } @@ -218,23 +278,35 @@ describe('insert', () => { fields: { 'foo[0][0]': { name: 'foo[0][0]', + blur: blur0, + change: change0, + focus: focus0, touched: true, error: 'A Error' }, 'foo[0][2]': { name: 'foo[0][2]', + blur: blur1, + change: change1, + focus: focus1, touched: true, error: 'B Error', lastFieldState: undefined }, 'foo[0][3]': { name: 'foo[0][3]', + blur: blur2, + change: change2, + focus: focus2, touched: true, error: 'C Error', lastFieldState: undefined }, 'foo[0][4]': { name: 'foo[0][4]', + blur: blur3, + change: change3, + focus: focus3, touched: false, error: 'D Error', lastFieldState: undefined