Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test case for full value replacement if the new value is the same as the last number of the previous value #517

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/number_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,18 +641,23 @@ class NumberFormat extends React.Component {
return value.substr(0, selectionStart) + separator + value.substr(selectionStart + 1, value.length);
}

/* don't do anyhting if something got added,
or if value is empty string (when whole input is cleared)
or whole input is replace with a number
*/

const leftBound = !!format ? 0 : prefix.length;
const rightBound = lastValue.length - (!!format ? 0 : suffix.length);

if (
value.length > lastValue.length
|| !value.length ||
// don't do anything if something got added
value.length > lastValue.length ||
// or if the new value is an empty string
!value.length ||
// or if nothing has changed, in which case start will be same as end
start === end ||
// or in case if whole input is selected and new value is typed
(selectionStart === 0 && selectionEnd === lastValue.length) ||
// or in case if the whole content is replaced by browser, example (autocomplete)
(start === 0 && end === lastValue.length) ||
// or if charcters between prefix and suffix is selected.
// For numeric inputs we apply the format so, prefix and suffix can be ignored
(selectionStart === leftBound && selectionEnd === rightBound)
) {
return value;
Expand Down
7 changes: 7 additions & 0 deletions test/library/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ describe('NumberFormat as input', () => {
expect(wrapper.find('input').prop('value')).toEqual('+1 (012) 345 6 78 US');
});

it('should replace the whole value if a new number is typed after selecting the everything', () => {
const wrapper = shallow(<NumberFormat prefix="$" value="10" allowedDecimalSeparators={[",", "."]} />);

simulateKeyInput(wrapper.find('input'), '0', 0, 3);

expect(wrapper.find('input').prop('value')).toEqual('$0')
})
it('should allow replacing all characters with number when formatting is present', () => {
const format = '+1 (###) ### # ## US';
const wrapper = shallow(<NumberFormat format={format} value="+1 (123) 456 7 89 US" mask="_"/>);
Expand Down