From 3fe5cf7b4f8743e81d9dd2c21874b006a76845b3 Mon Sep 17 00:00:00 2001 From: Justin Kahn Date: Fri, 26 Jul 2019 15:20:55 +0800 Subject: [PATCH] fix(Field): use constructor not initValue fix #924 --- src/field/index.js | 14 +++++++++++++- test/field/options-spec.js | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/field/index.js b/src/field/index.js index 58ba17ab92..032d52277d 100644 --- a/src/field/index.js +++ b/src/field/index.js @@ -134,7 +134,19 @@ class Field { } if (!('value' in field)) { - field.value = defaultValue; + if (parseName) { + const cachedValue = getIn(this.values, name); + field.value = + typeof cachedValue !== 'undefined' + ? cachedValue + : defaultValue; + } else { + const cachedValue = this.values[name]; + field.value = + typeof cachedValue !== 'undefined' + ? cachedValue + : defaultValue; + } } if (parseName && !getIn(this.values, name)) { this.values = setIn(this.values, name, field.value); diff --git a/test/field/options-spec.js b/test/field/options-spec.js index 4e25ed2762..6e840bfa37 100644 --- a/test/field/options-spec.js +++ b/test/field/options-spec.js @@ -308,7 +308,18 @@ describe('options', () => { field.init('input'); assert(field.getValue('input') === undefined); - }) + }); + + it('should set the value to constructor value even with initValue from init', function() { + const inputValue = 0; + const field = new Field(this, { + values: { + input: inputValue + }, + }); + field.init('input', {initValue: 1}); + assert.equal(field.getValue('input'), inputValue); + }); }); describe('should support parseName', () => { @@ -603,6 +614,27 @@ describe('options', () => { assert.equal(field.getValue('input.otherValue'), secondValue); }); }); + + it('should set the value to constructor value even with initValue from init', function() { + const fieldDefault = 0; + const initValue = 'other default value'; + const field = new Field(this, { + parseName: true, + values: { + input: { + myValue: fieldDefault + } + } + }); + + field.init('input.myValue', { initValue }); + + assert.deepEqual(field.getValues(), { + input: { + myValue: fieldDefault, + }, + }); + }); }); describe('should support autoValidate=false', () => {