choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
577 lines (511 loc) • 19.6 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) {
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
return true;
} catch (e) {
return false;
}
}
return function () {
var Super = _getPrototypeOf(Derived),
result;
if (isNativeReflectConstruct()) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import debounce from 'lodash/debounce';
import isEqual from 'lodash/isEqual';
import merge from 'lodash/merge';
import { create, Provider } from 'mini-store';
import classes from 'component-classes';
import warning from '../../_util/warning';
import addEventListener from '../../_util/addEventListener';
import ColumnManager from './ColumnManager';
import HeadTable from './HeadTable';
import BodyTable from './BodyTable';
import FootTable from './FootTable';
import ExpandableTable from './ExpandableTable';
var Table =
/*#__PURE__*/
function (_Component) {
_inherits(Table, _Component);
var _super = _createSuper(Table);
function Table(props) {
var _this;
_classCallCheck(this, Table);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "getRowKey", function (record, index) {
var rowKey = _this.props.rowKey;
var key = typeof rowKey === 'function' ? rowKey(record, index) : record[rowKey];
warning(key !== undefined, 'Each record in table should have a unique `key` prop,' + 'or set `rowKey` to an unique primary key.');
return key === undefined ? index : key;
});
_defineProperty(_assertThisInitialized(_this), "handleWindowResize", function () {
_this.syncFixedTableRowHeight();
_this.setScrollPositionClassName();
});
_defineProperty(_assertThisInitialized(_this), "syncFixedTableRowHeight", function () {
var tableRect = _this.tableNode.getBoundingClientRect(); // If tableNode's height less than 0, suppose it is hidden and don't recalculate rowHeight.
if (tableRect.height !== undefined && tableRect.height <= 0) {
return;
}
var prefixCls = _this.props.prefixCls;
var headRows = _this.headTable ? _this.headTable.querySelectorAll('thead') : _this.bodyTable.querySelectorAll('thead');
var footRows = _this.footTable ? _this.footTable.querySelectorAll('tfoot') : _this.bodyTable.querySelectorAll('tfoot');
var bodyRows = _this.bodyTable.querySelectorAll(".".concat(prefixCls, "-row")) || [];
var fixedColumnsHeadRowsHeight = [].map.call(headRows, function (row) {
return row.getBoundingClientRect().height || 'auto';
});
var fixedColumnsFootRowsHeight = [].map.call(footRows, function (row) {
return row.getBoundingClientRect().height || 'auto';
});
var fixedColumnsBodyRowsHeight = [].map.call(bodyRows, function (row) {
return row.getBoundingClientRect().height || 'auto';
});
var state = _this.store.getState();
if (isEqual(state.fixedColumnsHeadRowsHeight, fixedColumnsHeadRowsHeight) && isEqual(state.fixedColumnsFootRowsHeight, fixedColumnsFootRowsHeight) && isEqual(state.fixedColumnsBodyRowsHeight, fixedColumnsBodyRowsHeight)) {
return;
}
_this.store.setState({
fixedColumnsHeadRowsHeight: fixedColumnsHeadRowsHeight,
fixedColumnsFootRowsHeight: fixedColumnsFootRowsHeight,
fixedColumnsBodyRowsHeight: fixedColumnsBodyRowsHeight
});
});
_defineProperty(_assertThisInitialized(_this), "handleBodyScrollLeft", function (e) {
if (e.currentTarget !== e.target) {
return;
}
var target = e.target;
var _this$props$scroll = _this.props.scroll,
scroll = _this$props$scroll === void 0 ? {} : _this$props$scroll;
var _assertThisInitialize = _assertThisInitialized(_this),
headTable = _assertThisInitialize.headTable,
bodyTable = _assertThisInitialize.bodyTable,
footTable = _assertThisInitialize.footTable;
if (target.scrollLeft !== _this.lastScrollLeft && scroll.x) {
if (target === bodyTable) {
if (headTable) headTable.scrollLeft = target.scrollLeft;
if (footTable) footTable.scrollLeft = target.scrollLeft;
} else if (target === headTable) {
if (bodyTable) bodyTable.scrollLeft = target.scrollLeft;
if (footTable) footTable.scrollLeft = target.scrollLeft;
} else if (target === footTable) {
if (bodyTable) bodyTable.scrollLeft = target.scrollLeft;
if (headTable) headTable.scrollLeft = target.scrollLeft;
}
_this.setScrollPositionClassName();
} // Remember last scrollLeft for scroll direction detecting.
_this.lastScrollLeft = target.scrollLeft;
});
_defineProperty(_assertThisInitialized(_this), "handleBodyScrollTop", function (e) {
var target = e.target;
var _this$props$scroll2 = _this.props.scroll,
scroll = _this$props$scroll2 === void 0 ? {} : _this$props$scroll2;
var _assertThisInitialize2 = _assertThisInitialized(_this),
headTable = _assertThisInitialize2.headTable,
bodyTable = _assertThisInitialize2.bodyTable,
fixedColumnsBodyLeft = _assertThisInitialize2.fixedColumnsBodyLeft,
fixedColumnsBodyRight = _assertThisInitialize2.fixedColumnsBodyRight;
if (target.scrollTop !== _this.lastScrollTop && scroll.y && target !== headTable) {
var scrollTop = target.scrollTop;
if (fixedColumnsBodyLeft && target !== fixedColumnsBodyLeft) {
fixedColumnsBodyLeft.scrollTop = scrollTop;
}
if (fixedColumnsBodyRight && target !== fixedColumnsBodyRight) {
fixedColumnsBodyRight.scrollTop = scrollTop;
}
if (bodyTable && target !== bodyTable) {
bodyTable.scrollTop = scrollTop;
}
} // Remember last scrollTop for scroll direction detecting.
_this.lastScrollTop = target.scrollTop;
});
_defineProperty(_assertThisInitialized(_this), "handleBodyScroll", function (e) {
_this.handleBodyScrollLeft(e);
_this.handleBodyScrollTop(e);
});
_defineProperty(_assertThisInitialized(_this), "saveRef", function (name) {
return function (node) {
_this[name] = node;
};
});
['onRowClick', 'onRowDoubleClick', 'onRowContextMenu', 'onRowMouseEnter', 'onRowMouseLeave'].forEach(function (name) {
warning(props[name] === undefined, "".concat(name, " is deprecated, please use onRow instead."));
});
warning(props.getBodyWrapper === undefined, 'getBodyWrapper is deprecated, please use custom components instead.');
_this.columnManager = new ColumnManager(props.columns, props.children);
_this.store = create({
currentHoverKey: null,
fixedColumnsHeadRowsHeight: [],
fixedColumnsFootRowsHeight: [],
fixedColumnsBodyRowsHeight: []
});
_this.setScrollPosition('left');
_this.debouncedWindowResize = debounce(_this.handleWindowResize, 150);
return _this;
}
_createClass(Table, [{
key: "getChildContext",
value: function getChildContext() {
return {
table: {
props: this.props,
columnManager: this.columnManager,
saveRef: this.saveRef,
components: merge({
table: 'table',
header: {
wrapper: 'thead',
row: 'tr',
cell: 'th'
},
body: {
wrapper: 'tbody',
row: 'tr',
cell: 'td'
},
footer: {
wrapper: 'tfoot',
row: 'tr',
cell: 'td'
}
}, this.props.components)
}
};
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
if (this.columnManager.isAnyColumnsFixed()) {
this.handleWindowResize();
this.resizeEvent = addEventListener(window, 'resize', this.debouncedWindowResize);
}
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if (nextProps.columns && nextProps.columns !== this.props.columns) {
this.columnManager.reset(nextProps.columns);
} else if (nextProps.children !== this.props.children) {
this.columnManager.reset(null, nextProps.children);
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (this.columnManager.isAnyColumnsFixed()) {
this.handleWindowResize();
if (!this.resizeEvent) {
this.resizeEvent = addEventListener(window, 'resize', this.debouncedWindowResize);
}
} // when table changes to empty, reset scrollLeft
if (prevProps.data.length > 0 && this.props.data.length === 0 && this.hasScrollX()) {
this.resetScrollX();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.resizeEvent) {
this.resizeEvent.remove();
}
if (this.debouncedWindowResize) {
this.debouncedWindowResize.cancel();
}
}
}, {
key: "setScrollPosition",
value: function setScrollPosition(position) {
this.scrollPosition = position;
if (this.tableNode) {
var prefixCls = this.props.prefixCls;
if (position === 'both') {
classes(this.tableNode).remove(new RegExp("^".concat(prefixCls, "-scroll-position-.+$"))).add("".concat(prefixCls, "-scroll-position-left")).add("".concat(prefixCls, "-scroll-position-right"));
} else {
classes(this.tableNode).remove(new RegExp("^".concat(prefixCls, "-scroll-position-.+$"))).add("".concat(prefixCls, "-scroll-position-").concat(position));
}
}
}
}, {
key: "setScrollPositionClassName",
value: function setScrollPositionClassName() {
var node = this.bodyTable;
var scrollToLeft = node.scrollLeft === 0;
var scrollToRight = node.scrollLeft + 1 >= node.children[0].getBoundingClientRect().width - node.getBoundingClientRect().width;
if (scrollToLeft && scrollToRight) {
this.setScrollPosition('both');
} else if (scrollToLeft) {
this.setScrollPosition('left');
} else if (scrollToRight) {
this.setScrollPosition('right');
} else if (this.scrollPosition !== 'middle') {
this.setScrollPosition('middle');
}
}
}, {
key: "resetScrollX",
value: function resetScrollX() {
if (this.headTable) {
this.headTable.scrollLeft = 0;
}
if (this.bodyTable) {
this.bodyTable.scrollLeft = 0;
}
if (this.footTable) {
this.footTable.scrollLeft = 0;
}
}
}, {
key: "hasScrollX",
value: function hasScrollX() {
var _this$props$scroll3 = this.props.scroll,
scroll = _this$props$scroll3 === void 0 ? {} : _this$props$scroll3;
return 'x' in scroll;
}
}, {
key: "renderMainTable",
value: function renderMainTable() {
var _this$props = this.props,
scroll = _this$props.scroll,
prefixCls = _this$props.prefixCls;
var isAnyColumnsFixed = this.columnManager.isAnyColumnsFixed();
var scrollable = isAnyColumnsFixed || scroll.x || scroll.y;
var table = [this.renderTable({
columns: this.columnManager.groupedColumns(),
isAnyColumnsFixed: isAnyColumnsFixed
}), this.renderEmptyText(), this.renderFooter()];
return scrollable ? React.createElement("div", {
className: "".concat(prefixCls, "-scroll")
}, table) : table;
}
}, {
key: "renderLeftFixedTable",
value: function renderLeftFixedTable() {
var prefixCls = this.props.prefixCls;
return React.createElement("div", {
className: "".concat(prefixCls, "-fixed-left")
}, this.renderTable({
columns: this.columnManager.leftColumns(),
fixed: 'left'
}));
}
}, {
key: "renderRightFixedTable",
value: function renderRightFixedTable() {
var prefixCls = this.props.prefixCls;
return React.createElement("div", {
className: "".concat(prefixCls, "-fixed-right")
}, this.renderTable({
columns: this.columnManager.rightColumns(),
fixed: 'right'
}));
}
}, {
key: "renderTable",
value: function renderTable(options) {
var columns = options.columns,
fixed = options.fixed,
isAnyColumnsFixed = options.isAnyColumnsFixed;
var _this$props2 = this.props,
prefixCls = _this$props2.prefixCls,
_this$props2$scroll = _this$props2.scroll,
scroll = _this$props2$scroll === void 0 ? {} : _this$props2$scroll;
var tableClassName = scroll.x || fixed ? "".concat(prefixCls, "-fixed") : '';
var headTable = React.createElement(HeadTable, {
key: "head",
columns: columns,
fixed: fixed,
tableClassName: tableClassName,
handleBodyScrollLeft: this.handleBodyScrollLeft,
expander: this.expander
});
var bodyTable = React.createElement(BodyTable, {
key: "body",
columns: columns,
fixed: fixed,
tableClassName: tableClassName,
getRowKey: this.getRowKey,
handleBodyScroll: this.handleBodyScroll,
expander: this.expander,
isAnyColumnsFixed: isAnyColumnsFixed
});
var footTable = React.createElement(FootTable, {
key: "foot",
columns: columns,
fixed: fixed,
tableClassName: tableClassName,
handleBodyScrollLeft: this.handleBodyScrollLeft,
expander: this.expander
});
return [headTable, bodyTable, footTable];
}
}, {
key: "renderTitle",
value: function renderTitle() {
var _this$props3 = this.props,
title = _this$props3.title,
prefixCls = _this$props3.prefixCls;
return title ? React.createElement("div", {
className: "".concat(prefixCls, "-title"),
key: "title"
}, title(this.props.data)) : null;
}
}, {
key: "renderFooter",
value: function renderFooter() {
var _this$props4 = this.props,
footer = _this$props4.footer,
prefixCls = _this$props4.prefixCls;
return footer ? React.createElement("div", {
className: "".concat(prefixCls, "-footer"),
key: "footer"
}, footer(this.props.data)) : null;
}
}, {
key: "renderEmptyText",
value: function renderEmptyText() {
var _this$props5 = this.props,
emptyText = _this$props5.emptyText,
prefixCls = _this$props5.prefixCls,
data = _this$props5.data;
if (data.length) {
return null;
}
var emptyClassName = "".concat(prefixCls, "-placeholder");
return React.createElement("div", {
className: emptyClassName,
key: "emptyText"
}, typeof emptyText === 'function' ? emptyText() : emptyText);
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var props = this.props;
var prefixCls = props.prefixCls;
var className = props.prefixCls;
if (props.className) {
className += " ".concat(props.className);
}
if (props.useFixedHeader || props.scroll && props.scroll.y) {
className += " ".concat(prefixCls, "-fixed-header");
}
if (this.scrollPosition === 'both') {
className += " ".concat(prefixCls, "-scroll-position-left ").concat(prefixCls, "-scroll-position-right");
} else {
className += " ".concat(prefixCls, "-scroll-position-").concat(this.scrollPosition);
}
var hasLeftFixed = this.columnManager.isAnyColumnsLeftFixed();
var hasRightFixed = this.columnManager.isAnyColumnsRightFixed();
return React.createElement(Provider, {
store: this.store
}, React.createElement(ExpandableTable, _extends({}, props, {
columnManager: this.columnManager,
getRowKey: this.getRowKey
}), function (expander) {
_this2.expander = expander;
return React.createElement("div", {
ref: _this2.saveRef('tableNode'),
className: className,
style: props.style,
id: props.id
}, _this2.renderTitle(), React.createElement("div", {
className: "".concat(prefixCls, "-content")
}, _this2.renderMainTable(), hasLeftFixed && _this2.renderLeftFixedTable(), hasRightFixed && _this2.renderRightFixedTable()));
}));
}
}]);
return Table;
}(Component);
_defineProperty(Table, "propTypes", _objectSpread({
data: PropTypes.array,
useFixedHeader: PropTypes.bool,
columns: PropTypes.array,
prefixCls: PropTypes.string,
bodyStyle: PropTypes.object,
style: PropTypes.object,
rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
rowClassName: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
onRow: PropTypes.func,
onHeaderRow: PropTypes.func,
onRowClick: PropTypes.func,
onRowDoubleClick: PropTypes.func,
onRowContextMenu: PropTypes.func,
onRowMouseEnter: PropTypes.func,
onRowMouseLeave: PropTypes.func,
showHeader: PropTypes.bool,
title: PropTypes.func,
id: PropTypes.string,
footer: PropTypes.func,
emptyText: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
scroll: PropTypes.object,
rowRef: PropTypes.func,
getBodyWrapper: PropTypes.func,
children: PropTypes.node,
components: PropTypes.shape({
table: PropTypes.any,
header: PropTypes.shape({
wrapper: PropTypes.any,
row: PropTypes.any,
cell: PropTypes.any
}),
body: PropTypes.shape({
wrapper: PropTypes.any,
row: PropTypes.any,
cell: PropTypes.any
}),
footer: PropTypes.shape({
wrapper: PropTypes.any,
row: PropTypes.any,
cell: PropTypes.any
})
})
}, ExpandableTable.PropTypes));
_defineProperty(Table, "childContextTypes", {
table: PropTypes.any,
components: PropTypes.any
});
_defineProperty(Table, "defaultProps", {
data: [],
useFixedHeader: false,
rowKey: 'key',
rowClassName: function rowClassName() {
return '';
},
onRow: function onRow() {},
onHeaderRow: function onHeaderRow() {},
prefixCls: 'rc-table',
bodyStyle: {},
style: {},
showHeader: true,
scroll: {},
rowRef: function rowRef() {
return null;
},
emptyText: function emptyText() {
return 'No Data';
}
});
export { Table as default };
//# sourceMappingURL=Table.js.map