Skip to content

Fix issue for bug #452. Be able to delete / hit backspace on highligh… #473

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

Merged
merged 5 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 30 additions & 10 deletions src/number_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,6 @@ class NumberFormat extends React.Component {
return false;
}

checkIfFormatGotDeleted(start: number, end: number, value: string) {
for (let i = start; i < end; i++) {
if (this.isCharacterAFormat(i, value)) return true;
}
return false;
}

/**
* This will check if any formatting got removed by the delete or backspace and reset the value
* It will also work as fallback if android chome keyDown handler does not work
Expand Down Expand Up @@ -644,11 +637,38 @@ class NumberFormat extends React.Component {
return value;
}

//if format got deleted reset the value to last value
if (this.checkIfFormatGotDeleted(start, end, lastValue)) {
value = lastValue;
// check whether the deleted portion has a non numeric field
// this means that it may be a format pattern symbol or a separator
const deletedValues = lastValue.substr(start, end - start);
const hasNonNumeric = !![...deletedValues].find((deletedVal, idx) => this.isCharacterAFormat(idx + start, lastValue));

// if it has, only remove the numeric portion
if(hasNonNumeric) {
const deletedValuePortion = lastValue.substr(start)
const recordIndexOfFormatCharacters = {};
const resolvedPortion = [];
[...deletedValuePortion].forEach((currentPortion, idx) => {
if(this.isCharacterAFormat(idx + start, lastValue)){
recordIndexOfFormatCharacters[idx] = currentPortion;
} else if (idx > deletedValues.length - 1) {
resolvedPortion.push(currentPortion);
}
})

Object.keys(recordIndexOfFormatCharacters).forEach(idx => {
if(resolvedPortion.length > idx){
resolvedPortion.splice(idx, 0, recordIndexOfFormatCharacters[idx]);
} else {
resolvedPortion.push(recordIndexOfFormatCharacters[idx])
}
})

value = lastValue.substr(0, start) + resolvedPortion.join('');
Copy link
Owner

Choose a reason for hiding this comment

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

The problem is the format character can have numeric value in itself.
This logic makes the assumption that there will be no numeric char on format itself.

Copy link
Contributor Author

@rossjackson rossjackson Dec 18, 2020

Choose a reason for hiding this comment

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

My comment was misleading. I wasn't checking if the characters that were removed were numeric. I corrected it and also created unit test to back it up

}




//for numbers check if beforeDecimal got deleted and there is nothing after decimal,
//clear all numbers in such case while keeping the - sign
if (!format) {
Expand Down
21 changes: 11 additions & 10 deletions test/library/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,23 @@ describe('NumberFormat as input', () => {
simulateKeyInput(wrapper.find('input'), 'Backspace', 0, 4);
expect(wrapper.state().value).toEqual('+1 (123) 456 7 89 US');

//when format and number character are delted
wrapper.setProps({value: '+1 (999) 999 9 99 US'})
simulateKeyInput(wrapper.find('input'), 'Backspace', 6, 10);
expect(wrapper.state().value).toEqual('+1 (999) 999 9 99 US');

//when group of characters (including format character) is replaced with number
wrapper.setProps({value: '+1 (888) 888 8 88 US'});
simulateKeyInput(wrapper.find('input'), '8', 6, 10);
expect(wrapper.state().value).toEqual('+1 (888) 888 8 88 US');

//when a format character is replaced with number
wrapper.setProps({value: '+1 (777) 777 7 77 US'});
simulateKeyInput(wrapper.find('input'), '8', 8, 9);
expect(wrapper.state().value).toEqual('+1 (777) 777 7 77 US');
});

it('should update value if group of characters got deleted with format', () => {
const wrapper = shallow(<NumberFormat format="+1 (###) ### # ## US" value="+1 (999) 999 9 99 US"/>);
simulateKeyInput(wrapper.find('input'), 'Backspace', 6, 10);
expect(wrapper.state().value).toEqual('+1 (999) 999 9 US');

//when group of characters (including format character) is replaced with number
wrapper.setProps({value: '+1 (888) 888 8 88 US'});
simulateKeyInput(wrapper.find('input'), '8', 6, 10);
expect(wrapper.state().value).toEqual('+1 (888) 888 8 8 US');
})

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