Skip to content

Commit 9e8574e

Browse files
committed
fix(renderer): clearOnMount set clearedValue, not undefined
1 parent 8d0c086 commit 9e8574e

File tree

2 files changed

+130
-9
lines changed

2 files changed

+130
-9
lines changed

packages/react-form-renderer/src/hooks/use-field-api.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,29 @@ const useFieldApi = ({ name, initializeOnMount, component, render, validate, dat
1515
* This affects conditional fields
1616
*/
1717
if (initializeOnMount) {
18-
const initialValue = Object.prototype.hasOwnProperty.call(props, 'initialValue')
19-
? props.initialValue
20-
: props.formOptions.getFieldState(props.name).initial;
18+
const initialValue = Object.prototype.hasOwnProperty.call(props, 'initialValue') ? props.initialValue : formOptions.getFieldState(name).initial;
2119
fieldProps.input.onChange(initialValue);
2220
}
2321
}, [initializeOnMount, props.initialValue, fieldProps.meta.initial, fieldProps.input]);
2422

23+
/**
24+
* Prepare deleted value of field
25+
*/
26+
const fieldClearedValue = Object.prototype.hasOwnProperty.call(props, 'clearedValue') ? props.clearedValue : formOptions.clearedValue;
27+
2528
useEffect(
2629
() => () => {
2730
/**
2831
* Delete the value from form state when field is inmounted
2932
*/
3033
if ((formOptions.clearOnUnmount || props.clearOnUnmount) && props.clearOnUnmount !== false) {
31-
fieldProps.input.onChange(undefined);
34+
fieldProps.input.onChange(fieldClearedValue);
3235
}
3336
},
3437
// eslint-disable-next-line react-hooks/exhaustive-deps
3538
[]
3639
);
3740

38-
/**
39-
* Prepare deleted value of field
40-
*/
41-
const fieldClearedValue = Object.prototype.hasOwnProperty.call(props, 'clearedValue') ? props.clearedValue : formOptions.clearedValue;
42-
4341
/**
4442
* Map actions to props
4543
*/

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,129 @@ describe('renderForm function', () => {
992992
wrapper.find('form').simulate('submit');
993993
expect(onSubmit).toHaveBeenCalledWith({ unmnounted: undefined, foo: 'barrr' });
994994
});
995+
996+
it('should clear values after unmount and set to field cleared value', () => {
997+
const schema = {
998+
fields: [
999+
{
1000+
component: componentTypes.TEXT_FIELD,
1001+
name: 'foo'
1002+
},
1003+
{
1004+
component: componentTypes.TEXT_FIELD,
1005+
name: 'unmnounted',
1006+
label: 'Label 1',
1007+
clearedValue: 'bla',
1008+
clearOnUnmount: true,
1009+
condition: {
1010+
when: 'foo',
1011+
is: 'show'
1012+
}
1013+
}
1014+
]
1015+
};
1016+
1017+
const onSubmit = jest.fn();
1018+
1019+
const wrapper = mount(
1020+
<FormRenderer
1021+
FormTemplate={(props) => <FormTemplate {...props} />}
1022+
componentMapper={{
1023+
[componentTypes.TEXT_FIELD]: TextField
1024+
}}
1025+
schema={schema}
1026+
onSubmit={(values) => onSubmit(values)}
1027+
clearedValue="BlaBlaBla"
1028+
/>
1029+
);
1030+
1031+
wrapper
1032+
.find('input')
1033+
.first()
1034+
.simulate('change', { target: { value: 'show' } });
1035+
wrapper.update();
1036+
wrapper
1037+
.find('input')
1038+
.last()
1039+
.simulate('change', { target: { value: 'foovalue' } });
1040+
wrapper.update();
1041+
1042+
wrapper.find('form').simulate('submit');
1043+
1044+
expect(onSubmit).toHaveBeenCalledWith({ foo: 'show', unmnounted: 'foovalue' });
1045+
onSubmit.mockClear();
1046+
1047+
wrapper
1048+
.find('input')
1049+
.first()
1050+
.simulate('change', { target: { value: 'barrr' } });
1051+
wrapper.update();
1052+
1053+
wrapper.find('form').simulate('submit');
1054+
1055+
expect(onSubmit).toHaveBeenCalledWith({ foo: 'barrr', unmnounted: 'bla' });
1056+
});
1057+
1058+
it('should clear values after unmount and set to form cleared value', () => {
1059+
const schema = {
1060+
fields: [
1061+
{
1062+
component: componentTypes.TEXT_FIELD,
1063+
name: 'foo'
1064+
},
1065+
{
1066+
component: componentTypes.TEXT_FIELD,
1067+
name: 'unmnounted',
1068+
label: 'Label 1',
1069+
clearOnUnmount: true,
1070+
condition: {
1071+
when: 'foo',
1072+
is: 'show'
1073+
}
1074+
}
1075+
]
1076+
};
1077+
1078+
const onSubmit = jest.fn();
1079+
1080+
const wrapper = mount(
1081+
<FormRenderer
1082+
FormTemplate={(props) => <FormTemplate {...props} />}
1083+
componentMapper={{
1084+
[componentTypes.TEXT_FIELD]: TextField
1085+
}}
1086+
schema={schema}
1087+
onSubmit={(values) => onSubmit(values)}
1088+
clearedValue="BlaBlaBla"
1089+
/>
1090+
);
1091+
1092+
wrapper
1093+
.find('input')
1094+
.first()
1095+
.simulate('change', { target: { value: 'show' } });
1096+
wrapper.update();
1097+
wrapper
1098+
.find('input')
1099+
.last()
1100+
.simulate('change', { target: { value: 'foovalue' } });
1101+
wrapper.update();
1102+
1103+
wrapper.find('form').simulate('submit');
1104+
1105+
expect(onSubmit).toHaveBeenCalledWith({ foo: 'show', unmnounted: 'foovalue' });
1106+
onSubmit.mockClear();
1107+
1108+
wrapper
1109+
.find('input')
1110+
.first()
1111+
.simulate('change', { target: { value: 'barrr' } });
1112+
wrapper.update();
1113+
1114+
wrapper.find('form').simulate('submit');
1115+
1116+
expect(onSubmit).toHaveBeenCalledWith({ foo: 'barrr', unmnounted: 'BlaBlaBla' });
1117+
});
9951118
});
9961119

9971120
describe('#initializeOnMount', () => {

0 commit comments

Comments
 (0)