@ttk/component
Version:
ttk组件库
125 lines (97 loc) • 4.45 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var _extends = require('../extends-63327b06.js');
var getPrototypeOf = require('../getPrototypeOf-3a7a3df9.js');
var React = require('react');
var antd = require('antd');
require('../_commonjsHelpers-badc9712.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = getPrototypeOf._getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = getPrototypeOf._getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return getPrototypeOf._possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function formatNumber(value) {
value += '';
var list = value.split('.');
var prefix = list[0].charAt(0) === '-' ? '-' : '';
var num = prefix ? list[0].slice(1) : list[0];
var result = '';
while (num.length > 3) {
result = ",".concat(num.slice(-3)).concat(result);
num = num.slice(0, num.length - 3);
}
if (num) {
result = num + result;
}
return "".concat(prefix).concat(result).concat(list[1] ? ".".concat(list[1]) : '');
}
var NumericInput = /*#__PURE__*/function (_React$Component) {
getPrototypeOf._inherits(NumericInput, _React$Component);
var _super = _createSuper(NumericInput);
function NumericInput() {
var _this;
getPrototypeOf._classCallCheck(this, NumericInput);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_this.onChange = function (e) {
var value = e.target.value;
var _this$props = _this.props,
onChange = _this$props.onChange,
match = _this$props.match;
var myReg = match === 'int' ? /^[0-9]*$/ : /^-?(0|[1-9][0-9]*)(\.[0-9]*)?$/;
if (!Number.isNaN(value) && myReg.test(value) || value === '' || value === '-') {
onChange(value);
}
};
_this.onBlur = function () {
var _this$props2 = _this.props,
value = _this$props2.value,
onBlur = _this$props2.onBlur,
onChange = _this$props2.onChange,
notZero = _this$props2.notZero;
value = String(value);
if (notZero && Number(value) === 0) {
onChange('');
} else if (value && value.charAt(value.length - 1) === '.' || value === '-') {
onChange(value.slice(0, -1));
}
if (onBlur) {
onBlur();
}
};
return _this;
}
getPrototypeOf._createClass(NumericInput, [{
key: "render",
value: function render() {
var _this$props3 = this.props,
value = _this$props3.value,
hideTip = _this$props3.hideTip,
max = _this$props3.max,
placeholder = _this$props3.placeholder;
if (hideTip) {
return /*#__PURE__*/React__default["default"].createElement(antd.Input, _extends._extends({}, this.props, {
onChange: this.onChange,
onBlur: this.onBlur,
maxLength: max || 25
}));
}
var title = value ? /*#__PURE__*/React__default["default"].createElement("span", {
className: "numeric-input-title"
}, value !== '-' ? formatNumber(value) : '-') : placeholder || 'Input a number';
return /*#__PURE__*/React__default["default"].createElement(antd.Tooltip, {
trigger: ['focus'],
title: title,
placement: "topLeft",
overlayClassName: "numeric-input"
}, /*#__PURE__*/React__default["default"].createElement(antd.Input, _extends._extends({}, this.props, {
onChange: this.onChange,
onBlur: this.onBlur,
maxLength: max || 25
})));
}
}]);
return NumericInput;
}(React__default["default"].Component);
exports["default"] = NumericInput;