@ttk/component
Version:
ttk组件库
205 lines (164 loc) • 6.21 kB
JavaScript
import { _ as _extends } from '../extends-b1af4ff7.js';
import { _ as _defineProperty } from '../defineProperty-847730aa.js';
import React__default, { Component } from 'react';
import { _ as _inherits, a as _getPrototypeOf, b as _possibleConstructorReturn, c as _classCallCheck, d as _createClass } from '../getPrototypeOf-b95655c5.js';
import { InputNumber } from 'antd';
import classNames from 'classnames';
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; } }
var InputNumberComponent = /*#__PURE__*/function (_Component) {
_inherits(InputNumberComponent, _Component);
var _super = _createSuper(InputNumberComponent);
function InputNumberComponent() {
var _this;
_classCallCheck(this, InputNumberComponent);
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.state = {
dafaultValue: _this.props.dafaultValue || '',
focusStatus: false
};
_this.formatter = function (value) {
var regex = /(\d)(?=(?:\d{3})+$)/g;
if ("".concat(value).indexOf('.') > -1) {
regex = /(\d{1,3})(?=(\d{3})+(?:\.))/g;
}
value = "".concat(value).replace(regex, "$1,");
if (_this.props.clearZero === true && _this.state.focusStatus !== 'changing') {
value = "".concat(value).replace(/(?:\.0*|(\.\d+?)0+)$/, '$1');
}
return value;
};
_this.parser = function (value) {
var precision = _this.props.precision;
if (!_this.preValue) _this.preValue = '';
if (!precision) precision = 6;
if (value === '-' || value === '-0' || value === '-0.') {
_this.preValue = value;
return value;
}
if (value === '00') {
_this.preValue = '0';
return '0';
}
if (value === '-00') {
_this.preValue = '-0';
return '-0';
}
if (value === '--' || value == '-.') {
_this.preValue = '-';
return '-';
}
value = "".concat(value).replace(/\$\s?|(,*)/g, '');
if (!isNaN(Number(value))) {
if (value.indexOf('.') > -1) {
var len = "".concat(value).length - "".concat(value).indexOf('.') - 1;
if (len > precision) {
return _this.preValue;
}
}
_this.preValue = value;
return value;
} else {
return _this.preValue;
}
};
_this.onChange = function (value) {
// console.log(this.inputRef, 'this.inputRef')
_this.setState({
focusStatus: 'changing'
});
if (_this.preValue === '-0' || _this.preValue === '-0.') {
value = _this.preValue;
}
var onChange = _this.props.onChange;
onChange && onChange(value);
};
_this.onBlur = function (e) {
_this.setState({
focusStatus: false
});
var _this$props = _this.props,
onBlur = _this$props.onBlur,
clearZero = _this$props.clearZero;
e.target.value = "".concat(e.target.value).replace(/,/g, '');
if (clearZero === true) {
if (e.target.value) {
e.target.value = "".concat(e.target.value).replace(/(?:\.0*|(\.\d+?)0+)$/, '$1');
}
}
onBlur && onBlur(e);
};
_this.onFocus = function (e) {
_this.setState({
focusStatus: true
});
var onFocus = _this.props.onFocus;
onFocus && onFocus(e);
};
return _this;
}
_createClass(InputNumberComponent, [{
key: "componentDidMount",
value: function componentDidMount() {
this.preValue = this.inputRef.inputNumberRef.state.inputValue || '';
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if (nextProps.dafaultValue != this.state.dafaultValue) {
this.setState({
dafaultValue: nextProps.dafaultValue
});
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.preValue = '';
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var dafaultValue = this.state.dafaultValue;
var title;
if (Object.keys(this.props).includes('title')) {
title = this.props.title || '';
} else if (Object.keys(this.props).includes('value')) {
title = this.formatter(this.props.value || '');
} else {
title = this.formatter(this.props.defaultValue || '');
}
return /*#__PURE__*/React__default.createElement("div", {
title: title
}, /*#__PURE__*/React__default.createElement(InputNumber, _extends({}, this.props, {
defaultValue: dafaultValue,
formatter: this.formatter,
parser: this.parser,
onChange: this.onChange,
onBlur: this.onBlur,
onFocus: this.onFocus,
ref: function ref(_ref) {
return _this2.inputRef = _ref;
}
})));
}
}]);
return InputNumberComponent;
}(Component);
InputNumberComponent.defaultProps = {
dafaultValue: ''
};
function AntInputNumberComponent(props) {
var className = classNames(_defineProperty({
'mk-ant-input-number': true
}, props.className, !!props.className));
return /*#__PURE__*/React__default.createElement(InputNumberComponent, _extends({}, props, {
className: className
}));
}
export { AntInputNumberComponent as default };