Skip to content

Commit 6392a5b

Browse files
authored
Merge pull request #1039 from kavyanekkalapu/carbon_fix_numberinput
fix(carbon): textinput type Number onchange function
2 parents ac3b770 + 6151194 commit 6392a5b

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import { act } from 'react-dom/test-utils';
4+
5+
import { FormRenderer, componentTypes } from '@data-driven-forms/react-form-renderer';
6+
7+
import FormTemplate from '../form-template';
8+
import componentMapper from '../component-mapper';
9+
import { TextInput, NumberInput } from 'carbon-components-react';
10+
11+
describe('<TextInput and NumberInput />', () => {
12+
let onChange;
13+
let wrapper;
14+
let submitSpy;
15+
beforeEach(() => {
16+
submitSpy = jest.fn();
17+
onChange = jest.fn();
18+
});
19+
afterEach(() => {
20+
onChange.mockReset();
21+
submitSpy.mockReset();
22+
});
23+
it('renders NumberInput', () => {
24+
const schema = {
25+
fields: [
26+
{
27+
component: componentTypes.TEXT_FIELD,
28+
name: 'input',
29+
label: 'Please enter a value',
30+
type: 'number',
31+
step: 1,
32+
initialValue: 1
33+
}
34+
]
35+
};
36+
37+
const wrapper = mount(
38+
<FormRenderer
39+
onSubmit={(values) => submitSpy(values)}
40+
FormTemplate={(props) => <FormTemplate {...props} />}
41+
schema={schema}
42+
componentMapper={componentMapper}
43+
/>
44+
);
45+
46+
expect(wrapper.find(NumberInput)).toHaveLength(1);
47+
});
48+
49+
it('NumberInput - click on increment button', async () => {
50+
const schema = {
51+
fields: [
52+
{
53+
component: componentTypes.TEXT_FIELD,
54+
name: 'input',
55+
label: 'Please enter a value',
56+
type: 'number',
57+
step: 1,
58+
initialValue: 1
59+
}
60+
]
61+
};
62+
await act(async () => {
63+
wrapper = mount(
64+
<FormRenderer
65+
onSubmit={(values) => submitSpy(values)}
66+
FormTemplate={(props) => <FormTemplate {...props} />}
67+
schema={schema}
68+
componentMapper={componentMapper}
69+
/>
70+
);
71+
});
72+
73+
expect(wrapper.find(NumberInput)).toHaveLength(1);
74+
expect(wrapper.find('input')).toHaveLength(1);
75+
76+
await act(async () => {
77+
wrapper
78+
.find('button.up-icon')
79+
.first()
80+
.simulate('click');
81+
});
82+
await act(async () => {
83+
wrapper.find('form').simulate('submit');
84+
});
85+
wrapper.update();
86+
expect(submitSpy).toHaveBeenCalledWith({ input: '2' });
87+
});
88+
89+
it('NumberInput - click on decrement button ', async () => {
90+
const schema = {
91+
fields: [
92+
{
93+
component: componentTypes.TEXT_FIELD,
94+
name: 'input',
95+
label: 'Please enter a value',
96+
type: 'number',
97+
step: 1,
98+
initialValue: 5
99+
}
100+
]
101+
};
102+
await act(async () => {
103+
wrapper = mount(
104+
<FormRenderer
105+
onSubmit={(values) => submitSpy(values)}
106+
FormTemplate={(props) => <FormTemplate {...props} />}
107+
schema={schema}
108+
componentMapper={componentMapper}
109+
/>
110+
);
111+
});
112+
113+
expect(wrapper.find(NumberInput)).toHaveLength(1);
114+
expect(wrapper.find('input')).toHaveLength(1);
115+
116+
await act(async () => {
117+
wrapper
118+
.find('button.down-icon')
119+
.first()
120+
.simulate('click');
121+
});
122+
await act(async () => {
123+
wrapper.find('form').simulate('submit');
124+
});
125+
wrapper.update();
126+
expect(submitSpy).toHaveBeenCalledWith({ input: '4' });
127+
});
128+
129+
it('renders TextInput', () => {
130+
const schema = {
131+
fields: [
132+
{
133+
component: componentTypes.TEXT_FIELD,
134+
name: 'input',
135+
label: 'Please enter a value',
136+
initialValue: 'test'
137+
}
138+
]
139+
};
140+
141+
const wrapper = mount(
142+
<FormRenderer
143+
onSubmit={(values) => submitSpy(values)}
144+
FormTemplate={(props) => <FormTemplate {...props} />}
145+
schema={schema}
146+
componentMapper={componentMapper}
147+
/>
148+
);
149+
150+
expect(wrapper.find(TextInput)).toHaveLength(1);
151+
});
152+
});

packages/carbon-component-mapper/src/text-field/text-field.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const TextField = (props) => {
1111

1212
const Component = input.type === 'number' ? NumberInput : TextInput;
1313

14+
const setValue = (e, input) => (input.type === 'number' ? e.imaginaryTarget.value : e.target.value);
15+
1416
const invalid = (meta.touched || validateOnMount) && (meta.error || meta.submitError);
1517
const warn = (meta.touched || validateOnMount) && meta.warning;
1618

@@ -23,6 +25,7 @@ const TextField = (props) => {
2325
invalidText={invalid || ''}
2426
warn={Boolean(warn)}
2527
warnText={warn || ''}
28+
onChange={(e) => input.onChange(setValue(e, input))}
2629
{...(input.type === 'number' ? { label: labelText } : { labelText })}
2730
{...rest}
2831
/>

0 commit comments

Comments
 (0)