UNPKG

@txdfe/at

Version:

一个设计体系组件库

324 lines (323 loc) 15.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = virtual; var _react = _interopRequireDefault(require("react")); var _reactDom = require("react-dom"); var _propTypes = _interopRequireDefault(require("prop-types")); var _util = require("../util"); var _body = _interopRequireDefault(require("./virtual/body")); var _util2 = require("./util"); var _excluded = ["useVirtual", "components", "dataSource", "fixedHeader", "rowHeight", "scrollToRow", "onBodyScroll"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } var noop = function noop() {}; function virtual(BaseComponent) { var VirtualTable = /*#__PURE__*/function (_React$Component) { function VirtualTable() { var _this; _classCallCheck(this, VirtualTable); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, VirtualTable, [].concat(args)); _defineProperty(_this, "state", { rowHeight: _this.props.rowHeight, scrollToRow: _this.props.scrollToRow, height: _this.props.maxBodyHeight }); _defineProperty(_this, "onScroll", function () { // 避免横向滚动带来的性能问题 var scrollTop = _this.bodyNode.scrollTop; if (scrollTop === _this.lastScrollTop) { return; } var start = _this.computeScrollToRow(scrollTop); if (!('scrollToRow' in _this.props)) { _this.setState({ scrollToRow: start }); } _this.props.onBodyScroll(start); _this.lastScrollTop = scrollTop; }); _defineProperty(_this, "getBodyNode", function (node, lockType) { lockType = lockType ? lockType.charAt(0).toUpperCase() + lockType.substr(1) : ''; _this["body".concat(lockType, "Node")] = node; }); _defineProperty(_this, "getTableInstance", function (type, instance) { type = type ? type.charAt(0).toUpperCase() + type.substr(1) : ''; _this["table".concat(type, "Inc")] = instance; }); return _this; } _inherits(VirtualTable, _React$Component); return _createClass(VirtualTable, [{ key: "getChildContext", value: function getChildContext() { return { onVirtualScroll: this.onScroll, bodyHeight: this.computeBodyHeight(), innerTop: this.computeInnerTop(), getBodyNode: this.getBodyNode, getTableInstanceForVirtual: this.getTableInstance, rowSelection: this.rowSelection }; } }, { key: "UNSAFE_componentWillMount", value: function UNSAFE_componentWillMount() { var _this$props = this.props, useVirtual = _this$props.useVirtual, dataSource = _this$props.dataSource; this.hasVirtualData = useVirtual && dataSource && dataSource.length > 0; } }, { key: "componentDidMount", value: function componentDidMount() { if (this.hasVirtualData) { this.lastScrollTop = this.bodyNode.scrollTop; } this.adjustScrollTop(); this.adjustSize(); this.reComputeSize(); } }, { key: "UNSAFE_componentWillReceiveProps", value: function UNSAFE_componentWillReceiveProps(nextProps) { var useVirtual = nextProps.useVirtual, dataSource = nextProps.dataSource; this.hasVirtualData = useVirtual && dataSource && dataSource.length > 0; if ('maxBodyHeight' in nextProps) { if (this.state.height !== nextProps.maxBodyHeight) { this.setState({ height: nextProps.maxBodyHeight }); } } if ('scrollToRow' in nextProps) { this.setState({ scrollToRow: nextProps.scrollToRow }); } if (this.state.rowHeight && 'rowHeight' in nextProps) { var row = this.getRowNode(); var rowClientHeight = row && row.clientHeight; if (rowClientHeight && rowClientHeight !== this.state.rowHeight) { this.setState({ rowHeight: rowClientHeight }); } } } }, { key: "componentDidUpdate", value: function componentDidUpdate() { this.adjustScrollTop(); this.adjustSize(); this.reComputeSize(); } }, { key: "reComputeSize", value: function reComputeSize() { var rowHeight = this.state.rowHeight; if (typeof rowHeight === 'function' && this.hasVirtualData) { var row = this.getRowNode(); var rowClientHeight = row && row.clientHeight; if (rowClientHeight !== this.state.rowHeight) { this.setState({ rowHeight: rowClientHeight }); } } } }, { key: "computeBodyHeight", value: function computeBodyHeight() { var rowHeight = this.state.rowHeight; var dataSource = this.props.dataSource; if (typeof rowHeight === 'function') { return 0; } return dataSource.length * rowHeight; } }, { key: "computeInnerTop", value: function computeInnerTop() { var rowHeight = this.state.rowHeight; if (typeof rowHeight === 'function') { return 0; } return this.start * rowHeight; } }, { key: "getVisibleRange", value: function getVisibleRange(ExpectStart) { var _this$state = this.state, height = _this$state.height, rowHeight = _this$state.rowHeight; var len = this.props.dataSource.length; var end; var visibleCount = 0; var start = 0; if (typeof rowHeight === 'function') { // try get cell height; end = 1; } else { visibleCount = parseInt(height / rowHeight, 10); if (typeof ExpectStart === 'number') { start = ExpectStart < len ? ExpectStart : 0; } end = Math.min(+start + 1 + visibleCount + 10, len); } this.end = end; this.visibleCount = visibleCount; return { start: start, end: end }; } }, { key: "adjustScrollTop", value: function adjustScrollTop() { if (this.hasVirtualData) { this.bodyNode.scrollTop = this.lastScrollTop % this.state.rowHeight + this.state.rowHeight * this.state.scrollToRow; } } }, { key: "adjustSize", value: function adjustSize() { if (this.hasVirtualData) { var body = this.bodyNode; var virtualScrollNode = body.querySelector('div'); var clientHeight = body.clientHeight, clientWidth = body.clientWidth; var tableInc = this.tableInc; var tableNode = (0, _reactDom.findDOMNode)(tableInc); var prefix = this.props.prefix; var headerNode = tableNode.querySelector(".".concat(prefix, "table-header table")); var headerClientWidth = headerNode && headerNode.clientWidth; if (clientWidth < headerClientWidth) { _util.dom.setStyle(virtualScrollNode, 'min-width', headerClientWidth); var leftNode = this.bodyLeftNode; var rightNode = this.bodyRightNode; leftNode && _util.dom.setStyle(leftNode, 'max-height', clientHeight); rightNode && _util.dom.setStyle(rightNode, 'max-height', clientHeight); this.hasScrollbar = true; } else { _util.dom.setStyle(virtualScrollNode, 'min-width', 'auto'); this.hasScrollbar = false; } } } }, { key: "computeScrollToRow", value: function computeScrollToRow(offset) { var rowHeight = this.state.rowHeight; var start = parseInt(offset / rowHeight); this.start = start; return start; } }, { key: "getRowNode", value: function getRowNode() { try { // in case of finding an unmounted component due to cached data // need to clear refs of this.tableInc when dataSource Changed // use try catch for temporary return (0, _reactDom.findDOMNode)(this.tableInc.getRowRef(0)); } catch (error) { return null; } } }, { key: "render", value: function render() { /* eslint-disable no-unused-vars, prefer-const */ var _this$props2 = this.props, useVirtual = _this$props2.useVirtual, components = _this$props2.components, dataSource = _this$props2.dataSource, fixedHeader = _this$props2.fixedHeader, rowHeight = _this$props2.rowHeight, scrollToRow = _this$props2.scrollToRow, onBodyScroll = _this$props2.onBodyScroll, others = _objectWithoutProperties(_this$props2, _excluded); var entireDataSource = dataSource; this.rowSelection = this.props.rowSelection; if (this.hasVirtualData) { components = _objectSpread({}, components); var _this$getVisibleRange = this.getVisibleRange(this.state.scrollToRow), start = _this$getVisibleRange.start, end = _this$getVisibleRange.end; dataSource = dataSource.slice(start, end); if (!components.Body) { components.Body = _body["default"]; } fixedHeader = true; } return /*#__PURE__*/_react["default"].createElement(BaseComponent, _extends({}, others, { dataSource: dataSource, entireDataSource: entireDataSource, components: components, fixedHeader: fixedHeader })); } }]); }(_react["default"].Component); _defineProperty(VirtualTable, "VirtualBody", _body["default"]); _defineProperty(VirtualTable, "propTypes", _objectSpread({ /** * 是否开启虚拟滚动 */ useVirtual: _propTypes["default"].bool, /** * 设置行高 */ rowHeight: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].func]), maxBodyHeight: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string]), primaryKey: _propTypes["default"].string, dataSource: _propTypes["default"].array, /** * 在内容区域滚动的时候触发的函数 */ onBodyScroll: _propTypes["default"].func }, BaseComponent.propTypes)); _defineProperty(VirtualTable, "defaultProps", _objectSpread(_objectSpread({}, BaseComponent.defaultProps), {}, { primaryKey: 'id', rowHeight: noop, maxBodyHeight: 200, components: {}, prefix: 'next-', onBodyScroll: noop })); _defineProperty(VirtualTable, "childContextTypes", { onVirtualScroll: _propTypes["default"].func, bodyHeight: _propTypes["default"].number, innerTop: _propTypes["default"].number, getBodyNode: _propTypes["default"].func, getTableInstanceForVirtual: _propTypes["default"].func, rowSelection: _propTypes["default"].object }); (0, _util2.statics)(VirtualTable, BaseComponent); return VirtualTable; }