Skip to content

Commit

Permalink
fix: unable to empty value when setting precision (#309)
Browse files Browse the repository at this point in the history
* fix: unable to empty value when setting precision

* test: add test

* test: add onChange test
  • Loading branch information
Hale Deng authored Mar 27, 2021
1 parent 163d16f commit a728b34
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/utils/MiniDecimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ export default function getMiniDecimal(value: ValueType): DecimalClass {
* Align the logic of toFixed to around like 1.5 => 2
*/
export function toFixed(numStr: string, separatorStr: string, precision?: number) {
if (numStr === '') {
return '';
}
const { negativeStr, integerStr, decimalStr } = trimNumber(numStr);
const precisionDecimalStr = `${separatorStr}${decimalStr}`;

Expand Down
15 changes: 15 additions & 0 deletions tests/decimal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,20 @@ describe('InputNumber.Decimal', () => {

expect(wrapper.getInputValue()).toEqual('3');
});

it('should empty value after removing value', () => {
const onChange = jest.fn();
const wrapper = mount(<InputNumber precision={2} onChange={onChange} />);

wrapper.focusInput();
wrapper.changeValue('3');
wrapper.changeValue('');

expect(wrapper.getInputValue()).toEqual('');

wrapper.blurInput();
expect(onChange).toHaveBeenCalledWith(null);
expect(wrapper.getInputValue()).toEqual('');
});
});
});
4 changes: 4 additions & 0 deletions tests/util.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,9 @@ describe('InputNumber.Util', () => {
expect(toFixed('1.2', '.')).toEqual('1.2');
expect(toFixed('1.000', '.')).toEqual('1');
});

it('should return "" when input is ""', () => {
expect(toFixed('', '.')).toEqual('');
});
});
});

1 comment on commit a728b34

@vercel
Copy link

@vercel vercel bot commented on a728b34 Mar 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.