-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathreact-currency-input.cjs.js
427 lines (350 loc) · 14.7 KB
/
react-currency-input.cjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var PropTypes = _interopDefault(require('prop-types'));
var React = require('react');
var React__default = _interopDefault(React);
var ReactDOM = _interopDefault(require('react-dom'));
Object.assign = Object.assign ||
function(target) {
var arguments$1 = arguments;
for (var i = 1; i < arguments.length; i++) {
var source = arguments$1[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
function mask(value, precision, decimalSeparator, thousandSeparator, allowNegative, prefix, suffix){
if ( precision === void 0 ) precision = 2;
if ( decimalSeparator === void 0 ) decimalSeparator = '.';
if ( thousandSeparator === void 0 ) thousandSeparator = ',';
if ( allowNegative === void 0 ) allowNegative = false;
if ( prefix === void 0 ) prefix = '';
if ( suffix === void 0 ) suffix = '';
// provide some default values and arg validation.
if (precision < 0) { precision = 0; } // precision cannot be negative
if (precision > 20) { precision = 20; } // precision cannot be greater than 20
if (value === null || value===undefined) {
return {
value: 0,
maskedValue: ''
};
}
value = String(value); //if the given value is a Number, let's convert into String to manipulate that
if (value.length == 0) {
return {
value: 0,
maskedValue: ''
};
}
// extract digits. if no digits, fill in a zero.
var digits = value.match(/\d/g) || ['0'];
var numberIsNegative = false;
if (allowNegative) {
var negativeSignCount = (value.match(/-/g) || []).length;
// number will be negative if we have an odd number of "-"
// ideally, we should only ever have 0, 1 or 2 (positive number, making a number negative
// and making a negative number positive, respectively)
numberIsNegative = negativeSignCount % 2 === 1;
// if every digit in the array is '0', then the number should never be negative
var allDigitsAreZero = true;
for (var idx=0; idx < digits.length; idx += 1) {
if(digits[idx] !== '0') {
allDigitsAreZero = false;
break;
}
}
if (allDigitsAreZero) {
numberIsNegative = false;
}
}
// zero-pad a input
while (digits.length <= precision) { digits.unshift('0'); }
if (precision > 0) {
// add the decimal separator
digits.splice(digits.length - precision, 0, ".");
}
// clean up extraneous digits like leading zeros.
digits = Number(digits.join('')).toFixed(precision).split('');
var raw = Number(digits.join(''));
var decimalpos = digits.length - precision - 1; // -1 needed to position the decimal separator before the digits.
if (precision > 0) {
// set the final decimal separator
digits[decimalpos] = decimalSeparator;
} else {
// when precision is 0, there is no decimal separator.
decimalpos = digits.length;
}
// add in any thousand separators
for (var x=decimalpos - 3; x > 0; x = x - 3) {
digits.splice(x, 0, thousandSeparator);
}
// if we have a prefix or suffix, add them in.
if (prefix.length > 0) { digits.unshift(prefix); }
if (suffix.length > 0) { digits.push(suffix); }
// if the number is negative, insert a "-" to
// the front of the array and negate the raw value
if (allowNegative && numberIsNegative) {
digits.unshift('-');
raw = -raw;
}
return {
value: raw,
maskedValue: digits.join('').trim()
};
}
// IE* parseFloat polyfill
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseFloat#Polyfill
Number.parseFloat = parseFloat;
var CurrencyInput = (function (Component$$1) {
function CurrencyInput(props) {
Component$$1.call(this, props);
this.prepareProps = this.prepareProps.bind(this);
this.handleChange = this.handleChange.bind(this);
this.handleFocus = this.handleFocus.bind(this);
this.setSelectionRange = this.setSelectionRange.bind(this);
this.state = this.prepareProps(this.props);
this.inputSelectionStart = 1;
this.inputSelectionEnd = 1;
}
if ( Component$$1 ) CurrencyInput.__proto__ = Component$$1;
CurrencyInput.prototype = Object.create( Component$$1 && Component$$1.prototype );
CurrencyInput.prototype.constructor = CurrencyInput;
/**
* Exposes the current masked value.
*
* @returns {String}
*/
CurrencyInput.prototype.getMaskedValue = function getMaskedValue () {
return this.state.maskedValue;
};
/**
* General function used to cleanup and define the final props used for rendering
* @returns {{ maskedValue: {String}, value: {Number}, customProps: {Object} }}
*/
CurrencyInput.prototype.prepareProps = function prepareProps (props) {
var customProps = Object.assign({}, props); // babeljs converts to Object.assign, then polyfills.
delete customProps.onChange;
delete customProps.onChangeEvent;
delete customProps.value;
delete customProps.decimalSeparator;
delete customProps.thousandSeparator;
delete customProps.precision;
delete customProps.inputType;
delete customProps.allowNegative;
delete customProps.allowEmpty;
delete customProps.prefix;
delete customProps.suffix;
delete customProps.selectAllOnFocus;
delete customProps.autoFocus;
var initialValue = props.value;
if (initialValue === null) {
initialValue = props.allowEmpty? null : '';
}else{
if (typeof initialValue == 'string') {
// Some people, when confronted with a problem, think "I know, I'll use regular expressions."
// Now they have two problems.
// Strip out thousand separators, prefix, and suffix, etc.
if (props.thousandSeparator === "."){
// special handle the . thousand separator
initialValue = initialValue.replace(/\./g, '');
}
if (props.decimalSeparator != "."){
// fix the decimal separator
initialValue = initialValue.replace(new RegExp(props.decimalSeparator, 'g'), '.');
}
//Strip out anything that is not a digit, -, or decimal separator
initialValue = initialValue.replace(/[^0-9-.]/g, '');
// now we can parse.
initialValue = Number.parseFloat(initialValue);
}
initialValue = Number(initialValue).toLocaleString(undefined, {
style : 'decimal',
minimumFractionDigits: props.precision,
maximumFractionDigits: props.precision
});
}
var ref = mask(
initialValue,
props.precision,
props.decimalSeparator,
props.thousandSeparator,
props.allowNegative,
props.prefix,
props.suffix
);
var maskedValue = ref.maskedValue;
var value = ref.value;
return { maskedValue: maskedValue, value: value, customProps: customProps };
};
/**
* Component lifecycle function.
* Invoked when a component is receiving new props. This method is not called for the initial render.
*
* @param nextProps
* @see https://facebook.github.io/react/docs/component-specs.html#updating-componentwillreceiveprops
*/
CurrencyInput.prototype.componentWillReceiveProps = function componentWillReceiveProps (nextProps) {
this.setState(this.prepareProps(nextProps));
};
/**
* Component lifecycle function.
* @returns {XML}
* @see https://facebook.github.io/react/docs/react-component.html#componentdidmount
*/
CurrencyInput.prototype.componentDidMount = function componentDidMount (){
var node = ReactDOM.findDOMNode(this.theInput);
if (this.props.autoFocus) {
this.theInput.focus();
} else {
}
// this.setSelectionRange(node, selectionStart, selectionEnd);
};
/**
* Component lifecycle function
* @returns {XML}
* @see https://facebook.github.io/react/docs/react-component.html#componentwillupdate
*/
CurrencyInput.prototype.componentWillUpdate = function componentWillUpdate () {
var node = ReactDOM.findDOMNode(this.theInput);
this.inputSelectionStart = node.selectionStart;
this.inputSelectionEnd = node.selectionEnd;
};
/**
* Component lifecycle function.
* @returns {XML}
* @see https://facebook.github.io/react/docs/react-component.html#componentdidupdate
*/
CurrencyInput.prototype.componentDidUpdate = function componentDidUpdate (prevProps, prevState){
var ref = this.props;
var decimalSeparator = ref.decimalSeparator;
var node = ReactDOM.findDOMNode(this.theInput);
var isNegative = (this.theInput.value.match(/-/g) || []).length % 2 === 1;
var minPos = this.props.prefix.length + (isNegative ? 1 : 0);
var selectionEnd = Math.max(minPos, Math.min(this.inputSelectionEnd, this.theInput.value.length - this.props.suffix.length));
var selectionStart = Math.max(minPos, Math.min(this.inputSelectionEnd, selectionEnd));
var regexEscapeRegex = /[-[\]{}()*+?.,\\^$|#\s]/g;
var separatorsRegex = new RegExp(decimalSeparator.replace(regexEscapeRegex, '\\$&') + '|' + this.props.thousandSeparator.replace(regexEscapeRegex, '\\$&'), 'g');
var currSeparatorCount = (this.state.maskedValue.match(separatorsRegex) || []).length;
var prevSeparatorCount = (prevState.maskedValue.match(separatorsRegex) || []).length;
var adjustment = Math.max(currSeparatorCount - prevSeparatorCount, 0);
selectionEnd = selectionEnd + adjustment;
selectionStart = selectionStart + adjustment;
var precision = Number(this.props.precision);
var baselength = this.props.suffix.length
+ this.props.prefix.length
+ (precision > 0 ? decimalSeparator.length : 0) // if precision is 0 there will be no decimal part
+ precision
+ 1; // This is to account for the default '0' value that comes before the decimal separator
if (this.state.maskedValue.length == baselength){
// if we are already at base length, position the cursor at the end.
selectionEnd = this.theInput.value.length - this.props.suffix.length;
selectionStart = selectionEnd;
}
// this.setSelectionRange(node, selectionStart, selectionEnd);
this.inputSelectionStart = selectionStart;
this.inputSelectionEnd = selectionEnd;
};
/**
* Set selection range only if input is in focused state
* @param node DOMElement
* @param start number
* @param end number
*/
CurrencyInput.prototype.setSelectionRange = function setSelectionRange (node, start, end) {
if (document.activeElement === node) {
node.setSelectionRange(start, end);
}
};
/**
* onChange Event Handler
* @param event
*/
CurrencyInput.prototype.handleChange = function handleChange (event) {
var this$1 = this;
event.preventDefault();
var ref = mask(
event.target.value,
this.props.precision,
this.props.decimalSeparator,
this.props.thousandSeparator,
this.props.allowNegative,
this.props.prefix,
this.props.suffix
);
var maskedValue = ref.maskedValue;
var value = ref.value;
event.persist(); // fixes issue #23
this.setState({ maskedValue: maskedValue, value: value }, function () {
this$1.props.onChange(maskedValue, value, event);
this$1.props.onChangeEvent(event, maskedValue, value);
});
};
/**
* onFocus Event Handler
* @param event
*/
CurrencyInput.prototype.handleFocus = function handleFocus (event) {
if (!this.theInput) { return; }
//Whenever we receive focus check to see if the position is before the suffix, if not, move it.
var selectionEnd = this.theInput.value.length - this.props.suffix.length;
var isNegative = (this.theInput.value.match(/-/g) || []).length % 2 === 1;
var selectionStart = this.props.prefix.length + (isNegative ? 1 : 0);
this.props.selectAllOnFocus && event.target.setSelectionRange(selectionStart, selectionEnd);
this.inputSelectionStart = selectionStart;
this.inputSelectionEnd = selectionEnd;
};
CurrencyInput.prototype.handleBlur = function handleBlur (event) {
this.inputSelectionStart = 0;
this.inputSelectionEnd = 0;
};
/**
* Component lifecycle function.
* @returns {XML}
* @see https://facebook.github.io/react/docs/component-specs.html#render
*/
CurrencyInput.prototype.render = function render () {
var this$1 = this;
return (
React__default.createElement( 'input', Object.assign({},
{ ref: function (input) { this$1.theInput = input; }, type: this.props.inputType, value: this.state.maskedValue, onChange: this.handleChange, onFocus: this.handleFocus, onMouseUp: this.handleFocus }, this.state.customProps))
)
};
return CurrencyInput;
}(React.Component));
/**
* Prop validation.
* @see https://facebook.github.io/react/docs/component-specs.html#proptypes
*/
CurrencyInput.propTypes = {
onChange: PropTypes.func,
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
decimalSeparator: PropTypes.string,
thousandSeparator: PropTypes.string,
precision: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
inputType: PropTypes.string,
allowNegative: PropTypes.bool,
allowEmpty: PropTypes.bool,
prefix: PropTypes.string,
suffix: PropTypes.string,
selectAllOnFocus: PropTypes.bool
};
CurrencyInput.defaultProps = {
onChange: function(maskValue, value, event) {/*no-op*/},
onChangeEvent: function(event, maskValue, value) {/*no-op*/},
autoFocus: false,
value: '0',
decimalSeparator: '.',
thousandSeparator: ',',
precision: '2',
inputType: 'text',
allowNegative: false,
prefix: '',
suffix: '',
selectAllOnFocus: false
};
module.exports = CurrencyInput;
//# sourceMappingURL=react-currency-input.cjs.js.map