Skip to content

Commit

Permalink
fix(renderer): clearOnMount set clearedValue, not undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
rvsia committed Mar 5, 2020
1 parent e4c6550 commit 5939aa3
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/react-form-renderer/src/form-renderer/field-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ class FieldProvider extends Component{
}
}

fieldClearedValue = () => this.props.hasOwnProperty('clearedValue') ? this.props.clearedValue : this.props.formOptions.clearedValue

componentWillUnmount(){
if ((this.props.formOptions.clearOnUnmount || this.props.clearOnUnmount) && this.props.clearOnUnmount !== false) {
this.props.formOptions.change(this.props.name, undefined);
this.props.formOptions.change(this.props.name, this.fieldClearedValue());
}
}

render(){
const { clearOnUnmount, component, render, dataType, children, ...props } = this.props;
const fieldClearedValue = props.hasOwnProperty('clearedValue') ? props.clearedValue : props.formOptions.clearedValue;
const { clearedValue, ...fieldProps } = props;
if (component) {
const FieldComponent = component;
Expand All @@ -35,7 +36,7 @@ class FieldProvider extends Component{
...fieldsProps.meta,
dataType,
onChange,
clearedValue: fieldClearedValue,
clearedValue: this.fieldClearedValue(),
}, ...args);
},
}}
Expand All @@ -53,7 +54,7 @@ class FieldProvider extends Component{
...fieldsProps.meta,
dataType,
onChange,
clearedValue: fieldClearedValue,
clearedValue: this.fieldClearedValue(),
}, ...args),
},
}) } />;
Expand All @@ -71,7 +72,7 @@ class FieldProvider extends Component{
...fieldsProps.meta,
dataType,
onChange,
clearedValue: fieldClearedValue,
clearedValue: this.fieldClearedValue(),
}, ...args) }}
/>
) }
Expand All @@ -95,6 +96,7 @@ FieldProvider.propTypes = {
clearOnUnmount: PropTypes.bool,
initializeOnMount: PropTypes.bool,
initialValue: PropTypes.any,
clearedValue: PropTypes.any,
};

FieldProvider.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,113 @@ describe('renderForm function', () => {
wrapper.update();
expect(wrapper.find(Form).instance().form.getState().values.unmnounted).toEqual(undefined);
});

it('should clear values after unmount and set to field cleared value', () => {
layoutMapper = {
...layoutMapper,
[layoutComponents.FORM_WRAPPER]: ({ children, ...props }) => <form { ...props }>{ children }</form>,
};

const schema = {
fields: [{
component: components.TEXT_FIELD,
name: 'foo',
}, {
component: components.TEXT_FIELD,
name: 'unmnounted',
label: 'Label 1',
clearedValue: 'bla',
clearOnUnmount: true,
condition: {
when: 'foo',
is: 'show',
},
}]};

const onSubmit = jest.fn();

const wrapper = mount(
<FormRenderer
layoutMapper={ layoutMapper }
formFieldsMapper={{
[components.TEXT_FIELD]: TextField,
}}
schema={ schema }
onSubmit={ (values) => onSubmit(values) }
clearedValue="BlaBlaBla"
/>
);

wrapper.find('input').first().simulate('change', { target: { value: 'show' }});
wrapper.update();
wrapper.find('input').last().simulate('change', { target: { value: 'foovalue' }});
wrapper.update();

wrapper.find('form').simulate('submit');

expect(onSubmit).toHaveBeenCalledWith({ foo: 'show', unmnounted: 'foovalue' });
onSubmit.mockClear();

wrapper.find('input').first().simulate('change', { target: { value: 'barrr' }});
wrapper.update();

wrapper.find('form').simulate('submit');

expect(onSubmit).toHaveBeenCalledWith({ foo: 'barrr', unmnounted: 'bla' });
});

it('should clear values after unmount and set to form cleared value', () => {
layoutMapper = {
...layoutMapper,
[layoutComponents.FORM_WRAPPER]: ({ children, ...props }) => <form { ...props }>{ children }</form>,
};

const schema = {
fields: [{
component: components.TEXT_FIELD,
name: 'foo',
}, {
component: components.TEXT_FIELD,
name: 'unmnounted',
label: 'Label 1',
clearOnUnmount: true,
condition: {
when: 'foo',
is: 'show',
},
}]};

const onSubmit = jest.fn();

const wrapper = mount(
<FormRenderer
layoutMapper={ layoutMapper }
formFieldsMapper={{
[components.TEXT_FIELD]: TextField,
}}
schema={ schema }
onSubmit={ (values) => onSubmit(values) }
clearedValue="BlaBlaBla"
/>
);

wrapper.find('input').first().simulate('change', { target: { value: 'show' }});
wrapper.update();
wrapper.find('input').last().simulate('change', { target: { value: 'foovalue' }});
wrapper.update();

wrapper.find('form').simulate('submit');

expect(onSubmit).toHaveBeenCalledWith({ foo: 'show', unmnounted: 'foovalue' });
onSubmit.mockClear();

wrapper.find('input').first().simulate('change', { target: { value: 'barrr' }});
wrapper.update();

wrapper.find('form').simulate('submit');

expect(onSubmit).toHaveBeenCalledWith({ foo: 'barrr', unmnounted: 'BlaBlaBla' });
});
});

describe('#formSpy', () => {
Expand Down

0 comments on commit 5939aa3

Please sign in to comment.