Skip to content

Commit

Permalink
Merge pull request #355 from rvsia/clearOnUnMountClearedValue
Browse files Browse the repository at this point in the history
fix(renderer): clearOnUnmount set clearedValue, not undefined
  • Loading branch information
Hyperkid123 authored Mar 5, 2020
2 parents fa2da23 + 5939aa3 commit 6cdfb7b
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 @@ -15,15 +15,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 @@ -37,7 +38,7 @@ class FieldProvider extends Component{
...fieldsProps.meta,
dataType,
onChange,
clearedValue: fieldClearedValue,
clearedValue: this.fieldClearedValue(),
}, ...args);
},
}}
Expand All @@ -55,7 +56,7 @@ class FieldProvider extends Component{
...fieldsProps.meta,
dataType,
onChange,
clearedValue: fieldClearedValue,
clearedValue: this.fieldClearedValue(),
}, ...args),
},
}) } />;
Expand All @@ -73,7 +74,7 @@ class FieldProvider extends Component{
...fieldsProps.meta,
dataType,
onChange,
clearedValue: fieldClearedValue,
clearedValue: this.fieldClearedValue(),
}, ...args) }}
/>
) }
Expand All @@ -97,6 +98,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 6cdfb7b

Please sign in to comment.