|
1 | 1 | /*! |
2 | | - * react-number-format - 1.1.2 |
| 2 | + * react-number-format - 1.2.0 |
3 | 3 | * Author : Sudhanshu Yadav |
4 | 4 | * Copyright (c) 2016,2017 to Sudhanshu Yadav - ignitersworld.com , released under the MIT license. |
5 | 5 | */ |
@@ -99,13 +99,17 @@ return /******/ (function(modules) { // webpackBootstrap |
99 | 99 | format: _react.PropTypes.oneOfType([_react.PropTypes.string, _react.PropTypes.func]), |
100 | 100 | mask: _react.PropTypes.string, |
101 | 101 | 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 |
103 | 106 | }; |
104 | 107 |
|
105 | 108 | var defaultProps = { |
106 | 109 | displayType: 'input', |
107 | 110 | decimalSeparator: '.', |
108 | | - decimalPrecision: false |
| 111 | + decimalPrecision: false, |
| 112 | + allowNegative: true |
109 | 113 | }; |
110 | 114 |
|
111 | 115 | var NumberFormat = function (_React$Component) { |
@@ -230,22 +234,42 @@ return /******/ (function(modules) { // webpackBootstrap |
230 | 234 | prefix = _props3.prefix, |
231 | 235 | suffix = _props3.suffix, |
232 | 236 | mask = _props3.mask, |
233 | | - format = _props3.format; |
| 237 | + format = _props3.format, |
| 238 | + allowNegative = _props3.allowNegative, |
| 239 | + decimalPrecision = _props3.decimalPrecision; |
234 | 240 |
|
235 | 241 | var _getSeparators2 = this.getSeparators(), |
236 | 242 | thousandSeparator = _getSeparators2.thousandSeparator, |
237 | 243 | decimalSeparator = _getSeparators2.decimalSeparator; |
238 | 244 |
|
239 | | - var decimalPrecision = this.props.decimalPrecision; |
240 | | - |
241 | 245 | var maskPattern = format && typeof format == 'string' && !!mask; |
242 | | - |
243 | 246 | var numRegex = this.getNumberRegex(true); |
| 247 | + var hasNegative = void 0, |
| 248 | + removeNegative = void 0; |
244 | 249 |
|
245 | 250 | //change val to string if its number |
246 | 251 | if (typeof val === 'number') val = val + ''; |
247 | 252 |
|
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 | + |
249 | 273 | var num = val.match(numRegex).join(''); |
250 | 274 |
|
251 | 275 | var formattedValue = num; |
@@ -286,11 +310,13 @@ return /******/ (function(modules) { // webpackBootstrap |
286 | 310 | if (prefix) beforeDecimal = prefix + beforeDecimal; |
287 | 311 | if (suffix) afterDecimal = afterDecimal + suffix; |
288 | 312 |
|
| 313 | + if (hasNegative && !removeNegative) beforeDecimal = '-' + beforeDecimal; |
| 314 | + |
289 | 315 | formattedValue = beforeDecimal + (hasDecimals && decimalSeparator || '') + afterDecimal; |
290 | 316 | } |
291 | 317 |
|
292 | 318 | return { |
293 | | - value: formattedValue.match(numRegex).join(''), |
| 319 | + value: (hasNegative && !removeNegative ? '-' : '') + formattedValue.match(numRegex).join(''), |
294 | 320 | formattedValue: formattedValue |
295 | 321 | }; |
296 | 322 | } |
@@ -359,15 +385,16 @@ return /******/ (function(modules) { // webpackBootstrap |
359 | 385 | var key = e.key; |
360 | 386 |
|
361 | 387 | var numRegex = this.getNumberRegex(false, decimalPrecision !== false); |
| 388 | + var negativeRegex = new RegExp('-'); |
362 | 389 | //Handle backspace and delete against non numerical/decimal characters |
363 | 390 | if (selectionEnd - selectionStart === 0) { |
364 | | - if (key === 'Delete' && !numRegex.test(value[selectionStart])) { |
| 391 | + if (key === 'Delete' && !numRegex.test(value[selectionStart]) && !negativeRegex.test(value[selectionStart])) { |
365 | 392 | e.preventDefault(); |
366 | 393 | var nextCursorPosition = selectionStart; |
367 | 394 | while (!numRegex.test(value[nextCursorPosition]) && nextCursorPosition < value.length) { |
368 | 395 | nextCursorPosition++; |
369 | 396 | }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])) { |
371 | 398 | e.preventDefault(); |
372 | 399 | var prevCursorPosition = selectionStart; |
373 | 400 | while (!numRegex.test(value[prevCursorPosition - 1]) && prevCursorPosition > 0) { |
|
0 commit comments