Skip to content

Commit 5939aa3

Browse files
committed
fix(renderer): clearOnMount set clearedValue, not undefined
1 parent e4c6550 commit 5939aa3

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
@@ -13,15 +13,16 @@ class FieldProvider extends Component{
1313
}
1414
}
1515

16+
fieldClearedValue = () => this.props.hasOwnProperty('clearedValue') ? this.props.clearedValue : this.props.formOptions.clearedValue
17+
1618
componentWillUnmount(){
1719
if ((this.props.formOptions.clearOnUnmount || this.props.clearOnUnmount) && this.props.clearOnUnmount !== false) {
18-
this.props.formOptions.change(this.props.name, undefined);
20+
this.props.formOptions.change(this.props.name, this.fieldClearedValue());
1921
}
2022
}
2123

2224
render(){
2325
const { clearOnUnmount, component, render, dataType, children, ...props } = this.props;
24-
const fieldClearedValue = props.hasOwnProperty('clearedValue') ? props.clearedValue : props.formOptions.clearedValue;
2526
const { clearedValue, ...fieldProps } = props;
2627
if (component) {
2728
const FieldComponent = component;
@@ -35,7 +36,7 @@ class FieldProvider extends Component{
3536
...fieldsProps.meta,
3637
dataType,
3738
onChange,
38-
clearedValue: fieldClearedValue,
39+
clearedValue: this.fieldClearedValue(),
3940
}, ...args);
4041
},
4142
}}
@@ -53,7 +54,7 @@ class FieldProvider extends Component{
5354
...fieldsProps.meta,
5455
dataType,
5556
onChange,
56-
clearedValue: fieldClearedValue,
57+
clearedValue: this.fieldClearedValue(),
5758
}, ...args),
5859
},
5960
}) } />;
@@ -71,7 +72,7 @@ class FieldProvider extends Component{
7172
...fieldsProps.meta,
7273
dataType,
7374
onChange,
74-
clearedValue: fieldClearedValue,
75+
clearedValue: this.fieldClearedValue(),
7576
}, ...args) }}
7677
/>
7778
) }
@@ -95,6 +96,7 @@ FieldProvider.propTypes = {
9596
clearOnUnmount: PropTypes.bool,
9697
initializeOnMount: PropTypes.bool,
9798
initialValue: PropTypes.any,
99+
clearedValue: PropTypes.any,
98100
};
99101

100102
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)