UNPKG

acud

Version:

acg react 组件库

210 lines • 7.86 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _isNativeReflectConstruct from "@babel/runtime/helpers/esm/isNativeReflectConstruct"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } import * as React from 'react'; import classNames from 'classnames'; import raf from "rc-util/es/raf"; var MIN_SIZE = 20; function getPageY(e) { return 'touches' in e ? e.touches[0].pageY : e.pageY; } var ScrollBar = /*#__PURE__*/function (_React$Component) { function ScrollBar() { var _this; _classCallCheck(this, ScrollBar); _this = _callSuper(this, ScrollBar, arguments); _this.moveRaf = null; _this.scrollbarRef = /*#__PURE__*/React.createRef(); _this.thumbRef = /*#__PURE__*/React.createRef(); // eslint-disable-next-line no-undef _this.visibleTimeout = null; _this.state = { dragging: false, pageY: null, startTop: null, visible: false }; _this.delayHidden = function () { clearTimeout(_this.visibleTimeout); _this.setState({ visible: true }); _this.visibleTimeout = setTimeout(function () { _this.setState({ visible: false }); }, 2000); }; _this.onScrollbarTouchStart = function (e) { e.preventDefault(); }; _this.onContainerMouseDown = function (e) { e.stopPropagation(); e.preventDefault(); }; // ======================= Clean ======================= _this.patchEvents = function () { window.addEventListener('mousemove', _this.onMouseMove); window.addEventListener('mouseup', _this.onMouseUp); _this.thumbRef.current.addEventListener('touchmove', _this.onMouseMove); _this.thumbRef.current.addEventListener('touchend', _this.onMouseUp); }; _this.removeEvents = function () { window.removeEventListener('mousemove', _this.onMouseMove); window.removeEventListener('mouseup', _this.onMouseUp); _this.scrollbarRef.current.removeEventListener('touchstart', _this.onScrollbarTouchStart); _this.thumbRef.current.removeEventListener('touchstart', _this.onMouseDown); _this.thumbRef.current.removeEventListener('touchmove', _this.onMouseMove); _this.thumbRef.current.removeEventListener('touchend', _this.onMouseUp); raf.cancel(_this.moveRaf); }; // ======================= Thumb ======================= _this.onMouseDown = function (e) { var onStartMove = _this.props.onStartMove; _this.setState({ dragging: true, pageY: getPageY(e), startTop: _this.getTop() }); onStartMove(); _this.patchEvents(); e.stopPropagation(); e.preventDefault(); }; _this.onMouseMove = function (e) { var _this$state = _this.state, dragging = _this$state.dragging, pageY = _this$state.pageY, startTop = _this$state.startTop; var onScroll = _this.props.onScroll; raf.cancel(_this.moveRaf); if (dragging) { var offsetY = getPageY(e) - pageY; var newTop = startTop + offsetY; var enableScrollRange = _this.getEnableScrollRange(); var enableHeightRange = _this.getEnableHeightRange(); var ptg = enableHeightRange ? newTop / enableHeightRange : 0; var newScrollTop = Math.ceil(ptg * enableScrollRange); _this.moveRaf = raf(function () { onScroll(newScrollTop); }); } }; _this.onMouseUp = function () { var onStopMove = _this.props.onStopMove; _this.setState({ dragging: false }); onStopMove(); _this.removeEvents(); }; // ===================== Calculate ===================== _this.getSpinHeight = function () { var _this$props = _this.props, height = _this$props.height, count = _this$props.count; var baseHeight = height / count * 10; baseHeight = Math.max(baseHeight, MIN_SIZE); baseHeight = Math.min(baseHeight, height / 2); return Math.floor(baseHeight); }; _this.getEnableScrollRange = function () { var _this$props2 = _this.props, scrollHeight = _this$props2.scrollHeight, height = _this$props2.height; return scrollHeight - height || 0; }; _this.getEnableHeightRange = function () { var height = _this.props.height; var spinHeight = _this.getSpinHeight(); return height - spinHeight || 0; }; _this.getTop = function () { var scrollTop = _this.props.scrollTop; var enableScrollRange = _this.getEnableScrollRange(); var enableHeightRange = _this.getEnableHeightRange(); if (scrollTop === 0 || enableScrollRange === 0) { return 0; } var ptg = scrollTop / enableScrollRange; return ptg * enableHeightRange; }; // Not show scrollbar when height is large than scrollHeight _this.showScroll = function () { var _this$props3 = _this.props, height = _this$props3.height, scrollHeight = _this$props3.scrollHeight; return scrollHeight > height; }; return _this; } _inherits(ScrollBar, _React$Component); return _createClass(ScrollBar, [{ key: "componentDidMount", value: function componentDidMount() { this.scrollbarRef.current.addEventListener('touchstart', this.onScrollbarTouchStart); this.thumbRef.current.addEventListener('touchstart', this.onMouseDown); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (prevProps.scrollTop !== this.props.scrollTop) { this.delayHidden(); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.removeEvents(); clearTimeout(this.visibleTimeout); } // ====================== Render ======================= }, { key: "render", value: function render() { var _this$state2 = this.state, dragging = _this$state2.dragging, visible = _this$state2.visible; var prefixCls = this.props.prefixCls; var spinHeight = this.getSpinHeight(); var top = this.getTop(); var canScroll = this.showScroll(); var mergedVisible = canScroll && visible; return /*#__PURE__*/React.createElement("div", { ref: this.scrollbarRef, className: classNames("".concat(prefixCls, "-scrollbar"), _defineProperty({}, "".concat(prefixCls, "-scrollbar-show"), canScroll)), style: { width: 6, top: 0, bottom: 0, right: 0, position: 'absolute', display: mergedVisible ? null : 'none' }, onMouseDown: this.onContainerMouseDown, onMouseMove: this.delayHidden }, /*#__PURE__*/React.createElement("div", { ref: this.thumbRef, className: classNames("".concat(prefixCls, "-scrollbar-thumb"), _defineProperty({}, "".concat(prefixCls, "-scrollbar-thumb-moving"), dragging)), style: { width: '100%', height: spinHeight, top: top, left: 0, position: 'absolute', background: '#D4D6D9', borderRadius: 99, cursor: 'pointer', userSelect: 'none' }, onMouseDown: this.onMouseDown })); } }]); }(React.Component); export { ScrollBar as default };