UNPKG

@ttk/component

Version:

ttk组件库

117 lines (93 loc) 4.13 kB
import { _ as _extends } from '../extends-b1af4ff7.js'; import { _ as _inherits, a as _getPrototypeOf, b as _possibleConstructorReturn, c as _classCallCheck, d as _createClass } from '../getPrototypeOf-b95655c5.js'; import React__default from 'react'; import { Input, Tooltip } from 'antd'; import '../_commonjsHelpers-471920d6.js'; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _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) { _inherits(NumericInput, _React$Component); var _super = _createSuper(NumericInput); function NumericInput() { var _this; _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; } _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.createElement(Input, _extends({}, this.props, { onChange: this.onChange, onBlur: this.onBlur, maxLength: max || 25 })); } var title = value ? /*#__PURE__*/React__default.createElement("span", { className: "numeric-input-title" }, value !== '-' ? formatNumber(value) : '-') : placeholder || 'Input a number'; return /*#__PURE__*/React__default.createElement(Tooltip, { trigger: ['focus'], title: title, placement: "topLeft", overlayClassName: "numeric-input" }, /*#__PURE__*/React__default.createElement(Input, _extends({}, this.props, { onChange: this.onChange, onBlur: this.onBlur, maxLength: max || 25 }))); } }]); return NumericInput; }(React__default.Component); export { NumericInput as default };