shineout
Version:
Shein 前端组件库
199 lines (168 loc) • 6.05 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { PureComponent } from 'react';
import { range } from '../utils/numbers';
import { datepickerClass } from './styles';
import paramUtils from './paramUtils';
import { getLocale } from '../locale';
var lineHeight = 30;
var grayStyle = {
1: {
color: '#888'
},
2: {
color: '#ccc'
},
3: {
color: '#eee'
}
};
var DefaultValue = {
total: 60
};
var TimeScroll =
/*#__PURE__*/
function (_PureComponent) {
_inheritsLoose(TimeScroll, _PureComponent);
function TimeScroll(props) {
var _this;
_this = _PureComponent.call(this, props) || this;
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "element", void 0);
_this.bindElement = _this.bindElement.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.handleMouseLeave = _this.handleMouseLeave.bind(_assertThisInitialized(_assertThisInitialized(_this)));
_this.handleScroll = _this.handleScroll.bind(_assertThisInitialized(_assertThisInitialized(_this)));
return _this;
}
var _proto = TimeScroll.prototype;
_proto.componentDidMount = function componentDidMount() {
this.updateScrollTop();
};
_proto.componentDidUpdate = function componentDidUpdate() {
this.updateScrollTop();
};
_proto.getOptions = function getOptions() {
var timeZone = this.props.timeZone;
return {
timeZone: timeZone,
weekStartsOn: getLocale('startOfWeek')
};
};
_proto.getValue = function getValue(v) {
var _this$props = this.props,
step = _this$props.step,
ampm = _this$props.ampm;
if (!step || ampm) return v;
return Math.ceil(v / step);
};
_proto.getItemStyle = function getItemStyle(num, isDisabled) {
if (isDisabled) return null;
if (this.props.ampm || typeof this.props.step === 'number' && this.props.step > 0) {
if (this.props.value % this.props.step) return null;
return grayStyle[Math.ceil(Math.abs(this.props.value - num) / this.props.step)];
}
return grayStyle[Math.abs(this.props.value - num)];
};
_proto.bindElement = function bindElement(el) {
this.element = el;
};
_proto.updateScrollTop = function updateScrollTop() {
var _this2 = this;
var value = this.props.value;
if (!this.element) return;
if (typeof this.props.step === 'number' && this.props.step > 0 && value % this.props.step) return;
if (this.element.scrollTop !== lineHeight * value) {
setTimeout(function () {
if (_this2.element) _this2.scrollToValue();
});
}
};
_proto.scrollToValue = function scrollToValue() {
var value = this.props.value;
this.element.scrollTop = lineHeight * this.getValue(value);
};
_proto.handleClick = function handleClick(value) {
this.props.onChange(value);
this.element.scrollTop = lineHeight * this.getValue(value);
};
_proto.handleMouseLeave = function handleMouseLeave() {
var value = Math.round(this.element.scrollTop / lineHeight);
this.element.scrollTop = lineHeight * value;
};
_proto.handleScroll = function handleScroll() {
var _this$props2 = this.props,
step = _this$props2.step,
ampm = _this$props2.ampm;
var value = Math.round(this.element.scrollTop / lineHeight);
if (value * step === this.props.value) return;
if (typeof step === 'number' && step > 0 && !ampm && value !== this.props.value) {
this.props.onChange(value * step);
return;
}
if (value !== this.props.value) this.props.onChange(value);
};
_proto.renderItem = function renderItem(num) {
var _this$props3 = this.props,
ampm = _this$props3.ampm,
_this$props3$total = _this$props3.total,
total = _this$props3$total === void 0 ? DefaultValue.total : _this$props3$total,
value = _this$props3.value,
step = _this$props3.step,
mode = _this$props3.mode,
min = _this$props3.min,
max = _this$props3.max,
ra = _this$props3.range,
current = _this$props3.current,
disabled = _this$props3.disabled,
disabledTime = _this$props3.disabledTime,
index = _this$props3.index,
rangeDate = _this$props3.rangeDate;
if (typeof step === 'number' && step <= 0) return null;
if (!ampm && typeof step === 'number' && num % step !== 0) return null;
var text = num;
if (ampm) text = ['am', 'pm'][num];else if (total === 12 && num === 0) text = '12';else if (num < 10) text = "0" + num;
var _paramUtils$judgeTime = paramUtils.judgeTimeByRange({
target: num,
value: current,
mode: mode,
min: min,
max: max,
range: ra,
disabled: disabled,
disabledTime: disabledTime,
options: this.getOptions(),
index: index,
rangeDate: rangeDate
}),
isDisabled = _paramUtils$judgeTime[0];
var className = datepickerClass(!isDisabled && value === num && 'time-active');
return React.createElement("span", {
key: num,
className: className,
style: this.getItemStyle(num, isDisabled),
onClick: this.handleClick.bind(this, num)
}, text);
};
_proto.render = function render() {
var _this3 = this;
var total = this.props.total;
return React.createElement("div", {
ref: this.bindElement,
className: datepickerClass('time-list'),
onMouseLeave: this.handleMouseLeave,
onScroll: this.handleScroll
}, React.createElement("div", {
className: datepickerClass('pad')
}), range(total, 0).map(function (v) {
return _this3.renderItem(v);
}), React.createElement("div", {
className: datepickerClass('pad')
}));
};
return TimeScroll;
}(PureComponent);
_defineProperty(TimeScroll, "defaultProps", DefaultValue);
TimeScroll.defaultProps = {
total: 60
};
export default TimeScroll;