Skip to content

Commit 6cdfb7b

Browse files
authored
Merge pull request #355 from rvsia/clearOnUnMountClearedValue
fix(renderer): clearOnUnmount set clearedValue, not undefined
2 parents fa2da23 + 5939aa3 commit 6cdfb7b

File tree

2 files changed

+114
-5
lines changed

2 files changed

+114
-5
lines changed

packages/react-form-renderer/src/form-renderer/field-provider.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ class FieldProvider extends Component{
1515
}
1616
}
1717

18+
fieldClearedValue = () => this.props.hasOwnProperty('clearedValue') ? this.props.clearedValue : this.props.formOptions.clearedValue
19+
1820
componentWillUnmount(){
1921
if ((this.props.formOptions.clearOnUnmount || this.props.clearOnUnmount) && this.props.clearOnUnmount !== false) {
20-
this.props.formOptions.change(this.props.name, undefined);
22+
this.props.formOptions.change(this.props.name, this.fieldClearedValue());
2123
}
2224
}
2325

2426
render(){
2527
const { clearOnUnmount, component, render, dataType, children, ...props } = this.props;
26-
const fieldClearedValue = props.hasOwnProperty('clearedValue') ? props.clearedValue : props.formOptions.clearedValue;
2728
const { clearedValue, ...fieldProps } = props;
2829
if (component) {
2930
const FieldComponent = component;
@@ -37,7 +38,7 @@ class FieldProvider extends Component{
3738
...fieldsProps.meta,
3839
dataType,
3940
onChange,
40-
clearedValue: fieldClearedValue,
41+
clearedValue: this.fieldClearedValue(),
4142
}, ...args);
4243
},
4344
}}
@@ -55,7 +56,7 @@ class FieldProvider extends Component{
5556
...fieldsProps.meta,
5657
dataType,
5758
onChange,
58-
clearedValue: fieldClearedValue,
59+
clearedValue: this.fieldClearedValue(),
5960
}, ...args),
6061
},
6162
}) } />;
@@ -73,7 +74,7 @@ class FieldProvider extends Component{
7374
...fieldsProps.meta,
7475
dataType,
7576
onChange,
76-
clearedValue: fieldClearedValue,
77+
clearedValue: this.fieldClearedValue(),
7778
}, ...args) }}
7879
/>
7980
) }
@@ -97,6 +98,7 @@ FieldProvider.propTypes = {
9798
clearOnUnmount: PropTypes.bool,
9899
initializeOnMount: PropTypes.bool,
99100
initialValue: PropTypes.any,
101+
clearedValue: PropTypes.any,
100102
};
101103

102104
FieldProvider.defaultProps = {

packages/react-form-renderer/src/tests/form-renderer/render-form.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,113 @@ describe('renderForm function', () => {
684684
wrapper.update();
685685
expect(wrapper.find(Form).instance().form.getState().values.unmnounted).toEqual(undefined);
686686
});
687+
688+
it('should clear values after unmount and set to field cleared value', () => {
689+
layoutMapper = {
690+
...layoutMapper,
691+
[layoutComponents.FORM_WRAPPER]: ({ children, ...props }) => <form { ...props }>{ children }</form>,
692+
};
693+
694+
const schema = {
695+
fields: [{
696+
component: components.TEXT_FIELD,
697+
name: 'foo',
698+
}, {
699+
component: components.TEXT_FIELD,
700+
name: 'unmnounted',
701+
label: 'Label 1',
702+
clearedValue: 'bla',
703+
clearOnUnmount: true,
704+
condition: {
705+
when: 'foo',
706+
is: 'show',
707+
},
708+
}]};
709+
710+
const onSubmit = jest.fn();
711+
712+
const wrapper = mount(
713+
<FormRenderer
714+
layoutMapper={ layoutMapper }
715+
formFieldsMapper={{
716+
[components.TEXT_FIELD]: TextField,
717+
}}
718+
schema={ schema }
719+
onSubmit={ (values) => onSubmit(values) }
720+
clearedValue="BlaBlaBla"
721+
/>
722+
);
723+
724+
wrapper.find('input').first().simulate('change', { target: { value: 'show' }});
725+
wrapper.update();
726+
wrapper.find('input').last().simulate('change', { target: { value: 'foovalue' }});
727+
wrapper.update();
728+
729+
wrapper.find('form').simulate('submit');
730+
731+
expect(onSubmit).toHaveBeenCalledWith({ foo: 'show', unmnounted: 'foovalue' });
732+
onSubmit.mockClear();
733+
734+
wrapper.find('input').first().simulate('change', { target: { value: 'barrr' }});
735+
wrapper.update();
736+
737+
wrapper.find('form').simulate('submit');
738+
739+
expect(onSubmit).toHaveBeenCalledWith({ foo: 'barrr', unmnounted: 'bla' });
740+
});
741+
742+
it('should clear values after unmount and set to form cleared value', () => {
743+
layoutMapper = {
744+
...layoutMapper,
745+
[layoutComponents.FORM_WRAPPER]: ({ children, ...props }) => <form { ...props }>{ children }</form>,
746+
};
747+
748+
const schema = {
749+
fields: [{
750+
component: components.TEXT_FIELD,
751+
name: 'foo',
752+
}, {
753+
component: components.TEXT_FIELD,
754+
name: 'unmnounted',
755+
label: 'Label 1',
756+
clearOnUnmount: true,
757+
condition: {
758+
when: 'foo',
759+
is: 'show',
760+
},
761+
}]};
762+
763+
const onSubmit = jest.fn();
764+
765+
const wrapper = mount(
766+
<FormRenderer
767+
layoutMapper={ layoutMapper }
768+
formFieldsMapper={{
769+
[components.TEXT_FIELD]: TextField,
770+
}}
771+
schema={ schema }
772+
onSubmit={ (values) => onSubmit(values) }
773+
clearedValue="BlaBlaBla"
774+
/>
775+
);
776+
777+
wrapper.find('input').first().simulate('change', { target: { value: 'show' }});
778+
wrapper.update();
779+
wrapper.find('input').last().simulate('change', { target: { value: 'foovalue' }});
780+
wrapper.update();
781+
782+
wrapper.find('form').simulate('submit');
783+
784+
expect(onSubmit).toHaveBeenCalledWith({ foo: 'show', unmnounted: 'foovalue' });
785+
onSubmit.mockClear();
786+
787+
wrapper.find('input').first().simulate('change', { target: { value: 'barrr' }});
788+
wrapper.update();
789+
790+
wrapper.find('form').simulate('submit');
791+
792+
expect(onSubmit).toHaveBeenCalledWith({ foo: 'barrr', unmnounted: 'BlaBlaBla' });
793+
});
687794
});
688795

689796
describe('#formSpy', () => {

0 commit comments

Comments
 (0)