Skip to content

Commit 65a932a

Browse files
committed
Allow negative numbers
1 parent 528c385 commit 65a932a

File tree

8 files changed

+179
-65
lines changed

8 files changed

+179
-65
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Or get compiled development and production version from ./dist
2020
| thousandSeparator | mixed: single character string or true/false (boolean) | false | Add thousand separators on number |
2121
| decimalSeparator | mixed: single character string or true/false (boolean)| . | Support decimal point on a number |
2222
| decimalPrecision | mixed: number or boolean | false (2 if true)| If false it does not limit decimal place, if true default precision is 2 or else limits to provided decimal place |
23+
| allowNegative | boolean | true | allow negative numbers (Only when format option is not provided) |
2324
| prefix | String (ex : $) | none | Add a prefix before the number |
2425
| suffix | String (ex : /-) | none | Add a prefix after the number |
2526
| value | Number | null | Value to number format |
@@ -111,6 +112,9 @@ All custom input props and number input props are passed together.
111112
[http://codepen.io/s-yadav/pen/bpKNMa](http://codepen.io/s-yadav/pen/bpKNMa)
112113

113114
### Major Updates
115+
### v1.2.0
116+
- Support negative numbers
117+
114118
### v1.1.0
115119
- Support custom input
116120
- Support custom decimal / thousandSeparator

dist/react-number-format.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* react-number-format - 1.1.2
2+
* react-number-format - 1.2.0
33
* Author : Sudhanshu Yadav
44
* Copyright (c) 2016,2017 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license.
55
*/
@@ -99,13 +99,17 @@ return /******/ (function(modules) { // webpackBootstrap
9999
format: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.func]),
100100
mask: _react.PropTypes.string,
101101
value: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
102-
customInput: _react.PropTypes.func
102+
customInput: _react.PropTypes.func,
103+
allowNegative: _react.PropTypes.bool,
104+
onKeyDown: _react.PropTypes.func,
105+
onChange: _react.PropTypes.func
103106
};
104107

105108
var defaultProps = {
106109
displayType: 'input',
107110
decimalSeparator: '.',
108-
decimalPrecision: false
111+
decimalPrecision: false,
112+
allowNegative: true
109113
};
110114

111115
var NumberFormat = function (_React$Component) {
@@ -230,22 +234,42 @@ return /******/ (function(modules) { // webpackBootstrap
230234
prefix = _props3.prefix,
231235
suffix = _props3.suffix,
232236
mask = _props3.mask,
233-
format = _props3.format;
237+
format = _props3.format,
238+
allowNegative = _props3.allowNegative,
239+
decimalPrecision = _props3.decimalPrecision;
234240

235241
var _getSeparators2 = this.getSeparators(),
236242
thousandSeparator = _getSeparators2.thousandSeparator,
237243
decimalSeparator = _getSeparators2.decimalSeparator;
238244

239-
var decimalPrecision = this.props.decimalPrecision;
240-
241245
var maskPattern = format && typeof format == 'string' && !!mask;
242-
243246
var numRegex = this.getNumberRegex(true);
247+
var hasNegative = void 0,
248+
removeNegative = void 0;
244249

245250
//change val to string if its number
246251
if (typeof val === 'number') val = val + '';
247252

248-
if (!val || !val.match(numRegex)) return { value: '', formattedValue: maskPattern ? '' : '' };
253+
var negativeRegex = new RegExp('(-)');
254+
var doubleNegativeRegex = new RegExp('(-)(.)*(-)');
255+
256+
if (allowNegative && !format) {
257+
// Check number has '-' value
258+
hasNegative = negativeRegex.test(val);
259+
// Check number has 2 or more '-' values
260+
removeNegative = doubleNegativeRegex.test(val);
261+
}
262+
263+
var valMatch = val && val.match(numRegex);
264+
265+
if (!valMatch && removeNegative) {
266+
return { value: '', formattedValue: '' };
267+
} else if (!valMatch && hasNegative) {
268+
return { value: '', formattedValue: '-' };
269+
} else if (!valMatch) {
270+
return { value: '', formattedValue: maskPattern ? '' : '' };
271+
}
272+
249273
var num = val.match(numRegex).join('');
250274

251275
var formattedValue = num;
@@ -286,11 +310,13 @@ return /******/ (function(modules) { // webpackBootstrap
286310
if (prefix) beforeDecimal = prefix + beforeDecimal;
287311
if (suffix) afterDecimal = afterDecimal + suffix;
288312

313+
if (hasNegative && !removeNegative) beforeDecimal = '-' + beforeDecimal;
314+
289315
formattedValue = beforeDecimal + (hasDecimals && decimalSeparator || '') + afterDecimal;
290316
}
291317

292318
return {
293-
value: formattedValue.match(numRegex).join(''),
319+
value: (hasNegative && !removeNegative ? '-' : '') + formattedValue.match(numRegex).join(''),
294320
formattedValue: formattedValue
295321
};
296322
}
@@ -359,15 +385,16 @@ return /******/ (function(modules) { // webpackBootstrap
359385
var key = e.key;
360386

361387
var numRegex = this.getNumberRegex(false, decimalPrecision !== false);
388+
var negativeRegex = new RegExp('-');
362389
//Handle backspace and delete against non numerical/decimal characters
363390
if (selectionEnd - selectionStart === 0) {
364-
if (key === 'Delete' && !numRegex.test(value[selectionStart])) {
391+
if (key === 'Delete' && !numRegex.test(value[selectionStart]) && !negativeRegex.test(value[selectionStart])) {
365392
e.preventDefault();
366393
var nextCursorPosition = selectionStart;
367394
while (!numRegex.test(value[nextCursorPosition]) && nextCursorPosition < value.length) {
368395
nextCursorPosition++;
369396
}this.setCaretPosition(el, nextCursorPosition);
370-
} else if (key === 'Backspace' && !numRegex.test(value[selectionStart - 1])) {
397+
} else if (key === 'Backspace' && !numRegex.test(value[selectionStart - 1]) && !negativeRegex.test(value[selectionStart - 1])) {
371398
e.preventDefault();
372399
var prevCursorPosition = selectionStart;
373400
while (!numRegex.test(value[prevCursorPosition - 1]) && prevCursorPosition > 0) {

dist/react-number-format.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ class App extends React.Component {
8181
ThousandSeperator: ',', decimalSeparator='.', decimalPrecision:2
8282
</div>
8383
<div>
84-
<NumberFormat thousandSeparator={","} decimalSeparator={"."} decimalPrecision={2} />
84+
<NumberFormat thousandSeparator={","} decimalSeparator={"."} decimalPrecision={2} />
8585
</div>
8686
<br/>
8787
<div>
8888
ThousandSeperator: '.', decimalSeparator=',', decimalPrecision:2
8989
</div>
9090
<div>
91-
<NumberFormat thousandSeparator={"."} decimalSeparator={","} decimalPrecision={2} />
91+
<NumberFormat thousandSeparator={"."} decimalSeparator={","} decimalPrecision={2} />
9292
</div>
9393
</div>
9494

0 commit comments

Comments
 (0)