Skip to content

Commit 7756528

Browse files
fix(numberInput): avoid parsing value (#102)
* feat(numberInput): change to accept decimals * feat(numberInput): change to accept decimals * feat(numberInput): remove step * fix(numberInput): add onHandleChange function * fix(numberInput): add onHandleChange function * fix(numberInput): changes in handleChangeValue * fix(numberInput): changes in handleChangeValue * fix(numberInput): fix tests * refactor(numberInput): change const name * chore(package): bump version --------- Co-authored-by: Maximiliano forlenza <forlenzamaximiliano@gmail.com>
1 parent 6b2b7a5 commit 7756528

3 files changed

Lines changed: 49 additions & 14 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@indec/react-commons",
3-
"version": "5.6.1",
3+
"version": "5.7.0",
44
"description": "Common reactjs components for apps",
55
"private": false,
66
"main": "index.js",

src/components/NumberInput/NumberInput.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const NumberInput = ({
2323
quote,
2424
containerStyle,
2525
onChange,
26+
hasDecimal,
2627
iconLeft,
2728
iconRight,
2829
hasError,
@@ -36,6 +37,19 @@ const NumberInput = ({
3637
const [handleChange, error, setField] = useError(hasError);
3738
const isEmptyStringFieldValue = field?.value === '';
3839

40+
const handleChangeValue = newValue => {
41+
const formatedValue = hasDecimal ? newValue : Number(newValue);
42+
const changedValue = newValue === '' ? '' : formatedValue;
43+
if (field) {
44+
setField(field.name, changedValue, form.setFieldValue);
45+
} else {
46+
handleChange(
47+
{target: {id: name, value: changedValue}},
48+
onChange
49+
);
50+
}
51+
};
52+
3953
return (
4054
<FormControl
4155
name={field?.name || name}
@@ -53,16 +67,7 @@ const NumberInput = ({
5367
variant={variant}
5468
value={field?.value === 0 ? field.value : field?.value || value}
5569
onKeyDown={e => e.key === 'e' && e.preventDefault()}
56-
onChange={newValue => {
57-
if (field) {
58-
setField(field.name, newValue === '' ? '' : Number(newValue), form.setFieldValue);
59-
} else {
60-
handleChange(
61-
{target: {id: name, value: newValue === '' ? '' : Number(newValue)}},
62-
onChange
63-
);
64-
}
65-
}}
70+
onChange={handleChangeValue}
6671
{...props}
6772
>
6873
<NumberInputField data-testid={`input-${field?.name || name}`} placeholder={placeholder}/>
@@ -79,6 +84,7 @@ const NumberInput = ({
7984

8085
NumberInput.propTypes = {
8186
name: PropTypes.string,
87+
hasDecimal: PropTypes.bool,
8288
placeholder: PropTypes.string,
8389
variant: PropTypes.string,
8490
isDisabled: PropTypes.bool,
@@ -105,6 +111,7 @@ NumberInput.propTypes = {
105111
};
106112

107113
NumberInput.defaultProps = {
114+
hasDecimal: false,
108115
name: undefined,
109116
width: undefined,
110117
containerStyle: {},

src/components/NumberInput/NumberInput.test.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import {getByTestId} from '@testing-library/react';
2-
1+
import {fireEvent, getByTestId} from '@testing-library/react';
32
import {NumberInput} from '@/components';
43

54
describe('<NumberInput>', () => {
65
let props;
76
const getComponent = () => render(NumberInput, props, {formik: {}});
87
beforeEach(() => {
98
props = {
10-
name: 'test'
9+
name: 'test',
10+
onChange: jest.fn()
1111
};
1212
});
1313
afterEach(tearDown);
@@ -34,4 +34,32 @@ describe('<NumberInput>', () => {
3434
expect(label).toHaveTextContent('Test');
3535
});
3636
});
37+
38+
describe('when `props.hasDecimal` is `true`', () => {
39+
beforeEach(() => {
40+
props.hasDecimal = true;
41+
const {container} = getComponent();
42+
const input = getByTestId(container, 'input-test');
43+
fireEvent.change(input, {target: {value: 11.22}});
44+
});
45+
46+
it('should fire `props.onChange` with value as string', () => {
47+
expect(props.onChange).toHaveBeenCalled();
48+
expect(props.onChange).toHaveBeenCalledWith({target: {id: 'test', value: '11.22'}});
49+
});
50+
});
51+
52+
describe('when `props.hasDecimal` is `false`', () => {
53+
beforeEach(() => {
54+
props.hasDecimal = false;
55+
const {container} = getComponent();
56+
const input = getByTestId(container, 'input-test');
57+
fireEvent.change(input, {target: {value: 11.22}});
58+
});
59+
60+
it('should fire `props.onChange` with value as number', () => {
61+
expect(props.onChange).toHaveBeenCalled();
62+
expect(props.onChange).toHaveBeenCalledWith({target: {id: 'test', value: 11.22}});
63+
});
64+
});
3765
});

0 commit comments

Comments
 (0)