Skip to content

Commit 43fd5ac

Browse files
committed
fix(renderer): allow to pass falsy values in initiliazeOnMount
1 parent 9caa86f commit 43fd5ac

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { dataTypes } from '../constants';
88
class FieldProvider extends Component{
99
componentDidMount() {
1010
if (this.props.initializeOnMount) {
11-
const initialValue = this.props.initialValue || this.props.formOptions.getFieldState(this.props.name).initial;
11+
const initialValue = this.props.hasOwnProperty('initialValue') ?
12+
this.props.initialValue :
13+
this.props.formOptions.getFieldState(this.props.name).initial;
1214
this.props.formOptions.change(this.props.name, initialValue);
1315
}
1416
}

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ describe('renderForm function', () => {
739739
component: componentTypes.TEXT_FIELD,
740740
name: INITIALIZED_FIELD,
741741
initializeOnMount,
742-
initialValue,
742+
...(initialValue ? { initialValue } : {}),
743743
condition: {
744744
when: SHOWER_FIELD,
745745
is: SHOW_VALUE,
@@ -948,5 +948,69 @@ describe('renderForm function', () => {
948948

949949
expect(onSubmit).toHaveBeenCalledWith({ input: 'show_false', unmounted: false }, expect.any(Object), expect.any(Function));
950950
});
951+
952+
it('should set unefined value in initializeOnMount', () => {
953+
layoutMapper = {
954+
[layoutComponents.FORM_WRAPPER]: ({ children, ...props }) => <form { ...props }>{ children }</form>,
955+
[layoutComponents.BUTTON]: ({ label, ...rest }) => <button { ...rest }>{ label }</button>,
956+
[layoutComponents.BUTTON_GROUP]: ({ children }) => <div>{ children }</div>,
957+
[layoutComponents.TITLE]: ({ children }) => <div>{ children }</div>,
958+
[layoutComponents.DESCRIPTION]: ({ children }) => <div>{ children }</div>,
959+
};
960+
961+
const schema = {
962+
fields: [{
963+
component: components.TEXT_FIELD,
964+
name: 'input',
965+
}, {
966+
component: components.TEXT_FIELD,
967+
name: 'unmounted',
968+
initialValue: undefined,
969+
initializeOnMount: true,
970+
condition: {
971+
when: 'input',
972+
is: 'show_undef',
973+
},
974+
}, {
975+
component: components.TEXT_FIELD,
976+
name: 'unmounted',
977+
initialValue: true,
978+
initializeOnMount: true,
979+
condition: {
980+
when: 'input',
981+
is: 'show_true',
982+
},
983+
}],
984+
};
985+
986+
const onSubmit = jest.fn();
987+
988+
const wrapper = mount(
989+
<FormRenderer
990+
layoutMapper={ layoutMapper }
991+
formFieldsMapper={{
992+
[components.TEXT_FIELD]: TextField,
993+
}}
994+
schema={ schema }
995+
onSubmit={ onSubmit }
996+
/>
997+
);
998+
999+
wrapper.find('input').first().simulate('change', { target: { value: 'show_true' }});
1000+
wrapper.update();
1001+
1002+
wrapper.find('form').simulate('submit');
1003+
1004+
expect(onSubmit).toHaveBeenCalledWith({ input: 'show_true', unmounted: true }, expect.any(Object), expect.any(Function));
1005+
onSubmit.mockClear();
1006+
1007+
wrapper.find('input').first().simulate('change', { target: { value: 'show_undef' }});
1008+
wrapper.update();
1009+
1010+
wrapper.find('form').simulate('submit');
1011+
wrapper.update();
1012+
1013+
expect(onSubmit).toHaveBeenCalledWith({ input: 'show_undef', unmounted: undefined }, expect.any(Object), expect.any(Function));
1014+
});
9511015
});
9521016
});

0 commit comments

Comments
 (0)