Skip to content

Commit fa2da23

Browse files
authored
Merge pull request #354 from rvsia/fixFalsyInitialValue
Fixes falsy values for initialValue + initiliazeOnMount
2 parents e4c6550 + 43fd5ac commit fa2da23

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-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: 129 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,
@@ -884,5 +884,133 @@ describe('renderForm function', () => {
884884
mountInitializedField(wrapper);
885885
expectSchemaInitialValue(wrapper);
886886
});
887+
888+
it('should set false value in initializeOnMount', () => {
889+
layoutMapper = {
890+
[layoutComponents.FORM_WRAPPER]: ({ children, ...props }) => <form { ...props }>{ children }</form>,
891+
[layoutComponents.BUTTON]: ({ label, ...rest }) => <button { ...rest }>{ label }</button>,
892+
[layoutComponents.BUTTON_GROUP]: ({ children }) => <div>{ children }</div>,
893+
[layoutComponents.TITLE]: ({ children }) => <div>{ children }</div>,
894+
[layoutComponents.DESCRIPTION]: ({ children }) => <div>{ children }</div>,
895+
};
896+
897+
const schema = {
898+
fields: [{
899+
component: components.TEXT_FIELD,
900+
name: 'input',
901+
}, {
902+
component: components.TEXT_FIELD,
903+
name: 'unmounted',
904+
initialValue: false,
905+
initializeOnMount: true,
906+
condition: {
907+
when: 'input',
908+
is: 'show_false',
909+
},
910+
}, {
911+
component: components.TEXT_FIELD,
912+
name: 'unmounted',
913+
initialValue: true,
914+
initializeOnMount: true,
915+
condition: {
916+
when: 'input',
917+
is: 'show_true',
918+
},
919+
}],
920+
};
921+
922+
const onSubmit = jest.fn();
923+
924+
const wrapper = mount(
925+
<FormRenderer
926+
layoutMapper={ layoutMapper }
927+
formFieldsMapper={{
928+
[components.TEXT_FIELD]: TextField,
929+
}}
930+
schema={ schema }
931+
onSubmit={ onSubmit }
932+
/>
933+
);
934+
935+
wrapper.find('input').first().simulate('change', { target: { value: 'show_true' }});
936+
wrapper.update();
937+
938+
wrapper.find('form').simulate('submit');
939+
940+
expect(onSubmit).toHaveBeenCalledWith({ input: 'show_true', unmounted: true }, expect.any(Object), expect.any(Function));
941+
onSubmit.mockClear();
942+
943+
wrapper.find('input').first().simulate('change', { target: { value: 'show_false' }});
944+
wrapper.update();
945+
946+
wrapper.find('form').simulate('submit');
947+
wrapper.update();
948+
949+
expect(onSubmit).toHaveBeenCalledWith({ input: 'show_false', unmounted: false }, expect.any(Object), expect.any(Function));
950+
});
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+
});
8871015
});
8881016
});

0 commit comments

Comments
 (0)