Skip to content

Commit

Permalink
Merge pull request alibaba-fusion#930 from jdkahn/issue/924-field
Browse files Browse the repository at this point in the history
fix(Field): use constructor not initValue fix alibaba-fusion#924
  • Loading branch information
jdkahn authored Aug 1, 2019
2 parents 57f7b2f + 3fe5cf7 commit 4b105e5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 33 additions & 1 deletion test/field/options-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 4b105e5

Please sign in to comment.