Skip to content

Commit 8d0c086

Browse files
committed
fix(renderer): allow to pass falsy values in initiliazeOnMount
1 parent 903de10 commit 8d0c086

2 files changed

Lines changed: 106 additions & 36 deletions

File tree

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

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

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

Lines changed: 103 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ describe('renderForm function', () => {
10161016
component: componentTypes.TEXT_FIELD,
10171017
name: INITIALIZED_FIELD,
10181018
initializeOnMount,
1019-
initialValue,
1019+
...(initialValue ? { initialValue } : {}),
10201020
condition: {
10211021
when: SHOWER_FIELD,
10221022
is: SHOW_VALUE
@@ -1185,67 +1185,135 @@ describe('renderForm function', () => {
11851185
});
11861186

11871187
it('should set false value in initializeOnMount', () => {
1188-
layoutMapper = {
1189-
[layoutComponents.FORM_WRAPPER]: ({ children, ...props }) => <form { ...props }>{ children }</form>,
1190-
[layoutComponents.BUTTON]: ({ label, ...rest }) => <button { ...rest }>{ label }</button>,
1191-
[layoutComponents.BUTTON_GROUP]: ({ children }) => <div>{ children }</div>,
1192-
[layoutComponents.TITLE]: ({ children }) => <div>{ children }</div>,
1193-
[layoutComponents.DESCRIPTION]: ({ children }) => <div>{ children }</div>,
1188+
const schema = {
1189+
fields: [
1190+
{
1191+
component: componentTypes.TEXT_FIELD,
1192+
name: 'input'
1193+
},
1194+
{
1195+
component: componentTypes.TEXT_FIELD,
1196+
name: 'unmounted',
1197+
initialValue: false,
1198+
initializeOnMount: true,
1199+
condition: {
1200+
when: 'input',
1201+
is: 'show_false'
1202+
}
1203+
},
1204+
{
1205+
component: componentTypes.TEXT_FIELD,
1206+
name: 'unmounted',
1207+
initialValue: true,
1208+
initializeOnMount: true,
1209+
condition: {
1210+
when: 'input',
1211+
is: 'show_true'
1212+
}
1213+
}
1214+
]
11941215
};
11951216

1217+
const onSubmit = jest.fn();
1218+
1219+
const wrapper = mount(
1220+
<FormRenderer
1221+
FormTemplate={(props) => <FormTemplate {...props} />}
1222+
componentMapper={{
1223+
[componentTypes.TEXT_FIELD]: TextField
1224+
}}
1225+
schema={schema}
1226+
onSubmit={onSubmit}
1227+
/>
1228+
);
1229+
1230+
wrapper
1231+
.find('input')
1232+
.first()
1233+
.simulate('change', { target: { value: 'show_true' } });
1234+
wrapper.update();
1235+
1236+
wrapper.find('form').simulate('submit');
1237+
1238+
expect(onSubmit).toHaveBeenCalledWith({ input: 'show_true', unmounted: true }, expect.any(Object), expect.any(Function));
1239+
onSubmit.mockClear();
1240+
1241+
wrapper
1242+
.find('input')
1243+
.first()
1244+
.simulate('change', { target: { value: 'show_false' } });
1245+
wrapper.update();
1246+
1247+
wrapper.find('form').simulate('submit');
1248+
wrapper.update();
1249+
1250+
expect(onSubmit).toHaveBeenCalledWith({ input: 'show_false', unmounted: false }, expect.any(Object), expect.any(Function));
1251+
});
1252+
1253+
it('should set unefined value in initializeOnMount', () => {
11961254
const schema = {
1197-
fields: [{
1198-
component: components.TEXT_FIELD,
1199-
name: 'input',
1200-
}, {
1201-
component: components.TEXT_FIELD,
1202-
name: 'unmounted',
1203-
initialValue: false,
1204-
initializeOnMount: true,
1205-
condition: {
1206-
when: 'input',
1207-
is: 'show_false',
1255+
fields: [
1256+
{
1257+
component: componentTypes.TEXT_FIELD,
1258+
name: 'input'
12081259
},
1209-
}, {
1210-
component: components.TEXT_FIELD,
1211-
name: 'unmounted',
1212-
initialValue: true,
1213-
initializeOnMount: true,
1214-
condition: {
1215-
when: 'input',
1216-
is: 'show_true',
1260+
{
1261+
component: componentTypes.TEXT_FIELD,
1262+
name: 'unmounted',
1263+
initialValue: undefined,
1264+
initializeOnMount: true,
1265+
condition: {
1266+
when: 'input',
1267+
is: 'show_undef'
1268+
}
12171269
},
1218-
}],
1270+
{
1271+
component: componentTypes.TEXT_FIELD,
1272+
name: 'unmounted',
1273+
initialValue: true,
1274+
initializeOnMount: true,
1275+
condition: {
1276+
when: 'input',
1277+
is: 'show_true'
1278+
}
1279+
}
1280+
]
12191281
};
12201282

12211283
const onSubmit = jest.fn();
12221284

12231285
const wrapper = mount(
12241286
<FormRenderer
1225-
layoutMapper={ layoutMapper }
1226-
formFieldsMapper={{
1227-
[components.TEXT_FIELD]: TextField,
1287+
FormTemplate={(props) => <FormTemplate {...props} />}
1288+
componentMapper={{
1289+
[componentTypes.TEXT_FIELD]: TextField
12281290
}}
1229-
schema={ schema }
1230-
onSubmit={ onSubmit }
1291+
schema={schema}
1292+
onSubmit={onSubmit}
12311293
/>
12321294
);
12331295

1234-
wrapper.find('input').first().simulate('change', { target: { value: 'show_true' }});
1296+
wrapper
1297+
.find('input')
1298+
.first()
1299+
.simulate('change', { target: { value: 'show_true' } });
12351300
wrapper.update();
12361301

12371302
wrapper.find('form').simulate('submit');
12381303

12391304
expect(onSubmit).toHaveBeenCalledWith({ input: 'show_true', unmounted: true }, expect.any(Object), expect.any(Function));
12401305
onSubmit.mockClear();
12411306

1242-
wrapper.find('input').first().simulate('change', { target: { value: 'show_false' }});
1307+
wrapper
1308+
.find('input')
1309+
.first()
1310+
.simulate('change', { target: { value: 'show_undef' } });
12431311
wrapper.update();
12441312

12451313
wrapper.find('form').simulate('submit');
12461314
wrapper.update();
12471315

1248-
expect(onSubmit).toHaveBeenCalledWith({ input: 'show_false', unmounted: false }, expect.any(Object), expect.any(Function));
1316+
expect(onSubmit).toHaveBeenCalledWith({ input: 'show_undef', unmounted: undefined }, expect.any(Object), expect.any(Function));
12491317
});
12501318
});
12511319

0 commit comments

Comments
 (0)