Skip to content

Commit

Permalink
- Fixed #597 (Handle backspace before prefix, when negative value is …
Browse files Browse the repository at this point in the history
…present)

- Integrate testing-library
  • Loading branch information
s-yadav committed Dec 1, 2021
1 parent fdafbf1 commit 97366f0
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 32 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.6.2",
"@rollup/plugin-buble": "^0.21.3",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"@types/react": "^16.9.4",
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
Expand Down
42 changes: 21 additions & 21 deletions src/number_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,33 +740,33 @@ class NumberFormat extends React.Component {
const { value: lastValue } = this.state;

if (input) {
//set caret position, and value imperatively when element is provided
if (setCaretPosition) {
//calculate caret position if not defined
if (!caretPos) {
const inputValue = params.inputValue || input.value;
//calculate caret position if not defined
if (caretPos === undefined && setCaretPosition) {
const inputValue = params.inputValue || input.value;

const currentCaretPosition = getCurrentCaretPosition(input);
const currentCaretPosition = getCurrentCaretPosition(input);

/**
* set the value imperatively, this is required for IE fix
* This is also required as if new caret position is beyond the previous value.
* Caret position will not be set correctly
*/
input.value = formattedValue;
/**
* set the value imperatively, this is required for IE fix
* This is also required as if new caret position is beyond the previous value.
* Caret position will not be set correctly
*/
input.value = formattedValue;

//get the caret position
caretPos = this.getCaretPosition(inputValue, formattedValue, currentCaretPosition);
}
//get the caret position
caretPos = this.getCaretPosition(inputValue, formattedValue, currentCaretPosition);
}

/**
* set the value imperatively, as we set the caret position as well imperatively.
* This is to keep value and caret position in sync
*/
input.value = formattedValue;

//set caret position, and value imperatively when element is provided
if (setCaretPosition) {
//set caret position
this.setPatchedCaretPosition(input, caretPos, formattedValue);
} else {
/**
* if we are not setting caret position set the value imperatively.
* This is required on onBlur method
*/
input.value = formattedValue;
}
}

Expand Down
33 changes: 22 additions & 11 deletions test/library/keypress_and_caret.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import NumberFormat from '../../src/number_format';
import ReactDOM from 'react-dom';
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import {
simulateKeyInput,
Expand Down Expand Up @@ -249,19 +251,28 @@ describe('Test keypress and caret position changes', () => {
expect(caretPos).toEqual(2);
});

it('should allow removing negation(-), even if its before suffix', () => {
it('should allow removing negation(-), even if its before suffix', async () => {
const spy = jasmine.createSpy();
wrapper.setProps({
suffix: '',
prefix: '$',
value: '-$1,000',
onValueChange: spy,
});
wrapper.update();

simulateKeyInput(wrapper.find('input'), 'Backspace', 2, 2, setSelectionRange);
expect(wrapper.state().value).toEqual('$1,000');
expect(caretPos).toEqual(1);
const view = render(
<NumberFormat
thousandSeparator=","
suffix=""
prefix="$"
value="-$1,000"
onValueChange={spy}
data-testid="currency-input"
/>,
);

const input = await view.findByTestId('currency-input');

input.setSelectionRange(2, 2);

userEvent.type(input, '{backspace}');

expect(input.value).toEqual('$1,000');
expect(input.selectionStart).toEqual(1);
expect(spy).toHaveBeenCalled();
});
});
Expand Down
Loading

0 comments on commit 97366f0

Please sign in to comment.