Skip to content

Commit fd390c8

Browse files
authored
Merge pull request #1138 from rvsia/fixInitialValuesPriority
Fix priority for initialValues
2 parents 4f073d4 + 608655b commit fd390c8

File tree

6 files changed

+467
-353
lines changed

6 files changed

+467
-353
lines changed

packages/react-form-renderer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"devDependencies": {
3131
},
3232
"dependencies": {
33-
"final-form": "^4.20.0",
33+
"final-form": "^4.20.4",
3434
"final-form-arrays": "^3.0.2",
3535
"final-form-focus": "^1.1.2",
3636
"lodash": "^4.17.15",

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const FormRenderer = ({
101101
...form,
102102
ffGetRegisteredFields: form.getRegisteredFields,
103103
getRegisteredFields: internalGetRegisteredFields,
104+
initialValues: props.initialValues,
104105
},
105106
}}
106107
>
@@ -140,6 +141,7 @@ FormRenderer.propTypes = {
140141
[PropTypes.string]: PropTypes.func,
141142
}),
142143
}),
144+
initialValues: PropTypes.object,
143145
};
144146

145147
FormRenderer.defaultProps = {

packages/react-form-renderer/src/renderer-context/renderer-context.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface FormOptions extends FormApi {
1818
internalUnregisterField: (name: string) => void;
1919
getRegisteredFields: () => string[];
2020
ffGetRegisteredFields: () => string[];
21+
initialValues: AnyObject;
2122
}
2223

2324
export interface RendererContextValue {

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { act } from 'react-dom/test-utils';
23
import { mount } from 'enzyme';
34
import toJson from 'enzyme-to-json';
45
import arrayMutators from 'final-form-arrays';
@@ -1752,5 +1753,99 @@ describe('renderForm function', () => {
17521753
wrapper.find('form').simulate('submit');
17531754
expect(onSubmit).toHaveBeenCalledWith({ testField: 'higher-priority' });
17541755
});
1756+
1757+
it('empty initialValues ', () => {
1758+
const onSubmit = jest.fn();
1759+
const wrapper = mount(
1760+
<FormRenderer
1761+
FormTemplate={(props) => <FormTemplate {...props} />}
1762+
componentMapper={{
1763+
'custom-component': TextField,
1764+
}}
1765+
schema={{
1766+
fields: [
1767+
{
1768+
component: 'custom-component',
1769+
name: 'testField',
1770+
initialValue: 'lower-priority',
1771+
},
1772+
],
1773+
}}
1774+
onSubmit={(values) => onSubmit(values)}
1775+
initialValues={{ testField: undefined }}
1776+
/>
1777+
);
1778+
1779+
expect(wrapper.find('input').instance().value).toEqual('lower-priority');
1780+
1781+
wrapper.find('form').simulate('submit');
1782+
expect(onSubmit).toHaveBeenCalledWith({ testField: 'lower-priority' });
1783+
});
1784+
1785+
it('null initialValues ', () => {
1786+
const onSubmit = jest.fn();
1787+
const wrapper = mount(
1788+
<FormRenderer
1789+
FormTemplate={(props) => <FormTemplate {...props} />}
1790+
componentMapper={{
1791+
'custom-component': TextField,
1792+
}}
1793+
schema={{
1794+
fields: [
1795+
{
1796+
component: 'custom-component',
1797+
name: 'testField',
1798+
initialValue: 'lower-priority',
1799+
},
1800+
],
1801+
}}
1802+
onSubmit={(values) => onSubmit(values)}
1803+
initialValues={{ testField: null }}
1804+
/>
1805+
);
1806+
1807+
expect(wrapper.find('input').instance().value).toEqual('');
1808+
1809+
wrapper.find('form').simulate('submit');
1810+
expect(onSubmit).toHaveBeenCalledWith({ testField: null });
1811+
});
1812+
1813+
it('use initialValue when initialOnMount', async () => {
1814+
const onSubmit = jest.fn();
1815+
let wrapper;
1816+
1817+
await act(async () => {
1818+
wrapper = mount(
1819+
<FormRenderer
1820+
FormTemplate={(props) => <FormTemplate {...props} />}
1821+
componentMapper={{
1822+
'custom-component': TextField,
1823+
}}
1824+
schema={{
1825+
fields: [
1826+
{
1827+
component: 'custom-component',
1828+
name: 'testField',
1829+
initialValue: 'lower-priority',
1830+
initializeOnMount: true,
1831+
},
1832+
],
1833+
}}
1834+
onSubmit={(values) => onSubmit(values)}
1835+
initialValues={{ testField: 'higher-priority' }}
1836+
/>
1837+
);
1838+
});
1839+
wrapper.update();
1840+
1841+
expect(wrapper.find('input').instance().value).toEqual('lower-priority');
1842+
1843+
await act(async () => {
1844+
wrapper.find('form').simulate('submit');
1845+
});
1846+
wrapper.update();
1847+
1848+
expect(onSubmit).toHaveBeenCalledWith({ testField: 'lower-priority' });
1849+
});
17551850
});
17561851
});

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import componentTypes from '../component-types';
88
import { prepareArrayValidator, getValidate } from './validator-helpers';
99
import composeValidators from './compose-validators';
1010
import isEqual from 'lodash/isEqual';
11+
import get from 'lodash/get';
1112

1213
const calculateInitialValue = (props) => {
1314
if (Object.prototype.hasOwnProperty.call(props, 'initialValue') && props.dataType) {
@@ -88,6 +89,10 @@ const useFieldApi = ({ name, resolveProps, skipRegistration = false, ...props })
8889
const { validatorMapper, formOptions } = useContext(RendererContext);
8990
const [warning, setWarning] = useState();
9091

92+
// if there is field initial value, we have to check form initialValues
93+
// initialValues should have higher priority
94+
const formInitialValue = Object.prototype.hasOwnProperty.call(props, 'initialValue') ? get(formOptions.initialValues, name) : undefined;
95+
9196
const resolvedProps = resolveProps ? resolveProps(props, createFieldProps(name, formOptions), formOptions) || {} : {};
9297

9398
const combinedProps = { ...props, ...resolvedProps };
@@ -112,7 +117,7 @@ const useFieldApi = ({ name, resolveProps, skipRegistration = false, ...props })
112117
...(stateValidate ? { validate: stateValidate } : {}),
113118
};
114119

115-
const field = useField(name, enhancedProps);
120+
const field = useField(name, { ...enhancedProps, ...(typeof formInitialValue !== 'undefined' && { initialValue: formInitialValue }) });
116121

117122
/** Reinitilize type */
118123
useEffect(() => {

0 commit comments

Comments
 (0)