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

Fix whole number replacement when using fixedDecimalScale and prefix/suffix #228

Closed
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
9 changes: 6 additions & 3 deletions dist/react-number-format.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,9 @@ function (_React$Component) {
var _this$props10 = this.props,
format = _this$props10.format,
decimalSeparator = _this$props10.decimalSeparator,
allowNegative = _this$props10.allowNegative;
allowNegative = _this$props10.allowNegative,
prefix = _this$props10.prefix,
suffix = _this$props10.suffix;
var lastNumStr = this.state.numAsString || '';
var _this$selectionBefore = this.selectionBeforeInput,
selectionStart = _this$selectionBefore.selectionStart,
Expand All @@ -1097,7 +1099,7 @@ function (_React$Component) {
*/


if (value.length > lastValue.length || !value.length || start === end || start === 0 && end === lastValue.length || selectionStart === 0 && selectionEnd === lastValue.length) {
if (value.length > lastValue.length || !value.length || start === end || start <= prefix.length && end >= lastValue.length - suffix.length || selectionStart <= prefix.length && selectionEnd >= lastValue.length - suffix.length) {
return value;
} //if format got deleted reset the value to last value

Expand Down Expand Up @@ -1341,7 +1343,8 @@ function (_React$Component) {
_el$value3 = el.value,
value = _el$value3 === void 0 ? '' : _el$value3;

var caretPosition = _this4.correctCaretPosition(value, selectionStart);
var caretPosition = _this4.correctCaretPosition(value, selectionStart); //setPatchedCaretPosition only when everything is not selected on focus (while tabbing into the field)


if (caretPosition !== selectionStart && !(selectionStart === 0 && selectionEnd === value.length)) {
_this4.setPatchedCaretPosition(el, caretPosition, value);
Expand Down
9 changes: 6 additions & 3 deletions dist/react-number-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,9 @@
var _this$props10 = this.props,
format = _this$props10.format,
decimalSeparator = _this$props10.decimalSeparator,
allowNegative = _this$props10.allowNegative;
allowNegative = _this$props10.allowNegative,
prefix = _this$props10.prefix,
suffix = _this$props10.suffix;
var lastNumStr = this.state.numAsString || '';
var _this$selectionBefore = this.selectionBeforeInput,
selectionStart = _this$selectionBefore.selectionStart,
Expand All @@ -1103,7 +1105,7 @@
*/


if (value.length > lastValue.length || !value.length || start === end || start === 0 && end === lastValue.length || selectionStart === 0 && selectionEnd === lastValue.length) {
if (value.length > lastValue.length || !value.length || start === end || start <= prefix.length && end >= lastValue.length - suffix.length || selectionStart <= prefix.length && selectionEnd >= lastValue.length - suffix.length) {
return value;
} //if format got deleted reset the value to last value

Expand Down Expand Up @@ -1347,7 +1349,8 @@
_el$value3 = el.value,
value = _el$value3 === void 0 ? '' : _el$value3;

var caretPosition = _this4.correctCaretPosition(value, selectionStart);
var caretPosition = _this4.correctCaretPosition(value, selectionStart); //setPatchedCaretPosition only when everything is not selected on focus (while tabbing into the field)


if (caretPosition !== selectionStart && !(selectionStart === 0 && selectionEnd === value.length)) {
_this4.setPatchedCaretPosition(el, caretPosition, value);
Expand Down
2 changes: 1 addition & 1 deletion dist/react-number-format.min.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions lib/number_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,9 @@ function (_React$Component) {
var _this$props10 = this.props,
format = _this$props10.format,
decimalSeparator = _this$props10.decimalSeparator,
allowNegative = _this$props10.allowNegative;
allowNegative = _this$props10.allowNegative,
prefix = _this$props10.prefix,
suffix = _this$props10.suffix;
var lastNumStr = this.state.numAsString || '';
var _this$selectionBefore = this.selectionBeforeInput,
selectionStart = _this$selectionBefore.selectionStart,
Expand All @@ -646,7 +648,7 @@ function (_React$Component) {
*/


if (value.length > lastValue.length || !value.length || start === end || start === 0 && end === lastValue.length || selectionStart === 0 && selectionEnd === lastValue.length) {
if (value.length > lastValue.length || !value.length || start === end || start <= prefix.length && end >= lastValue.length - suffix.length || selectionStart <= prefix.length && selectionEnd >= lastValue.length - suffix.length) {
return value;
} //if format got deleted reset the value to last value

Expand Down Expand Up @@ -890,7 +892,8 @@ function (_React$Component) {
_el$value3 = el.value,
value = _el$value3 === void 0 ? '' : _el$value3;

var caretPosition = _this4.correctCaretPosition(value, selectionStart);
var caretPosition = _this4.correctCaretPosition(value, selectionStart); //setPatchedCaretPosition only when everything is not selected on focus (while tabbing into the field)


if (caretPosition !== selectionStart && !(selectionStart === 0 && selectionEnd === value.length)) {
_this4.setPatchedCaretPosition(el, caretPosition, value);
Expand Down
6 changes: 3 additions & 3 deletions src/number_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ class NumberFormat extends React.Component {
* It will also work as fallback if android chome keyDown handler does not work
**/
correctInputValue(caretPos: number, lastValue: string, value: string) {
const {format, decimalSeparator, allowNegative} = this.props;
const {format, decimalSeparator, allowNegative, prefix, suffix} = this.props;
const lastNumStr = this.state.numAsString || '';
const {selectionStart, selectionEnd} = this.selectionBeforeInput;
const {start, end} = findChangedIndex(lastValue, value);
Expand All @@ -595,8 +595,8 @@ class NumberFormat extends React.Component {
value.length > lastValue.length
|| !value.length ||
start === end ||
(start === 0 && end === lastValue.length) ||
(selectionStart === 0 && selectionEnd === lastValue.length)
(start <= prefix.length && end >= lastValue.length - suffix.length) ||
Copy link
Owner

@s-yadav s-yadav Sep 22, 2018

Choose a reason for hiding this comment

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

  • ^update the comment
  • create two variables, and check for weather format pattern is not defined.
const leftBound = !!format ? 0 : prefix.length;
const rightBound = lastValue.length - (!!format ? 0 : suffix.length);

Copy link
Owner

Choose a reason for hiding this comment

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

Also the condition start <= prefix.length && end >= lastValue.length - suffix.length will return half formatted value when multiple character is used for prefix/ suffix. For example if you used $$ as prefix.
and the value is $$3.00. And if you replace $3.00 with character 4. This method will return $4 which this method is trying to avoid. This might cause some other bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The point about partially selected prefixes/suffixes is very valid. In my manual tests the overall behaviour in such a case still seemed to be fine, but since I do not know the whole context here I am now suggesting a more defensive approach.
Only adding one additional condition for returning early from correctInputValue(), which is if exactly the number without prefix/suffix is selected/changed. If parts of prefix/suffix are part of the selection, this results in no change to the value.

(selectionStart <= prefix.length && selectionEnd >= lastValue.length - suffix.length)
) {
return value;
}
Expand Down
17 changes: 17 additions & 0 deletions test/library/input_numeric_format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,23 @@ describe('Test NumberFormat as input with numeric format options', () => {
expect(wrapper.state().value).toEqual('$1,234,000');
});

it(`should allow replacing all digits (without prefix and/or suffix) with input
digit(s) when decimalScale and fixedDecimalScale is defined. Issue #159`, () => {
const wrapper = shallow(<NumberFormat prefix="$" thousandSeparator={true} decimalScale={2} fixedDecimalScale={true} value="$1,234.00"/>);
simulateKeyInput(wrapper.find('input'), '56', 1, 9);
expect(wrapper.state().value).toEqual('$56.00');

wrapper.setProps({prefix: '', suffix: '%', value: '98.76%'});
wrapper.update();
simulateKeyInput(wrapper.find('input'), '1', 0, 5);
expect(wrapper.state().value).toEqual('1.00%');

wrapper.setProps({prefix: '$', value: '$23.00%'});
wrapper.update();
simulateKeyInput(wrapper.find('input'), '15', 1, 6);
expect(wrapper.state().value).toEqual('$15.00%');
});

it('should not round by default', () => {
const wrapper = shallow(<NumberFormat/>);
const input = wrapper.find('input');
Expand Down