UNPKG

react-numberinput

Version:

React Number input component. Allow to input formatted number values. Also you can use this component as example of inputCore component with reformat user function

115 lines (114 loc) • 4.97 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var react_maskinput_1 = require("react-maskinput"); function removeSelectedRange(value, selection) { if (selection.start === selection.end) { return value; } if (selection.end < selection.start) { var tmp = selection.end; selection.end = selection.start; selection.start = tmp; } if (value.length > selection.start) { return value.slice(0, selection.start).concat(value.slice(selection.end, value.length)); } return value; } /** * Component that allow to format only numbers. (5 000, 123 456 789, etc.) * This component work on top of react-maskinput and define custom formatting function called `reformat`. * Also you can use this component as example to create you own components based on react-maskinput. */ var NumberInput = /** @class */ (function (_super) { __extends(NumberInput, _super); function NumberInput() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.applyValue = null; _this.reformat = function (_a) { var value = _a.value, _b = _a.input, input = _b === void 0 ? '' : _b, selection = _a.selection; var newSelection = { start: selection.start, end: selection.end, }; var data = removeSelectedRange(value.replace(/(\D)/g, function (text) { return (text === ' ' ? ' ' : ''); }), newSelection); var inputValue = input.replace(/\D/g, ''); var oldLength = data.length; data = data.slice(0, newSelection.start) + inputValue + data.slice(newSelection.start, data.length); var spaces = data.match(/\s/g) || []; var oldSpacesCount = spaces.length; var newSpacesCount = 0; data = data.replace(/\s/g, '').replace(/(\d)(?=(\d\d\d)+(?!\d))/g, function (text) { newSpacesCount++; return text + " "; }); var index = newSelection.start + Math.min(0, newSpacesCount - oldSpacesCount); if (inputValue) { index = Math.max(0, data.length - oldLength + index); } newSelection.end = newSelection.start = index; return { value: data, maskedValue: data, visibleValue: data, selection: newSelection, }; }; _this.handleLeadingZeros = function (e) { if (_this.props.removeOnlyZeroString) { _this.applyValue(e.target.value.replace(/^[0 ]+$/, '0')); } else { var result = e.target.value.replace(/^[0 ]+/, ''); _this.applyValue(result.trim() ? result.trim() : '0'); } _this.props.onBlur && _this.props.onBlur(e); }; _this.getCallback = function (applyValue) { _this.applyValue = applyValue; }; return _this; } NumberInput.prototype.render = function () { var _a = this.props, removeOnlyZeroString = _a.removeOnlyZeroString, rest = __rest(_a, ["removeOnlyZeroString"]); return (React.createElement(react_maskinput_1.default, __assign({}, rest, { reformat: this.reformat, onBlur: this.handleLeadingZeros, getApplyValueCallback: this.getCallback }))); }; return NumberInput; }(React.Component)); exports.default = NumberInput;