@aliretail/react-materials-components
Version:
125 lines (105 loc) • 4.31 kB
JavaScript
import _NumberPicker from "@alifd/next/es/number-picker";
import _extends from "@babel/runtime/helpers/extends";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import * as React from 'react';
import cx from 'classnames';
import _ from 'lodash';
var RangeNumberPicker = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(RangeNumberPicker, _React$Component);
function RangeNumberPicker() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
_this.onFirstChange = function (value) {
var _this$props = _this.props,
oldValue = _this$props.value,
onChange = _this$props.onChange;
onChange([value, oldValue[1]]);
};
_this.onSecondChange = function (value) {
var _this$props2 = _this.props,
oldValue = _this$props2.value,
onChange = _this$props2.onChange;
onChange([oldValue[0], value]);
};
return _this;
}
var _proto = RangeNumberPicker.prototype;
_proto.getSecNumMin = function getSecNumMin() {
var _this$props3 = this.props,
notConstrain = _this$props3.notConstrain,
secondNumberPickerProps = _this$props3.secondNumberPickerProps,
value = _this$props3.value;
var firstNum = value === null || value === void 0 ? void 0 : value[0];
var secNumMin = secondNumberPickerProps === null || secondNumberPickerProps === void 0 ? void 0 : secondNumberPickerProps.min; // 如果是受到约束的话 最小值为设置的min和第一个值的最小值
if (!notConstrain && firstNum) {
if (typeof secNumMin !== 'number' || secNumMin < firstNum) {
return firstNum;
}
}
return secNumMin;
};
_proto.getFirNumMax = function getFirNumMax() {
var _this$props4 = this.props,
notConstrain = _this$props4.notConstrain,
secondNumberPickerProps = _this$props4.secondNumberPickerProps,
value = _this$props4.value;
var SecNum = value === null || value === void 0 ? void 0 : value[1];
var firNumMax = secondNumberPickerProps === null || secondNumberPickerProps === void 0 ? void 0 : secondNumberPickerProps.max; // 如果是受到约束的话 最小值为设置的min和第一个值的最小值
if (!notConstrain && SecNum) {
if (typeof firNumMax !== 'number' || SecNum < firNumMax) {
return SecNum;
}
}
return firNumMax;
};
_proto.render = function render() {
var _cx;
var _this$props5 = this.props,
value = _this$props5.value,
className = _this$props5.className,
style = _this$props5.style,
prefixText = _this$props5.prefixText,
suffixText = _this$props5.suffixText,
betweenText = _this$props5.betweenText,
firstNumberPickerProps = _this$props5.firstNumberPickerProps,
secondNumberPickerProps = _this$props5.secondNumberPickerProps;
var otherProps = _.omit(this.props, ['value', 'onChange', 'className', 'style', 'firstNumberPickerProps', 'secondNumberPickerProps', 'notConstrain']);
var classes = cx((_cx = {
'aliretail-range-number-picker': true
}, _cx[className] = !!className, _cx));
var firNum = value[0],
secNum = value[1];
return /*#__PURE__*/React.createElement("div", {
className: classes,
style: style
}, prefixText, /*#__PURE__*/React.createElement(_NumberPicker, _extends({
value: firNum,
onChange: this.onFirstChange
}, otherProps, firstNumberPickerProps, {
max: this.getFirNumMax()
})), /*#__PURE__*/React.createElement("span", {
className: "split-char"
}, betweenText), /*#__PURE__*/React.createElement(_NumberPicker, _extends({
value: secNum,
onChange: this.onSecondChange
}, otherProps, secondNumberPickerProps, {
min: this.getSecNumMin()
})), suffixText);
};
return RangeNumberPicker;
}(React.Component);
RangeNumberPicker.defaultProps = {
value: [],
className: '',
style: {},
prefixText: '',
suffixText: '',
betweenText: '-',
notConstrain: false,
firstNumberPickerProps: {},
secondNumberPickerProps: {}
};
export default RangeNumberPicker;