Skip to content

Commit

Permalink
Merge pull request #898 from jdkahn/issue/897-field
Browse files Browse the repository at this point in the history
fix(Field): falsy values: fix #897
  • Loading branch information
youluna authored Jul 22, 2019
2 parents 016c13e + 3d1743d commit 629438d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
34 changes: 34 additions & 0 deletions test/field/options-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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, {
Expand Down

0 comments on commit 629438d

Please sign in to comment.