Skip to content

Format function should support decimal #327

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 5 additions & 7 deletions dist/react-number-format.es.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* react-number-format - 4.0.8
* react-number-format - 4.0.9
* Author : Sudhanshu Yadav
* Copyright (c) 2016, 2019 to Sudhanshu Yadav, released under the MIT license.
* https://github.com/s-yadav/react-number-format
Expand Down Expand Up @@ -508,7 +508,7 @@ function (_React$Component) {
var _this$getSeparators2 = this.getSeparators(),
decimalSeparator = _this$getSeparators2.decimalSeparator;

return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && !format ? '|' + escapeRegExp(decimalSeparator) : ''), g ? 'g' : undefined);
return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && (!format || typeof format === "function") ? '|' + escapeRegExp(decimalSeparator) : ''), g ? 'g' : undefined);
}
}, {
key: "getSeparators",
Expand Down Expand Up @@ -736,16 +736,14 @@ function (_React$Component) {
removeFormatting = _this$props4.removeFormatting;
if (!val) return val;

if (!format) {
val = this.removePrefixAndSuffix(val);
val = this.getFloatString(val);
} else if (typeof format === 'string') {
if (typeof format === 'string') {
val = this.removePatternFormatting(val);
} else if (typeof removeFormatting === 'function') {
//condition need to be handled if format method is provide,
val = removeFormatting(val);
} else {
val = (val.match(/\d/g) || []).join('');
val = this.removePrefixAndSuffix(val);
val = this.getFloatString(val);
}

return val;
Expand Down
12 changes: 5 additions & 7 deletions dist/react-number-format.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* react-number-format - 4.0.8
* react-number-format - 4.0.9
* Author : Sudhanshu Yadav
* Copyright (c) 2016, 2019 to Sudhanshu Yadav, released under the MIT license.
* https://github.com/s-yadav/react-number-format
Expand Down Expand Up @@ -514,7 +514,7 @@
var _this$getSeparators2 = this.getSeparators(),
decimalSeparator = _this$getSeparators2.decimalSeparator;

return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && !format ? '|' + escapeRegExp(decimalSeparator) : ''), g ? 'g' : undefined);
return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && (!format || typeof format === "function") ? '|' + escapeRegExp(decimalSeparator) : ''), g ? 'g' : undefined);
}
}, {
key: "getSeparators",
Expand Down Expand Up @@ -742,16 +742,14 @@
removeFormatting = _this$props4.removeFormatting;
if (!val) return val;

if (!format) {
val = this.removePrefixAndSuffix(val);
val = this.getFloatString(val);
} else if (typeof format === 'string') {
if (typeof format === 'string') {
val = this.removePatternFormatting(val);
} else if (typeof removeFormatting === 'function') {
//condition need to be handled if format method is provide,
val = removeFormatting(val);
} else {
val = (val.match(/\d/g) || []).join('');
val = this.removePrefixAndSuffix(val);
val = this.getFloatString(val);
}

return val;
Expand Down
2 changes: 1 addition & 1 deletion dist/react-number-format.min.js

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions lib/number_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function (_React$Component) {
var _this$getSeparators2 = this.getSeparators(),
decimalSeparator = _this$getSeparators2.decimalSeparator;

return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && !format ? '|' + (0, _utils.escapeRegExp)(decimalSeparator) : ''), g ? 'g' : undefined);
return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && (!format || typeof format === "function") ? '|' + (0, _utils.escapeRegExp)(decimalSeparator) : ''), g ? 'g' : undefined);
}
}, {
key: "getSeparators",
Expand Down Expand Up @@ -423,16 +423,14 @@ function (_React$Component) {
removeFormatting = _this$props4.removeFormatting;
if (!val) return val;

if (!format) {
val = this.removePrefixAndSuffix(val);
val = this.getFloatString(val);
} else if (typeof format === 'string') {
if (typeof format === 'string') {
val = this.removePatternFormatting(val);
} else if (typeof removeFormatting === 'function') {
//condition need to be handled if format method is provide,
val = removeFormatting(val);
} else {
val = (val.match(/\d/g) || []).join('');
val = this.removePrefixAndSuffix(val);
val = this.getFloatString(val);
}

return val;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-number-format",
"description": "React component to format number in an input or as a text.",
"version": "4.0.8",
"version": "4.0.9",
"main": "lib/number_format.js",
"module": "dist/react-number-format.es.js",
"author": "Sudhanshu Yadav",
Expand Down
10 changes: 4 additions & 6 deletions src/number_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class NumberFormat extends React.Component {
getNumberRegex(g: boolean, ignoreDecimalSeparator?: boolean) {
const {format, decimalScale} = this.props;
const {decimalSeparator} = this.getSeparators();
return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && !format ? '|' + escapeRegExp(decimalSeparator) : ''), g ? 'g' : undefined);
return new RegExp('\\d' + (decimalSeparator && decimalScale !== 0 && !ignoreDecimalSeparator && (!format || typeof format === "function") ? '|' + escapeRegExp(decimalSeparator) : ''), g ? 'g' : undefined);
}

getSeparators() {
Expand Down Expand Up @@ -417,15 +417,13 @@ class NumberFormat extends React.Component {
const {format, removeFormatting} = this.props;
if (!val) return val;

if (!format) {
val = this.removePrefixAndSuffix(val);
val = this.getFloatString(val);
} else if (typeof format === 'string') {
if (typeof format === 'string') {
val = this.removePatternFormatting(val);
} else if (typeof removeFormatting === 'function') { //condition need to be handled if format method is provide,
val = removeFormatting(val);
} else {
val = (val.match(/\d/g) || []).join('')
val = this.removePrefixAndSuffix(val);
Copy link
Owner

Choose a reason for hiding this comment

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

We should not handle other formatting props, when format function is provided.

val = this.getFloatString(val);
}
return val;
}
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 @@ -312,6 +312,13 @@ describe('NumberFormat as input', () => {
expect(() => {
shallow(<NumberFormat format="#### #### ####" mask={['D', 'D', 'M', '1', '2', 'Y', 'Y', 'Y']}/>)
}).toThrow()
});

it('should allow to use decimal separator for a format prop as a function', () => {
const wrapper = shallow(<NumberFormat format={val => val}/>);

simulateKeyInput(wrapper.find('input'), '123.24', 0);
expect(wrapper.state().value).toEqual('123.24');
})
})
});