Skip to content

Commit

Permalink
fix(Field): fixes #880
Browse files Browse the repository at this point in the history
- set `values` cache when controlled component with `init` props
  • Loading branch information
jdkahn committed Jul 15, 2019
1 parent f093363 commit ebe1ea1
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ class Field {
// Controlled Component
if (valueName in originalProps) {
field.value = originalProps[valueName];

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

if (!('value' in field)) {
Expand Down
104 changes: 103 additions & 1 deletion test/field/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Enzyme.configure({ adapter: new Adapter() });
const FormItem = Form.Item;

/* eslint-disable react/jsx-filename-extension */
/*global describe it */
/*global describe it afterEach */
describe('field', () => {
describe('render', () => {
it('should support Form', function(done) {
Expand Down Expand Up @@ -158,6 +158,14 @@ describe('field', () => {
});
});
describe('init', () => {
let wrapper;
afterEach(() => {
if (wrapper) {
wrapper.unmount();
wrapper = null;
}
});

it('init(input)', function(done) {
const field = new Field(this);
const inited = field.init('input');
Expand Down Expand Up @@ -311,6 +319,100 @@ describe('field', () => {

done();
});

it('should support control through `setState`', function(done) {
class Demo extends React.Component {
state = {
show: true,
inputValue: 'start'
};
field = new Field(this);

render() {
const init = this.field.init;
return (
<div>
<Input {...init('input', {props: {value: this.state.inputValue}})} />{' '}
<button
id="set"
onClick={() => {
assert(
this.field.getValue('input') === 'start'
);
this.setState({
inputValue: 'end',
})
}}
>
click
</button>
<button
id="get"
onClick={() => {
assert(
this.field.getValue('input') === 'end'
);
done();
}}
>
click
</button>
</div>
);
}
}

wrapper = mount(<Demo />);
wrapper.find('#set').simulate('click');
wrapper.find('#get').simulate('click');
});

it('should support control through `setState` when `parseName` is true', function(done) {
class Demo extends React.Component {
state = {
show: true,
inputValue: 'start'
};
field = new Field(this, { parseName: true});

render() {
const init = this.field.init;
return (
<div>
<Input {...init('input', {props: {value: this.state.inputValue}})} />{' '}
<button
id="set"
onClick={() => {
assert(
this.field.getValue('input') === 'start'
);
this.setState({
inputValue: 'end',
})
}}
>
click
</button>
<button
id="get"
onClick={() => {
assert(
this.field.getValue('input') === 'end'
);
done();
}}
>
click
</button>
</div>
);
}
}

wrapper = mount(<Demo />);
wrapper.find('#set').simulate('click');
wrapper.find('#get').simulate('click');
})
});

describe('behaviour', () => {
Expand Down

0 comments on commit ebe1ea1

Please sign in to comment.