diff --git a/src/field/index.js b/src/field/index.js index 0ec1831cf8..ec68c2d90d 100644 --- a/src/field/index.js +++ b/src/field/index.js @@ -103,12 +103,12 @@ class Field { let defaultValue; if (typeof initValue !== 'undefined') { defaultValue = initValue; - } else if (originalProps[defaultValueName]) { + } else if (typeof originalProps[defaultValueName] !== 'undefined') { defaultValue = originalProps[defaultValueName]; } else if (parseName) { defaultValue = getIn(this.values, name); - } else { - defaultValue = (this.values && this.values[name]) || undefined; + } else if (this.values && typeof this.values[name] !== 'undefined') { + defaultValue = this.values[name]; } Object.assign(field, { diff --git a/test/field/options-spec.js b/test/field/options-spec.js index 47d7ea271e..4e25ed2762 100644 --- a/test/field/options-spec.js +++ b/test/field/options-spec.js @@ -163,6 +163,29 @@ describe('options', () => { done(); }); + describe('defaultValue', () => { + it('should support `defaultValue`', function() { + const inputValue = 'my value'; + const field = new Field(this); + field.init('input', {props: {defaultValue: inputValue}}); + assert.equal(field.getValue('input'), inputValue); + }); + + it('should support `defaultValue` with different value name and make camel case', function() { + const inputValue = 'my value'; + const field = new Field(this); + field.init('input', { valueName: 'myValue', props: { defaultMyValue: inputValue} }); + assert.equal(field.getValue('input'), inputValue); + }); + + it('should support `defaultValue` with falsy value', function() { + const inputValue = 0; + const field = new Field(this); + field.init('input', {props: {defaultValue: inputValue}}); + assert.equal(field.getValue('input'), inputValue); + }); + }) + describe('values', () => { it('should set default field input values when given `values` in constructor', function() { const inputValue = 'my value'; @@ -174,6 +197,17 @@ describe('options', () => { assert.equal(field.getValue('input'), inputValue); }); + it('should set default field input values when given falsy `values` in constructor', function() { + const inputValue = 0; + const field = new Field(this, { + values: { + input: inputValue + }, + }); + field.init('input'); + assert.equal(field.getValue('input'), inputValue); + }); + it('should set default field input values when given `values` and `parseName` = true in constructor', function() { const inputValue = 'my value'; const field = new Field(this, {