Skip to content

Commit 326d9f0

Browse files
Hyperkid123rvsia
authored andcommitted
fix(renderer): convert initialValue dataType
1 parent 0a422fc commit 326d9f0

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { convertType } from './enhanced-on-change';
2+
3+
const convertInitialValue = (initialValue, dataType) => {
4+
if (initialValue === undefined || !dataType) {
5+
return initialValue;
6+
}
7+
8+
if (Array.isArray(initialValue)) {
9+
return initialValue.map((value) =>
10+
typeof value === 'object'
11+
? {
12+
...value,
13+
value: Object.prototype.hasOwnProperty.call(value, 'value') ? convertType(dataType, value.value) : value
14+
}
15+
: convertType(dataType, value)
16+
);
17+
}
18+
19+
return convertType(dataType, initialValue);
20+
};
21+
22+
export default convertInitialValue;

packages/react-form-renderer/src/form-renderer/enhanced-on-change.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const castToBoolean = (value) => {
3434
* @param {FieldDataTypes} dataType type for value conversion
3535
* @param {Any} value value to be converted
3636
*/
37-
const convertType = (dataType, value) =>
37+
export const convertType = (dataType, value) =>
3838
({
3939
[dataTypes.INTEGER]: !isNaN(Number(value)) && parseInt(value),
4040
[dataTypes.FLOAT]: !isNaN(Number(value)) && parseFloat(value),

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@ import PropTypes from 'prop-types';
33

44
import componentTypes from '../components/component-types';
55
import composeValidators from '../components/compose-validators';
6+
import convertInitialValue from './convert-initial-value';
67

78
const shouldAssignFormOptions = (componentType) => componentTypes.FIELD_ARRAY === componentType;
89
const assignSpecialType = (componentType) => ([componentTypes.CHECKBOX, componentTypes.RADIO].includes(componentType) ? componentType : undefined);
910

1011
const FieldWrapper = ({ componentType, validate, component, ...rest }) => {
12+
const props = rest;
13+
14+
if (Object.prototype.hasOwnProperty.call(rest, 'initialValue') && Object.prototype.hasOwnProperty.call(rest, 'dataType')) {
15+
props.initialValue = convertInitialValue(rest.initialValue, rest.dataType);
16+
}
17+
1118
const componentProps = {
1219
type: assignSpecialType(componentType),
13-
...rest
20+
...props
1421
};
22+
1523
if (shouldAssignFormOptions(componentType)) {
1624
componentProps.arrayValidator = (value = []) => {
1725
if (!Array.isArray(value)) {
@@ -38,7 +46,8 @@ FieldWrapper.propTypes = {
3846
componentType: PropTypes.string,
3947
validate: PropTypes.arrayOf(PropTypes.func),
4048
component: PropTypes.oneOfType([PropTypes.node, PropTypes.func, PropTypes.element]).isRequired,
41-
dataType: PropTypes.string
49+
dataType: PropTypes.string,
50+
initialValue: PropTypes.any
4251
};
4352

4453
export default FieldWrapper;

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,58 @@ describe('<FormRenderer />', () => {
127127
);
128128
expect(toJson(wrapper)).toMatchSnapshot();
129129
});
130+
131+
describe('Initial value data types', () => {
132+
it('should convert string to integer', () => {
133+
const onSubmit = jest.fn();
134+
const schema = {
135+
fields: [
136+
{
137+
component: componentTypes.TEXT_FIELD,
138+
name: 'initial-convert',
139+
initialValue: '5',
140+
dataType: 'integer'
141+
}
142+
]
143+
};
144+
const wrapper = mount(<FormRenderer {...initialProps} schema={schema} onSubmit={(values) => onSubmit(values)} />);
145+
wrapper.find('form').simulate('submit');
146+
expect(onSubmit).toHaveBeenCalledWith({ 'initial-convert': 5 });
147+
});
148+
149+
it('should convert individual values in array of literals as initial value', () => {
150+
const onSubmit = jest.fn();
151+
const schema = {
152+
fields: [
153+
{
154+
component: componentTypes.TEXT_FIELD,
155+
name: 'initial-convert',
156+
initialValue: ['5', 3, '11', '999'],
157+
dataType: 'integer'
158+
}
159+
]
160+
};
161+
const wrapper = mount(<FormRenderer {...initialProps} schema={schema} onSubmit={(values) => onSubmit(values)} />);
162+
wrapper.find('form').simulate('submit');
163+
expect(onSubmit).toHaveBeenCalledWith({ 'initial-convert': [5, 3, 11, 999] });
164+
});
165+
166+
it('should convert individual values in array of objects as initial value', () => {
167+
const onSubmit = jest.fn();
168+
const schema = {
169+
fields: [
170+
{
171+
component: componentTypes.TEXT_FIELD,
172+
name: 'initial-convert',
173+
initialValue: [{ value: '5' }, { value: 3 }, { value: '11' }, { value: '999' }],
174+
dataType: 'integer'
175+
}
176+
]
177+
};
178+
const wrapper = mount(<FormRenderer {...initialProps} schema={schema} onSubmit={(values) => onSubmit(values)} />);
179+
180+
wrapper.find('form').simulate('submit');
181+
expect(onSubmit).toHaveBeenCalledWith({ 'initial-convert': [{ value: 5 }, { value: 3 }, { value: 11 }, { value: 999 }] });
182+
});
183+
});
130184
});

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ export const pattern = memoize(({ pattern, message, flags } = {}) => {
4444

4545
if (Array.isArray(value)) {
4646
const error = value.find((item) => {
47-
const parsedValue = typeof item === 'string' ? item : item.toString();
47+
const parsedValue =
48+
typeof item === 'object' && Object.prototype.hasOwnProperty.call(item, 'value')
49+
? item.value.toString()
50+
: typeof item === 'string'
51+
? item
52+
: item.toString();
4853
return pattern && !parsedValue.match(verifiedPattern);
4954
});
5055
const msg = prepareMsg(message, 'pattern').defaultMessage;

0 commit comments

Comments
 (0)