Skip to content

Commit

Permalink
Merge pull request alibaba-fusion#1002 from bindoon/fix-field
Browse files Browse the repository at this point in the history
fix(Field): bug of resetToDefault withtou initValue. close alibaba-fusion#1001
  • Loading branch information
jdkahn authored Aug 13, 2019
2 parents 67f0d49 + a76b05c commit 530c7d3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,8 @@ class Field {
if (typeof initValue !== 'undefined') {
defaultValue = initValue;
} else if (typeof originalProps[defaultValueName] !== 'undefined') {
// here use typeof, in case of defaultValue={0}
defaultValue = originalProps[defaultValueName];
} else if (parseName) {
defaultValue = getIn(this.values, name);
} else if (this.values && typeof this.values[name] !== 'undefined') {
defaultValue = this.values[name];
}

Object.assign(field, {
Expand All @@ -121,18 +118,24 @@ class Field {
ref: originalProps.ref,
});

// Controlled Component
// Controlled Component, should alwasy equal props.value
if (valueName in originalProps) {
field.value = originalProps[valueName];

// When rerendering set the values
// When rerendering set the values from props.value
if (parseName) {
this.values = setIn(this.values, name, field.value);
} else {
this.values[name] = field.value;
}
}

// should get value from this.values
/**
* a new field (value not in field)
* step 1: get value from this.values
* step 2: from defaultValue
*/
if (!('value' in field)) {
if (parseName) {
const cachedValue = getIn(this.values, name);
Expand All @@ -148,6 +151,7 @@ class Field {
: defaultValue;
}
}

if (parseName && !getIn(this.values, name)) {
this.values = setIn(this.values, name, field.value);
} else if (!parseName && !this.values[name]) {
Expand Down
10 changes: 10 additions & 0 deletions test/field/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,16 @@ describe('field', () => {
field.resetToDefault('input');
assert.deepEqual(field.getValues(), { input: '4', input2: '33'});
});

it('should set only named value to `undefined` on `resetToDefaults()` if init without `initValue`', function() {
const field = new Field(this);
field.init('input');
field.setValue('input', 'a value');
field.init('input'); // simulation a rerender

field.resetToDefault();
assert.deepEqual(field.getValues(), { input: undefined});
});
});

it('remove', function(done) {
Expand Down
20 changes: 14 additions & 6 deletions test/field/options-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe('options', () => {
assert.equal(field.getValue('input'), undefined);
});

it('should reset `input` to constructor `values` after calling `resetToDefault`', function() {
it('should reset `input` to constructor `undefined` after calling `resetToDefault`', function() {
const fieldDefault = 'field default value';
const field = new Field(this, {
values: {
Expand All @@ -274,7 +274,7 @@ describe('options', () => {
});
field.init('input');
field.resetToDefault('input');
assert.equal(field.getValue('input'), fieldDefault);
assert.equal(field.getValue('input'), undefined);
});

it('should reset `input` to undefined when given `values` in constructor and call `reset`', function() {
Expand Down Expand Up @@ -589,12 +589,17 @@ describe('options', () => {
field.init('input.otherValue');
field.setValue('input.myValue', secondValue);
field.setValue('input.otherValue', secondValue);

// simulation rerender
field.init('input.myValue');
field.init('input.otherValue');

field.resetToDefault();
assert.equal(field.getValue('input.myValue'), fieldDefault);
assert.equal(field.getValue('input.otherValue'), fieldDefault);
assert.equal(field.getValue('input.myValue'), undefined);
assert.equal(field.getValue('input.otherValue'), undefined);
});

it('should reset `input` to undefined when given `values` in constructor and call `resetToDefault`', function() {
it('should reset `input.myValue` which inited to undefined when given `values` in constructor and call `resetToDefault`', function() {
const fieldDefault = 'field default value';
const secondValue = 'second';
const field = new Field(this, {
Expand All @@ -609,8 +614,11 @@ describe('options', () => {
field.init('input.myValue');
field.setValue('input.myValue', secondValue);
field.setValue('input.otherValue', secondValue);

field.init('input.myValue');

field.resetToDefault('input.myValue');
assert.equal(field.getValue('input.myValue'), fieldDefault);
assert.equal(field.getValue('input.myValue'), undefined);
assert.equal(field.getValue('input.otherValue'), secondValue);
});
});
Expand Down

0 comments on commit 530c7d3

Please sign in to comment.