choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
965 lines (802 loc) • 32.6 kB
JavaScript
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _bignumber = _interopRequireDefault(require("bignumber.js"));
var _isString = _interopRequireDefault(require("lodash/isString"));
var _noop = _interopRequireDefault(require("lodash/noop"));
var _isNil = _interopRequireDefault(require("lodash/isNil"));
var _isNumber = _interopRequireDefault(require("lodash/isNumber"));
var _dataset = require("choerodon-ui/dataset");
var _KeyCode = _interopRequireDefault(require("../../_util/KeyCode"));
var _InputHandler = _interopRequireDefault(require("./InputHandler"));
var _icon = _interopRequireDefault(require("../../icon"));
var _EventManager = require("../../_util/EventManager");
function defaultParser(input) {
return input.replace(/[^\w\.-]+/g, '');
}
/**
* When click and hold on a button - the speed of auto changin the value.
*/
var SPEED = 200;
/**
* When click and hold on a button - the delay before auto changin the value.
*/
var DELAY = 600;
var isEqual = function isEqual(oldValue, newValue) {
return newValue === oldValue || (!(0, _isString["default"])(newValue) || !newValue.endsWith('.')) && (!(0, _isString["default"])(oldValue) || !oldValue.endsWith('.')) && _dataset.math.eq(newValue, oldValue) || (0, _isNumber["default"])(newValue) && (0, _isNumber["default"])(oldValue) && _dataset.math.isNaN(newValue) && _dataset.math.isNaN(oldValue);
};
var InputNumber = /*#__PURE__*/function (_Component) {
(0, _inherits2["default"])(InputNumber, _Component);
var _super = (0, _createSuper2["default"])(InputNumber);
function InputNumber(_props) {
var _this;
(0, _classCallCheck2["default"])(this, InputNumber);
_this = _super.call(this, _props);
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onKeyDown", function (e) {
var _this$props = _this.props,
onKeyDown = _this$props.onKeyDown,
onPressEnter = _this$props.onPressEnter;
if (e.keyCode === _KeyCode["default"].UP) {
var ratio = _this.getRatio(e);
_this.up(e, ratio);
_this.stop();
} else if (e.keyCode === _KeyCode["default"].DOWN) {
var _ratio = _this.getRatio(e);
_this.down(e, _ratio);
_this.stop();
} else if (e.keyCode === _KeyCode["default"].ENTER && onPressEnter) {
onPressEnter(e);
} // Trigger user key down
_this.recordCursorPosition();
_this.lastKeyCode = e.keyCode;
if (onKeyDown) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
onKeyDown.apply(void 0, [e].concat(args));
}
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onKeyUp", function (e) {
_this.stop();
var onKeyUp = _this.props.onKeyUp;
_this.recordCursorPosition(); // Trigger user key up
if (onKeyUp) {
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
onKeyUp.apply(void 0, [e].concat(args));
}
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChange", function (e) {
var _this$props2 = _this.props,
onChange = _this$props2.onChange,
value = _this$props2.value;
var stateValue = _this.state.value;
if (_this.state.focused) {
_this.inputting = true;
} // 限制数字输入框只能输入数字
var newValue = _this.props.parser(_this.getValueFromEvent(e));
if (isNaN("".concat(newValue, "0"))) newValue = _this.rawInput !== undefined ? _this.rawInput : stateValue !== undefined ? stateValue : value;
_this.rawInput = newValue;
if (_this.composing) {
_this.setState({
inputValue: _this.getValueFromEvent(e)
});
} else {
_this.setState({
inputValue: _this.rawInput
});
onChange(_this.toNumber(_this.rawInput)); // valid number or invalid string
}
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onMouseUp", function () {
var onMouseUp = _this.props.onMouseUp;
_this.recordCursorPosition();
if (onMouseUp) {
onMouseUp.apply(void 0, arguments);
}
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onFocus", function () {
var _this$props3;
_this.setState({
focused: true
});
(_this$props3 = _this.props).onFocus.apply(_this$props3, arguments);
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onBlur", function (e) {
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
args[_key3 - 1] = arguments[_key3];
}
_this.inputting = false;
_this.setState({
focused: false
});
var value = _this.getCurrentValidValue(_this.state.inputValue);
e.persist();
_this.setValue(value, function (newValue) {
var onBlur = _this.props.onBlur;
if (onBlur) {
var originValue = _this.input.value;
var inputValue = _this.getInputDisplayValue({
focus: false,
value: newValue
});
_this.input.value = inputValue;
onBlur.apply(void 0, [e].concat(args));
_this.input.value = originValue;
}
});
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onComposition", function (e) {
if (e.type === 'compositionstart') {
_this.composing = true;
} else if (e.type === 'compositionend') {
_this.composing = false;
_this.onChange(e);
}
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getInputDisplayValue", function (state) {
var _ref = state || _this.state,
focused = _ref.focused,
inputValue = _ref.inputValue,
value = _ref.value;
var inputDisplayValue;
if (focused) {
inputDisplayValue = inputValue;
} else {
inputDisplayValue = _this.toPrecisionAsStep(value);
}
if (inputDisplayValue === undefined || inputDisplayValue === null) {
inputDisplayValue = '';
}
var inputDisplayValueFormat = _this.formatWrapper(inputDisplayValue);
if (!(0, _isNil["default"])(_this.props.decimalSeparator)) {
inputDisplayValueFormat = inputDisplayValueFormat.toString().replace('.', _this.props.decimalSeparator);
}
return inputDisplayValueFormat;
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "recordCursorPosition", function () {
// Record position
try {
_this.cursorStart = _this.input.selectionStart;
_this.cursorEnd = _this.input.selectionEnd;
_this.currentValue = _this.input.value;
_this.cursorBefore = _this.input.value.substring(0, _this.cursorStart);
_this.cursorAfter = _this.input.value.substring(_this.cursorEnd);
} catch (e) {// Fix error in Chrome:
// Failed to read the 'selectionStart' property from 'HTMLInputElement'
// http://stackoverflow.com/q/21177489/3040605
}
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "restoreByAfter", function (str) {
if (str === undefined) return false;
var fullStr = _this.input.value;
var index = fullStr.lastIndexOf(str);
if (index === -1) return false;
var prevCursorPos = _this.cursorBefore.length;
if (_this.lastKeyCode === _KeyCode["default"].DELETE && _this.cursorBefore.charAt(prevCursorPos - 1) === str[0]) {
_this.fixCaret(prevCursorPos, prevCursorPos);
return true;
}
if (index + str.length === fullStr.length) {
_this.fixCaret(index, index);
return true;
}
return false;
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "partRestoreByAfter", function (str) {
if (str === undefined) return false; // For loop from full str to the str with last char to map. e.g. 123
// -> 123
// -> 23
// -> 3
return Array.prototype.some.call(str, function (_, start) {
var partStr = str.substring(start);
return _this.restoreByAfter(partStr);
});
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "stop", function () {
if (_this.autoStepTimer) {
clearTimeout(_this.autoStepTimer);
}
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "down", function (e, ratio, recursive) {
_this.pressingUpOrDown = true;
_this.step('down', e, ratio, recursive);
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "up", function (e, ratio, recursive) {
_this.pressingUpOrDown = true;
_this.step('up', e, ratio, recursive);
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "saveUp", function (node) {
_this.upHandler = node;
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "saveDown", function (node) {
_this.downHandler = node;
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "saveInput", function (node) {
_this.input = node;
});
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "renderSuffix", function () {
var props = (0, _objectSpread2["default"])({}, _this.props);
var prefixCls = props.prefixCls,
disabled = props.disabled,
readOnly = props.readOnly,
useTouch = props.useTouch,
upHandler = props.upHandler,
downHandler = props.downHandler;
var upDisabledClass = '';
var downDisabledClass = '';
var value = _this.state.value;
if (value || value === 0) {
if (!isNaN(value)) {
var val = new _bignumber["default"](value);
if (_dataset.math.gte(val, props.max)) {
upDisabledClass = "".concat(prefixCls, "-handler-up-disabled");
}
if (_dataset.math.lte(val, props.min)) {
downDisabledClass = "".concat(prefixCls, "-handler-down-disabled");
}
} else {
upDisabledClass = "".concat(prefixCls, "-handler-up-disabled");
downDisabledClass = "".concat(prefixCls, "-handler-down-disabled");
}
}
var editable = !props.readOnly && !props.disabled;
var upEvents;
var downEvents;
if (useTouch) {
upEvents = {
onTouchStart: editable && !upDisabledClass ? _this.up : _noop["default"],
onTouchEnd: _this.stop
};
downEvents = {
onTouchStart: editable && !downDisabledClass ? _this.down : _noop["default"],
onTouchEnd: _this.stop
};
} else {
upEvents = {
onMouseDown: editable && !upDisabledClass ? _this.up : _noop["default"],
onMouseUp: _this.stop,
onMouseLeave: _this.stop
};
downEvents = {
onMouseDown: editable && !downDisabledClass ? _this.down : _noop["default"],
onMouseUp: _this.stop,
onMouseLeave: _this.stop
};
}
var isUpDisabled = !!upDisabledClass || disabled || readOnly;
var isDownDisabled = !!downDisabledClass || disabled || readOnly;
return /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefixCls, "-handler-wrap")
}, /*#__PURE__*/_react["default"].createElement(_InputHandler["default"], (0, _extends2["default"])({
ref: _this.saveUp,
disabled: isUpDisabled,
prefixCls: prefixCls,
unselectable: "unselectable"
}, upEvents, {
role: "button",
"aria-label": "Increase Value",
"aria-disabled": !!isUpDisabled,
className: "".concat(prefixCls, "-handler ").concat(prefixCls, "-handler-up ").concat(upDisabledClass)
}), upHandler || /*#__PURE__*/_react["default"].createElement(_icon["default"], {
unselectable: "unselectable",
type: "baseline-arrow_drop_up",
className: "".concat(prefixCls, "-handler-up-inner"),
onClick: _EventManager.preventDefault
})), /*#__PURE__*/_react["default"].createElement(_InputHandler["default"], (0, _extends2["default"])({
ref: _this.saveDown,
disabled: isDownDisabled,
prefixCls: prefixCls,
unselectable: "unselectable"
}, downEvents, {
role: "button",
"aria-label": "Decrease Value",
"aria-disabled": !!isDownDisabled,
className: "".concat(prefixCls, "-handler ").concat(prefixCls, "-handler-down ").concat(downDisabledClass)
}), downHandler || /*#__PURE__*/_react["default"].createElement(_icon["default"], {
unselectable: "unselectable",
type: "baseline-arrow_drop_down",
className: "".concat(prefixCls, "-handler-down-inner"),
onClick: _EventManager.preventDefault
})));
});
var _value;
if ('value' in _props) {
_value = _props.value;
} else {
_value = _props.defaultValue;
}
_this.state = {
focused: _props.autoFocus
};
var validValue = _this.getValidValue(_this.toNumber(_value));
_this.state = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, _this.state), {}, {
inputValue: _this.toPrecisionAsStep(validValue),
value: validValue
});
return _this;
}
(0, _createClass2["default"])(InputNumber, [{
key: "componentDidMount",
value: function componentDidMount() {
this.componentDidUpdate();
}
}, {
key: "componentWillUpdate",
value: function componentWillUpdate() {
try {
this.start = this.input.selectionStart;
this.end = this.input.selectionEnd;
} catch (e) {// Fix error in Chrome:
// Failed to read the 'selectionStart' property from 'HTMLInputElement'
// http://stackoverflow.com/q/21177489/3040605
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props4 = this.props,
value = _this$props4.value,
onChange = _this$props4.onChange,
max = _this$props4.max,
min = _this$props4.min;
var focused = this.state.focused; // Don't trigger in componentDidMount
if (prevProps) {
if (!isEqual(prevProps.value, value) || !isEqual(prevProps.max, max) || !isEqual(prevProps.min, min)) {
var validValue = focused ? value : this.getValidValue(value);
var nextInputValue;
if (this.pressingUpOrDown) {
nextInputValue = validValue;
} else if (this.inputting) {
nextInputValue = this.rawInput;
} else {
nextInputValue = this.toPrecisionAsStep(validValue);
}
this.setState({
// eslint-disable-line
value: validValue,
inputValue: nextInputValue
});
} // Trigger onChange when max or min change
// https://github.com/ant-design/ant-design/issues/11574
var nextValue = 'value' in this.props ? value : this.state.value; // ref: null < 20 === true
// https://github.com/ant-design/ant-design/issues/14277
if (onChange && 'max' in this.props && !_dataset.math.eq(prevProps.max, max) && _dataset.math.isFinite(nextValue) && _dataset.math.gt(nextValue, max)) {
onChange(max);
}
if (onChange && 'min' in this.props && !_dataset.math.eq(prevProps.min, min) && _dataset.math.isFinite(nextValue) && _dataset.math.lt(nextValue, min)) {
onChange(min);
}
} // Restore cursor
try {
// Firefox set the input cursor after it get focused.
// This caused that if an input didn't init with the selection,
// set will cause cursor not correct when first focus.
// Safari will focus input if set selection. We need skip this.
if (this.cursorStart !== undefined && this.state.focused) {
// In most cases, the string after cursor is stable.
// We can move the cursor before it
if ( // If not match full str, try to match part of str
!this.partRestoreByAfter(this.cursorAfter) && this.state.value !== this.props.value) {
// If not match any of then, let's just keep the position
// TODO: Logic should not reach here, need check if happens
var pos = this.cursorStart + 1; // If not have last string, just position to the end
if (!this.cursorAfter) {
pos = this.input.value.length;
} else if (this.lastKeyCode === _KeyCode["default"].BACKSPACE) {
pos = this.cursorStart - 1;
} else if (this.lastKeyCode === _KeyCode["default"].DELETE) {
pos = this.cursorStart;
}
this.fixCaret(pos, pos);
} else if (this.currentValue === this.input.value) {
// Handle some special key code
switch (this.lastKeyCode) {
case _KeyCode["default"].BACKSPACE:
this.fixCaret(this.cursorStart - 1, this.cursorStart - 1);
break;
case _KeyCode["default"].DELETE:
this.fixCaret(this.cursorStart + 1, this.cursorStart + 1);
break;
default: // Do nothing
}
}
}
} catch (e) {// Do nothing
} // Reset last key
this.lastKeyCode = null; // pressingUpOrDown is true means that someone just click up or down button
if (!this.pressingUpOrDown) {
return;
}
if (this.props.focusOnUpDown && this.state.focused) {
var selectionRange = this.input.setSelectionRange;
if (selectionRange && typeof selectionRange === 'function' && this.start !== undefined && this.end !== undefined) {
this.input.setSelectionRange(this.start, this.end);
} else {
this.focus();
}
}
this.pressingUpOrDown = false;
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.stop();
}
}, {
key: "getCurrentValidValue",
value: function getCurrentValidValue(value) {
var val = value;
if (val === '') {
val = '';
} else if (!this.isNotCompleteNumber(val)) {
val = this.getValidValue(val);
} else {
val = this.state.value;
}
return this.toNumber(val);
}
}, {
key: "getRatio",
value: function getRatio(e) {
var ratio = 1;
if (e.metaKey || e.ctrlKey) {
ratio = 0.1;
} else if (e.shiftKey) {
ratio = 10;
}
return ratio;
}
}, {
key: "getValueFromEvent",
value: function getValueFromEvent(e) {
// optimize for chinese input expierence
var value = e.target.value.trim().replace(/。/g, '.');
if (!(0, _isNil["default"])(this.props.decimalSeparator)) {
value = value.replace(this.props.decimalSeparator, '.');
}
return value;
}
}, {
key: "getValidValue",
value: function getValidValue(value) {
var min = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.props.min;
var max = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.props.max;
if (_dataset.math.isNaN(value)) {
return value;
}
if (_dataset.math.lt(value, min)) {
value = min;
}
if (_dataset.math.gt(value, max)) {
value = max;
}
return value;
}
}, {
key: "setValue",
value: function setValue(v, callback) {
// trigger onChange
var precision = this.props.precision;
var newValue = this.isNotCompleteNumber(v) ? null : v;
var _this$state = this.state,
_this$state$value = _this$state.value,
value = _this$state$value === void 0 ? null : _this$state$value,
_this$state$inputValu = _this$state.inputValue,
inputValue = _this$state$inputValu === void 0 ? null : _this$state$inputValu;
var newValueInString = _dataset.math.isFinite(newValue) ? _dataset.math.toFixed(newValue, precision) : "".concat(newValue);
var changed = newValue !== value || newValueInString !== "".concat(inputValue);
if (!('value' in this.props)) {
this.setState({
value: newValue,
inputValue: this.toPrecisionAsStep(v)
}, callback ? function () {
return callback(newValue);
} : undefined);
} else {
// always set input value same as value
this.setState({
inputValue: this.toPrecisionAsStep(this.state.value)
}, callback ? function () {
return callback(newValue);
} : undefined);
}
if (changed) {
this.props.onChange(newValue);
}
return newValue;
}
}, {
key: "getPrecision",
value: function getPrecision(value) {
if (!(0, _isNil["default"])(this.props.precision)) {
return this.props.precision;
}
return _dataset.math.dp(value);
} // step={1.0} value={1.51}
// press +
// then value should be 2.51, rather than 2.5
// if this.props.precision is undefined
// https://github.com/react-component/input-number/issues/39
}, {
key: "getMaxPrecision",
value: function getMaxPrecision(currentValue) {
var ratio = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var precision = this.props.precision;
if (!(0, _isNil["default"])(precision)) {
return precision;
}
var step = this.props.step;
var ratioPrecision = this.getPrecision(ratio);
var stepPrecision = this.getPrecision(step);
if (!currentValue) {
return ratioPrecision + stepPrecision;
}
var currentValuePrecision = this.getPrecision(currentValue);
return Math.max(currentValuePrecision, ratioPrecision + stepPrecision);
}
}, {
key: "getPrecisionFactor",
value: function getPrecisionFactor(currentValue) {
var ratio = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
var precision = this.getMaxPrecision(currentValue, ratio);
return _dataset.math.pow(10, precision);
}
}, {
key: "fixCaret",
value: function fixCaret(start, end) {
if (start === undefined || end === undefined || !this.input || !this.input.value) {
return;
}
try {
var currentStart = this.input.selectionStart;
var currentEnd = this.input.selectionEnd;
if (start !== currentStart || end !== currentEnd) {
this.input.setSelectionRange(start, end);
}
} catch (e) {// Fix error in Chrome:
// Failed to read the 'selectionStart' property from 'HTMLInputElement'
// http://stackoverflow.com/q/21177489/3040605
}
}
}, {
key: "focus",
value: function focus() {
this.input.focus();
this.recordCursorPosition();
}
}, {
key: "blur",
value: function blur() {
this.input.blur();
}
}, {
key: "select",
value: function select() {
this.input.select();
}
}, {
key: "formatWrapper",
value: function formatWrapper(num) {
// http://2ality.com/2012/03/signedzero.html
if (_dataset.math.isNegativeZero(num)) {
return '-0';
}
if (this.props.formatter) {
return this.props.formatter(num);
}
return num;
}
}, {
key: "toPrecisionAsStep",
value: function toPrecisionAsStep(num) {
if (this.isNotCompleteNumber(num) || num === '') {
return num;
}
var precision = Math.abs(this.getMaxPrecision(num));
if (precision === 0) {
return _dataset.math.toString(num);
}
if (!isNaN(precision)) {
return _dataset.math.toFixed(num, precision);
}
return _dataset.math.toString(num);
} // '1.' '1x' 'xx' '' => are not complete numbers
}, {
key: "isNotCompleteNumber",
value: function isNotCompleteNumber(num) {
return _dataset.math.isNaN(num) || num === '' || num === null || num && num.toString().indexOf('.') === num.toString().length - 1;
}
}, {
key: "toNumber",
value: function toNumber(num) {
var precision = this.props.precision;
if (this.isNotCompleteNumber(num)) {
return num;
}
return _dataset.Utils.parseNumber(num, precision);
}
}, {
key: "upStep",
value: function upStep(val, rat) {
var _this$props5 = this.props,
step = _this$props5.step,
min = _this$props5.min;
var result;
if (_dataset.math.isFinite(val)) {
var precision = Math.abs(this.getMaxPrecision(val, rat));
result = _dataset.math.fix(new _bignumber["default"](_dataset.math.toFixed(_dataset.math.plus(val, step), precision)));
} else {
result = min === -Infinity ? step : min;
}
return this.toNumber(result);
}
}, {
key: "downStep",
value: function downStep(val, rat) {
var _this$props6 = this.props,
step = _this$props6.step,
min = _this$props6.min;
var result;
if (_dataset.math.isFinite(val)) {
var precision = Math.abs(this.getMaxPrecision(val, rat));
result = _dataset.math.fix(new _bignumber["default"](_dataset.math.toFixed(_dataset.math.minus(val, step), precision)));
} else {
result = min === -Infinity ? _dataset.math.negated(step) : min;
}
return this.toNumber(result);
}
}, {
key: "step",
value: function step(type, e) {
var _this2 = this;
var ratio = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
var recursive = arguments.length > 3 ? arguments[3] : undefined;
this.stop();
if (e) {
e.persist();
e.preventDefault();
}
var props = this.props;
if (props.disabled) {
return;
}
var value = this.getCurrentValidValue(this.state.inputValue) || 0;
if (this.isNotCompleteNumber(value)) {
return;
}
var val = this["".concat(type, "Step")](value, ratio);
var outOfRange;
if (_dataset.math.gt(val, props.max)) {
val = props.max;
outOfRange = true;
} else if (_dataset.math.lt(val, props.min)) {
val = props.min;
outOfRange = true;
}
this.setValue(val);
this.setState({
focused: true
});
if (outOfRange) {
return;
}
this.autoStepTimer = setTimeout(function () {
_this2[type](e, ratio, true);
}, recursive ? SPEED : DELAY);
}
}, {
key: "render",
value: function render() {
var _classNames;
var props = (0, _objectSpread2["default"])({}, this.props);
var prefixCls = props.prefixCls,
disabled = props.disabled,
_props$autoComplete = props.autoComplete,
autoComplete = _props$autoComplete === void 0 ? 'off' : _props$autoComplete,
_props$renderInput = props.renderInput,
renderInput = _props$renderInput === void 0 ? function (inputProps) {
return /*#__PURE__*/_react["default"].createElement("input", inputProps);
} : _props$renderInput,
_props$renderHandler = props.renderHandler,
renderHandler = _props$renderHandler === void 0 ? _noop["default"] : _props$renderHandler;
var _this$state2 = this.state,
value = _this$state2.value,
inputValue = _this$state2.inputValue,
focused = _this$state2.focused;
var classes = (0, _classnames["default"])((_classNames = {}, (0, _defineProperty2["default"])(_classNames, prefixCls, true), (0, _defineProperty2["default"])(_classNames, props.className, !!props.className), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-focused"), focused), _classNames));
var editable = !props.readOnly && !props.disabled;
var dataOrAriaAttributeProps = {};
for (var key in props) {
if (props.hasOwnProperty(key) && (key.substr(0, 5) === 'data-' || key.substr(0, 5) === 'aria-' || key === 'role')) {
dataOrAriaAttributeProps[key] = props[key];
}
} // focus state, show input value
// unfocus state, show valid value
var inputDisplayValue = this.composing ? inputValue : this.getInputDisplayValue();
var handler = this.renderSuffix();
return /*#__PURE__*/_react["default"].createElement("div", {
className: classes,
style: props.style,
title: props.title,
onMouseEnter: props.onMouseEnter,
onMouseLeave: props.onMouseLeave,
onMouseOver: props.onMouseOver,
onMouseOut: props.onMouseOut,
onCompositionStart: this.onComposition,
onCompositionEnd: this.onComposition
}, renderHandler(handler), /*#__PURE__*/_react["default"].createElement("div", {
className: "".concat(prefixCls, "-input-wrap")
}, renderInput((0, _objectSpread2["default"])({
role: 'spinbutton',
'aria-valuemin': props.min,
'aria-valuemax': props.max,
'aria-valuenow': value,
required: props.required,
type: props.type,
placeholder: props.placeholder,
onClick: props.onClick,
onMouseUp: this.onMouseUp,
className: "".concat(prefixCls, "-input"),
tabIndex: props.tabIndex,
autoComplete: autoComplete,
onFocus: this.onFocus,
onBlur: this.onBlur,
onKeyDown: editable ? this.onKeyDown : _noop["default"],
onKeyUp: editable ? this.onKeyUp : _noop["default"],
autoFocus: props.autoFocus,
maxLength: props.maxLength,
readOnly: props.readOnly,
disabled: props.disabled,
max: props.max,
min: props.min,
step: props.step,
name: props.name,
title: props.title,
id: props.id,
onChange: this.onChange,
ref: this.saveInput,
value: inputDisplayValue,
pattern: props.pattern,
suffix: handler,
label: props.label,
inputMode: props.inputMode
}, dataOrAriaAttributeProps))));
}
}]);
return InputNumber;
}(_react.Component);
exports["default"] = InputNumber;
(0, _defineProperty2["default"])(InputNumber, "defaultProps", {
focusOnUpDown: true,
useTouch: false,
prefixCls: 'rc-input-number',
max: Infinity,
min: -Infinity,
step: 1,
style: {},
onChange: _noop["default"],
onKeyDown: _noop["default"],
onPressEnter: _noop["default"],
onFocus: _noop["default"],
onBlur: _noop["default"],
parser: defaultParser,
required: false,
autoComplete: 'off'
});
//# sourceMappingURL=index.js.map