@txdfe/at
Version:
一个设计体系组件库
583 lines (495 loc) • 20 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from 'react';
import PropTypes from 'prop-types';
import cls from 'classnames';
import moment from 'moment';
import TimePickerPanel from './panel';
import Input from '../input';
import Overlay from '../overlay';
import nextLocale from '../locale/zh-cn';
import { formatDateValue } from './utils';
var Popup = Overlay.Popup;
var timePickerLocale = nextLocale.TimePicker;
var noop = function noop() {};
var getFormatValues = function getFormatValues(values, format) {
if (!Array.isArray(values)) {
return [null, null];
}
return [formatDateValue(values[0], format), formatDateValue(values[1], format)];
};
var RangePicker = /*#__PURE__*/function (_React$Component) {
_inherits(RangePicker, _React$Component);
var _super = _createSuper(RangePicker);
function RangePicker(props) {
var _this;
_classCallCheck(this, RangePicker);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "onMouseOver", function () {
_this.setState({
isHover: true
});
});
_defineProperty(_assertThisInitialized(_this), "onMouseOut", function () {
_this.setState({
isHover: false
});
});
_defineProperty(_assertThisInitialized(_this), "onVisibleChange", function (visible, type) {
if (!('visible' in _this.props)) {
_this.setState({
visible: visible
});
}
_this.props.onVisibleChange(visible, type);
});
_defineProperty(_assertThisInitialized(_this), "onSelectStartTime", function (value) {
var _this$state = _this.state,
startValue = _this$state.startValue,
endValue = _this$state.endValue;
if (!('value' in _this.props)) {
_this.setState({
startValue: value,
startInputing: false
});
}
if (!startValue || startValue && value.valueOf() !== startValue.valueOf()) {
_this.onValueChange([value, endValue]);
}
});
_defineProperty(_assertThisInitialized(_this), "onSelectEndTime", function (value) {
var _this$state2 = _this.state,
startValue = _this$state2.startValue,
endValue = _this$state2.endValue;
if (!('value' in _this.props)) {
_this.setState({
endValue: value,
endInputing: false
});
}
if (!endValue || endValue && value.valueOf() !== endValue.valueOf()) {
_this.onValueChange([startValue, value]);
}
});
_defineProperty(_assertThisInitialized(_this), "onValueChange", function (v) {
var format = _this.props.format;
var ret = _this.inputAsString ? [v[0].format(format), v[1].format(format)] : v;
_this.props.onChange(ret);
});
_defineProperty(_assertThisInitialized(_this), "onClearValue", function () {
_this.setState({
startValue: '',
endValue: '',
startInputStr: '',
endInputStr: ''
});
var ret = _this.inputAsString ? ['', ''] : [null, null];
_this.props.onChange(ret);
});
_defineProperty(_assertThisInitialized(_this), "onTriggerChange", function (v, e, eventType) {
if (!('value' in _this.props)) {
if (eventType === 'clear') {
e.stopPropagation();
_this.onClearValue();
}
} else if (eventType === 'clear') {
// 受控状态下用户点击 clear
e.stopPropagation();
var ret = _this.inputAsString ? ['', ''] : [null, null];
_this.props.onChange(ret);
}
});
_defineProperty(_assertThisInitialized(_this), "onStartInputChange", function (value) {
if (!('value' in _this.props)) {
_this.setState({
startInputing: true,
startInputStr: value
});
}
});
_defineProperty(_assertThisInitialized(_this), "onEndInputChange", function (value) {
if (!('value' in _this.props)) {
_this.setState({
endInputing: true,
endInputStr: value
});
}
});
_defineProperty(_assertThisInitialized(_this), "onStartInputBlur", function () {
var _this$state3 = _this.state,
startValue = _this$state3.startValue,
startInputStr = _this$state3.startInputStr,
endValue = _this$state3.endValue;
if (!startValue && startInputStr) {
var format = _this.props.format;
var parsed = moment(startInputStr, format, true);
if (parsed.isValid()) {
_this.setState({
startValue: parsed,
startInputStr: ''
});
_this.onValueChange([parsed, endValue]);
}
_this.setState({
startInputing: false
});
}
});
_defineProperty(_assertThisInitialized(_this), "onEndInputBlur", function () {
var _this$state4 = _this.state,
startValue = _this$state4.startValue,
endInputStr = _this$state4.endInputStr,
endValue = _this$state4.endValue;
if (!endValue && endInputStr) {
var format = _this.props.format;
var parsed = moment(endInputStr, format, true);
if (parsed.isValid()) {
_this.setState({
endValue: parsed,
endInputStr: ''
});
_this.onValueChange([startValue, parsed]);
}
_this.setState({
endInputing: false
});
}
});
_defineProperty(_assertThisInitialized(_this), "renderTrigger", function () {
var _this$props = _this.props,
prefix = _this$props.prefix,
size = _this$props.size,
locale = _this$props.locale,
disabled = _this$props.disabled,
format = _this$props.format;
var _this$state5 = _this.state,
startValue = _this$state5.startValue,
endValue = _this$state5.endValue,
startInputStr = _this$state5.startInputStr,
endInputStr = _this$state5.endInputStr,
startInputing = _this$state5.startInputing,
endInputing = _this$state5.endInputing;
var startInputValue = startInputing ? startInputStr : startValue && startValue.format(format) || '';
var endInputValue = endInputing ? endInputStr : endValue && endValue.format(format) || '';
return /*#__PURE__*/React.createElement("div", {
tabIndex: disabled ? null : 0
}, /*#__PURE__*/React.createElement(Input, {
size: size,
readOnly: true,
disabled: disabled,
placeholder: locale.startPlaceholder,
onChange: _this.onTrigger,
value: startInputValue,
hasBorder: false,
className: "".concat(prefix, "time-range-picker-trigger-start-input")
}), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefix, "time-range-picker-trigger-separator")
}, "~"), /*#__PURE__*/React.createElement(Input, {
size: size,
readOnly: true,
disabled: disabled,
placeholder: locale.endPlaceholder,
value: endInputValue,
hasBorder: false,
hint: "clock",
hasClear: true,
onChange: _this.onTriggerChange,
className: "".concat(prefix, "time-range-picker-trigger-start-input")
}));
});
_defineProperty(_assertThisInitialized(_this), "renderTimePanel", function () {
var _this$props2 = _this.props,
prefix = _this$props2.prefix,
locale = _this$props2.locale,
disabled = _this$props2.disabled,
disabledHours = _this$props2.disabledHours,
disabledMinutes = _this$props2.disabledMinutes,
disabledSeconds = _this$props2.disabledSeconds,
format = _this$props2.format,
hourStep = _this$props2.hourStep,
minuteStep = _this$props2.minuteStep,
secondStep = _this$props2.secondStep;
var _this$state6 = _this.state,
startValue = _this$state6.startValue,
endValue = _this$state6.endValue,
startInputStr = _this$state6.startInputStr,
endInputStr = _this$state6.endInputStr,
startInputing = _this$state6.startInputing,
endInputing = _this$state6.endInputing;
var startInputValue = startInputing ? startInputStr : startValue && startValue.format(format) || '';
var endInputValue = endInputing ? endInputStr : endValue && endValue.format(format) || '';
var showSecond = format.indexOf('s') > -1;
var sharedTimePickerProps = {
prefix: prefix,
locale: locale,
disabled: disabled,
showSecond: showSecond,
hourStep: hourStep,
minuteStep: minuteStep,
secondStep: secondStep
};
return /*#__PURE__*/React.createElement("div", {
className: "".concat(prefix, "time-range-picker-panel")
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefix, "time-range-picker-panel-header")
}, /*#__PURE__*/React.createElement(Input, {
size: "xs",
onChange: _this.onStartInputChange,
onBlur: _this.onStartInputBlur,
onPressEnter: _this.onStartInputBlur,
disabled: disabled,
value: startInputValue,
placeholder: format,
className: "".concat(prefix, "time-range-picker-panel-start-input")
}), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefix, "time-range-picker-trigger-separator")
}, "~"), /*#__PURE__*/React.createElement(Input, {
size: "xs",
onChange: _this.onEndInputChange,
onBlur: _this.onEndInputBlur,
onPressEnter: _this.onEndInputBlur,
disabled: disabled,
value: endInputValue,
placeholder: format,
className: "".concat(prefix, "time-range-picker-panel-end-input")
})), /*#__PURE__*/React.createElement(TimePickerPanel, _extends({}, sharedTimePickerProps, {
disabledHours: disabledHours[0],
disabledMinutes: disabledMinutes[0],
disabledSeconds: disabledSeconds[0],
className: "".concat(prefix, "time-range-picker-start-panel"),
value: startValue,
onSelect: _this.onSelectStartTime
})), /*#__PURE__*/React.createElement(TimePickerPanel, _extends({}, sharedTimePickerProps, {
disabledHours: disabledHours[1],
disabledMinutes: disabledMinutes[1],
disabledSeconds: disabledSeconds[1],
className: "".concat(prefix, "time-range-picker-end-panel"),
value: endValue,
onSelect: _this.onSelectEndTime
})));
});
var val = props.value || props.defaultValue;
var _value = getFormatValues(val, props.format);
_this.inputAsString = val && typeof val[0] === 'string' && typeof val[1] === 'string';
_this.state = {
startInputStr: '',
endInputStr: '',
startInputing: false,
endInputing: false,
visible: props.visible || props.defaultVisible,
startValue: _value[0] || '',
endValue: _value[1] || '',
isHover: false
};
return _this;
}
_createClass(RangePicker, [{
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
if (this.props.visible !== nextProps.visible) {
this.setState({
visible: nextProps.visible
});
}
if (this.props.value !== nextProps.value) {
var value = getFormatValues(nextProps.value, nextProps.format);
this.setState({
startValue: value[0],
endValue: value[1]
});
}
}
}, {
key: "render",
value: function render() {
var _cls;
var _this$state7 = this.state,
visible = _this$state7.visible,
isHover = _this$state7.isHover;
var _this$props3 = this.props,
prefix = _this$props3.prefix,
followTrigger = _this$props3.followTrigger,
popupProps = _this$props3.popupProps,
popupContainer = _this$props3.popupContainer,
popupAlign = _this$props3.popupAlign,
popupTriggerType = _this$props3.popupTriggerType,
popupStyle = _this$props3.popupStyle,
popupClassName = _this$props3.popupClassName,
disabled = _this$props3.disabled,
style = _this$props3.style,
className = _this$props3.className;
return /*#__PURE__*/React.createElement("div", {
className: cls((_cls = {}, _defineProperty(_cls, "".concat(prefix, "time-range-picker"), true), _defineProperty(_cls, "".concat(prefix, "time-range-picker-active"), visible && !disabled), _defineProperty(_cls, "".concat(prefix, "time-range-picker-hover"), isHover && !disabled), _defineProperty(_cls, "".concat(prefix, "time-range-picker-disabled"), disabled), _defineProperty(_cls, "className", className), _cls)),
style: style,
onMouseOver: this.onMouseOver,
onMouseOut: this.onMouseOut
}, /*#__PURE__*/React.createElement(Popup, _extends({}, popupProps, {
offset: [0, 4],
closable: false,
trigger: this.renderTrigger(),
triggerType: popupTriggerType,
followTrigger: followTrigger,
container: popupContainer,
disabled: disabled,
visible: visible,
onVisibleChange: this.onVisibleChange,
align: popupAlign,
style: popupStyle,
className: popupClassName
}), this.renderTimePanel()));
}
}]);
return RangePicker;
}(React.Component);
_defineProperty(RangePicker, "propTypes", {
/**
* 设置trigger的className
*/
className: PropTypes.string,
/**
* 设置trigger的style
*/
style: PropTypes.object,
/**
* 设置trigger的style
*/
size: PropTypes.oneOf(['small', 'medium']),
/**
* 设置组件className前缀
*/
prefix: PropTypes.string,
/**
* 语言包
*/
locale: PropTypes.object,
/**
* 弹层是否默认可见,初始化时有效
*/
defaultVisible: PropTypes.bool,
/**
* 弹层是否可见,受控
*/
visible: PropTypes.bool,
/**
* 弹层显隐回调
*/
onVisibleChange: PropTypes.func,
/**
* 默认值,moment或字符串数组,非受控
*/
defaultValue: PropTypes.array,
/**
* moment或字符串数组,受控
*/
value: PropTypes.array,
/**
* 整体值发生变化时触发的回调方法
*/
onChange: PropTypes.func,
/**
* 时间格式
*/
format: PropTypes.string,
/**
* 小时选择步长
*/
hourStep: PropTypes.number,
/**
* 分钟选择步长
*/
minuteStep: PropTypes.number,
/**
* 秒选择步长
*/
secondStep: PropTypes.number,
/**
* 禁用的小时,数组
* @return {Boolean} 是否禁用
*/
disabledHours: PropTypes.arrayOf(PropTypes.func),
/**
* 禁用的分钟,数组
* @return {Boolean} 是否禁用
*/
disabledMinutes: PropTypes.arrayOf(PropTypes.func),
/**
* 禁用的秒,数组
* @return {Boolean} 是否禁用
*/
disabledSeconds: PropTypes.arrayOf(PropTypes.func),
/**
* 弹层容器
* @param {Object} target 目标节点
* @return {ReactNode} 容器节点
*/
popupContainer: PropTypes.func,
/**
* 弹层对齐方式, 详情见Overlay 文档
*/
popupAlign: PropTypes.string,
/**
* 弹层触发方式
*/
popupTriggerType: PropTypes.oneOf(['click', 'hover']),
/**
* 弹层自定义样式
*/
popupStyle: PropTypes.object,
/**
* 弹层自定义样式类
*/
popupClassName: PropTypes.string,
/**
* 弹层属性
*/
popupProps: PropTypes.object,
/**
* 是否跟随滚动
*/
followTrigger: PropTypes.bool,
/**
* 是否禁用
*/
disabled: PropTypes.bool
});
_defineProperty(RangePicker, "defaultProps", {
prefix: 'next-',
size: 'medium',
onVisibleChange: noop,
onChange: noop,
format: 'HH:mm:ss',
locale: timePickerLocale,
hourStep: 1,
minuteStep: 1,
secondStep: 1,
disabled: false,
disabledHours: [function () {
return false;
}, function () {
return false;
}],
disabledMinutes: [function () {
return false;
}, function () {
return false;
}],
disabledSeconds: [function () {
return false;
}, function () {
return false;
}],
popupAlign: 'tl bl',
popupTriggerType: 'click'
});
export default RangePicker;