@aloudata/aloudata-design 2.5.1 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/DataPreviewTable/components/Body/Error.d.ts +1 -2
- package/dist/DataPreviewTable/components/Body/Error.js +5 -7
- package/dist/DataPreviewTable/components/Header/index.d.ts +1 -0
- package/dist/DataPreviewTable/components/Header/index.js +3 -4
- package/dist/DataPreviewTable/index.js +16 -15
- package/dist/DataPreviewTable/interface.d.ts +2 -2
- package/dist/DataPreviewTable/style/index.less +6 -0
- package/dist/Table/helper.d.ts +6 -4
- package/dist/Table/helper.js +20 -5
- package/dist/Table/hooks/useFixed.d.ts +10 -0
- package/dist/Table/hooks/useFixed.js +56 -0
- package/dist/Table/hooks/useScroll.d.ts +5 -1
- package/dist/Table/hooks/useScroll.js +79 -11
- package/dist/Table/index.js +56 -51
- package/dist/Table/style/index.less +70 -4
- package/dist/Table/types.d.ts +4 -0
- package/dist/ald.min.css +1 -1
- package/package.json +2 -1
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
interface IErrorProps {
|
|
3
|
-
contentWidth: number;
|
|
4
3
|
wrapWidth: number;
|
|
5
|
-
errorMsg?: string;
|
|
4
|
+
errorMsg?: string | React.ReactNode;
|
|
6
5
|
}
|
|
7
6
|
declare function ErrorBody(props: IErrorProps): React.JSX.Element;
|
|
8
7
|
declare const _default: React.MemoExoticComponent<typeof ErrorBody>;
|
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import React, { memo } from 'react';
|
|
2
2
|
import Result from "../../../Result";
|
|
3
3
|
function ErrorBody(props) {
|
|
4
|
-
var
|
|
5
|
-
wrapWidth = props.wrapWidth,
|
|
4
|
+
var wrapWidth = props.wrapWidth,
|
|
6
5
|
_props$errorMsg = props.errorMsg,
|
|
7
6
|
errorMsg = _props$errorMsg === void 0 ? '加载失败' : _props$errorMsg;
|
|
8
|
-
var width = contentWidth > wrapWidth ? wrapWidth : contentWidth;
|
|
9
7
|
return /*#__PURE__*/React.createElement("div", {
|
|
10
8
|
className: "ald-data-preview-table-error",
|
|
11
9
|
style: {
|
|
12
|
-
width:
|
|
10
|
+
width: wrapWidth
|
|
13
11
|
}
|
|
14
|
-
}, /*#__PURE__*/React.createElement(Result, {
|
|
12
|
+
}, typeof errorMsg === 'string' ? /*#__PURE__*/React.createElement(Result, {
|
|
15
13
|
status: "warning",
|
|
16
|
-
|
|
17
|
-
}));
|
|
14
|
+
subTitle: errorMsg
|
|
15
|
+
}) : errorMsg);
|
|
18
16
|
}
|
|
19
17
|
export default /*#__PURE__*/memo(ErrorBody);
|
|
@@ -8,7 +8,8 @@ import React from 'react';
|
|
|
8
8
|
import { HEADER_HEIGHT } from "../../constant";
|
|
9
9
|
import DragBar from "../DragBar";
|
|
10
10
|
export default function Header(props) {
|
|
11
|
-
var
|
|
11
|
+
var contentWidth = props.contentWidth,
|
|
12
|
+
columns = props.columns,
|
|
12
13
|
columnLayout = props.columnLayout,
|
|
13
14
|
setWidth = props.setWidth,
|
|
14
15
|
_props$columnRange = _slicedToArray(props.columnRange, 2),
|
|
@@ -17,9 +18,7 @@ export default function Header(props) {
|
|
|
17
18
|
return /*#__PURE__*/React.createElement("div", {
|
|
18
19
|
className: "ald-data-preview-header",
|
|
19
20
|
style: {
|
|
20
|
-
|
|
21
|
-
top: 0,
|
|
22
|
-
left: 0
|
|
21
|
+
width: contentWidth
|
|
23
22
|
}
|
|
24
23
|
}, columns.slice(startIndex, endIndex + 1).map(function (column) {
|
|
25
24
|
var _column$renderHeader;
|
|
@@ -31,8 +31,8 @@ var DataPreviewTable = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
|
31
31
|
onScroll = _useDirection.onScroll,
|
|
32
32
|
columnLayout = _useDirection.columnLayout,
|
|
33
33
|
setColumnsWidth = _useDirection.setColumnsWidth,
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
tableColumns = _useDirection.tableColumns,
|
|
35
|
+
contentWidth = _useDirection.contentWidth;
|
|
36
36
|
useImperativeHandle(ref, function () {
|
|
37
37
|
return {
|
|
38
38
|
scrollToColumnInView: function scrollToColumnInView(columnId) {
|
|
@@ -70,10 +70,10 @@ var DataPreviewTable = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
|
70
70
|
});
|
|
71
71
|
var skeletonRows = useMemo(function () {
|
|
72
72
|
var _wrapRef$current4;
|
|
73
|
-
|
|
73
|
+
if (!wrapRef.current) return 0;
|
|
74
|
+
var rows = Math.ceil(((((_wrapRef$current4 = wrapRef.current) === null || _wrapRef$current4 === void 0 ? void 0 : _wrapRef$current4.clientHeight) || 0) - HEADER_HEIGHT) / CELL_HEIGHT);
|
|
74
75
|
return rows - 1 < 0 ? 0 : rows - 1;
|
|
75
|
-
|
|
76
|
-
}, [wrapRef.current]);
|
|
76
|
+
}, []);
|
|
77
77
|
return /*#__PURE__*/React.createElement(ScrollArea, {
|
|
78
78
|
ref: wrapRef,
|
|
79
79
|
style: {
|
|
@@ -88,22 +88,24 @@ var DataPreviewTable = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
|
88
88
|
className: classNames(props.className, 'ald-data-preview-table', {
|
|
89
89
|
'ald-data-preview-table-show-skeleton': dataStatus === 'pending'
|
|
90
90
|
})
|
|
91
|
-
}, /*#__PURE__*/React.createElement(
|
|
92
|
-
className: "ald-data-preview-progress",
|
|
93
|
-
loading: !!loading,
|
|
94
|
-
ref: progressRef
|
|
95
|
-
}), /*#__PURE__*/React.createElement("div", {
|
|
91
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
96
92
|
style: {
|
|
97
93
|
height: dataStatus === 'pending' ? skeletonRows * CELL_HEIGHT + HEADER_HEIGHT : contentHeight,
|
|
98
|
-
width: contentWidth,
|
|
99
94
|
position: 'relative'
|
|
100
95
|
}
|
|
101
|
-
}, /*#__PURE__*/React.createElement(
|
|
96
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
97
|
+
className: "ald-data-preview-sticky"
|
|
98
|
+
}, /*#__PURE__*/React.createElement(Progress, {
|
|
99
|
+
className: "ald-data-preview-progress",
|
|
100
|
+
loading: !!loading,
|
|
101
|
+
ref: progressRef
|
|
102
|
+
}), /*#__PURE__*/React.createElement(Header, {
|
|
103
|
+
contentWidth: contentWidth,
|
|
102
104
|
columns: tableColumns,
|
|
103
105
|
columnLayout: columnLayout,
|
|
104
106
|
setWidth: setColumnsWidth,
|
|
105
107
|
columnRange: columnRange
|
|
106
|
-
}), dataStatus === 'pending' && tableColumns.length > 0 && /*#__PURE__*/React.createElement("div", {
|
|
108
|
+
})), dataStatus === 'pending' && tableColumns.length > 0 && /*#__PURE__*/React.createElement("div", {
|
|
107
109
|
className: "ald-data-preview-loading"
|
|
108
110
|
}, /*#__PURE__*/React.createElement(Spin, null)), dataStatus === 'success' && /*#__PURE__*/React.createElement(Body, {
|
|
109
111
|
columns: tableColumns,
|
|
@@ -113,8 +115,7 @@ var DataPreviewTable = /*#__PURE__*/forwardRef(function (props, ref) {
|
|
|
113
115
|
rowRange: rowRange
|
|
114
116
|
}), dataStatus === 'error' && /*#__PURE__*/React.createElement(Error, {
|
|
115
117
|
errorMsg: errorMsg,
|
|
116
|
-
wrapWidth: ((_wrapRef$current5 = wrapRef.current) === null || _wrapRef$current5 === void 0 ? void 0 : _wrapRef$current5.clientWidth) || 0
|
|
117
|
-
contentWidth: contentWidth
|
|
118
|
+
wrapWidth: ((_wrapRef$current5 = wrapRef.current) === null || _wrapRef$current5 === void 0 ? void 0 : _wrapRef$current5.clientWidth) || 0
|
|
118
119
|
})));
|
|
119
120
|
});
|
|
120
121
|
export default /*#__PURE__*/memo(DataPreviewTable);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CSSProperties } from 'react';
|
|
1
|
+
import React, { CSSProperties } from 'react';
|
|
2
2
|
export interface ITableProps {
|
|
3
3
|
columns?: PreviewColumn[];
|
|
4
4
|
datasource?: TValue[][];
|
|
@@ -6,7 +6,7 @@ export interface ITableProps {
|
|
|
6
6
|
height?: CSSProperties['height'];
|
|
7
7
|
dataStatus?: 'pending' | 'success' | 'error';
|
|
8
8
|
loading?: boolean;
|
|
9
|
-
errorMsg?: string;
|
|
9
|
+
errorMsg?: string | React.ReactNode;
|
|
10
10
|
className?: string;
|
|
11
11
|
}
|
|
12
12
|
export interface DataPreviewTableRef {
|
package/dist/Table/helper.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { ITableColumn, ITableProps } from './types';
|
|
3
|
-
export declare function getTableColumns<TDataItem extends object>({ columns,
|
|
3
|
+
export declare function getTableColumns<TDataItem extends object>({ columns, totalWidth, onError, }: {
|
|
4
4
|
columns: ITableColumn<TDataItem>[];
|
|
5
|
-
columnSizing: boolean;
|
|
6
5
|
totalWidth: number;
|
|
7
6
|
onError: ITableProps<TDataItem>['onError'];
|
|
8
7
|
}): {
|
|
@@ -12,13 +11,16 @@ export declare function getTableColumns<TDataItem extends object>({ columns, col
|
|
|
12
11
|
size: number;
|
|
13
12
|
meta: {
|
|
14
13
|
onError: ((errorInfo: import("./types").ITableCellErrorInfo) => void) | undefined;
|
|
14
|
+
columnWidths: number[];
|
|
15
|
+
index: number;
|
|
15
16
|
title: import("react").ReactNode;
|
|
16
17
|
dataIndex?: string | undefined;
|
|
17
18
|
width?: string | number | undefined;
|
|
18
19
|
render?: ((text: any, record: TDataItem, rowIndex: number) => import("react").ReactNode) | undefined;
|
|
19
20
|
ellipsis?: boolean | undefined;
|
|
20
21
|
noPadding?: boolean | undefined;
|
|
21
|
-
align?: "
|
|
22
|
+
align?: "left" | "center" | "right" | undefined;
|
|
23
|
+
fixed?: "left" | "right" | undefined;
|
|
22
24
|
};
|
|
23
25
|
}[];
|
|
24
26
|
/**
|
|
@@ -28,7 +30,7 @@ export declare function getTableColumns<TDataItem extends object>({ columns, col
|
|
|
28
30
|
* @param totalWidth
|
|
29
31
|
* @returns
|
|
30
32
|
*/
|
|
31
|
-
export declare function getColumnWidths(columns: ITableColumn<unknown>[],
|
|
33
|
+
export declare function getColumnWidths(columns: ITableColumn<unknown>[], totalWidth: number): number[];
|
|
32
34
|
export declare function getNumberFromPercentageStr(str: string): number;
|
|
33
35
|
/**
|
|
34
36
|
* 判断是否百分比字符串
|
package/dist/Table/helper.js
CHANGED
|
@@ -9,11 +9,24 @@ import Cell from "./components/Cell";
|
|
|
9
9
|
import Header from "./components/Header";
|
|
10
10
|
export function getTableColumns(_ref) {
|
|
11
11
|
var columns = _ref.columns,
|
|
12
|
-
columnSizing = _ref.columnSizing,
|
|
13
12
|
totalWidth = _ref.totalWidth,
|
|
14
13
|
onError = _ref.onError;
|
|
15
|
-
var columnWidths = getColumnWidths(columns,
|
|
16
|
-
|
|
14
|
+
var columnWidths = getColumnWidths(columns, totalWidth);
|
|
15
|
+
// 根据固定列的位置重排列顺序,将 left 的列放到最前面,right 的列放到最后面
|
|
16
|
+
var leftColumns = [];
|
|
17
|
+
var rightColumns = [];
|
|
18
|
+
var unFixedColumns = [];
|
|
19
|
+
columns.forEach(function (col) {
|
|
20
|
+
if (col.fixed === 'left') {
|
|
21
|
+
leftColumns.push(col);
|
|
22
|
+
} else if (col.fixed === 'right') {
|
|
23
|
+
rightColumns.push(col);
|
|
24
|
+
} else {
|
|
25
|
+
unFixedColumns.push(col);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
var sortedColumns = [].concat(leftColumns, unFixedColumns, rightColumns);
|
|
29
|
+
return _.map(sortedColumns, function (col, index) {
|
|
17
30
|
return {
|
|
18
31
|
// 这里还需要排除掉空字符串的情况,id 不能为 ''
|
|
19
32
|
accessorKey: col.dataIndex || "column-".concat(index),
|
|
@@ -21,7 +34,9 @@ export function getTableColumns(_ref) {
|
|
|
21
34
|
cell: Cell,
|
|
22
35
|
size: columnWidths[index],
|
|
23
36
|
meta: _objectSpread(_objectSpread({}, col), {}, {
|
|
24
|
-
onError: onError
|
|
37
|
+
onError: onError,
|
|
38
|
+
columnWidths: columnWidths,
|
|
39
|
+
index: index
|
|
25
40
|
})
|
|
26
41
|
};
|
|
27
42
|
});
|
|
@@ -34,7 +49,7 @@ export function getTableColumns(_ref) {
|
|
|
34
49
|
* @param totalWidth
|
|
35
50
|
* @returns
|
|
36
51
|
*/
|
|
37
|
-
export function getColumnWidths(columns,
|
|
52
|
+
export function getColumnWidths(columns, totalWidth) {
|
|
38
53
|
// 可改变列宽情况,react-table 可拖动列宽要求列宽值必须是数字,因为将百分比列宽转换为数字
|
|
39
54
|
var DEFAULT_WIDTH = 150;
|
|
40
55
|
// 计算出每个列的具体宽度值
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { ColumnDef } from '@tanstack/react-table';
|
|
3
|
+
export default function useFixed<TDataItem>({ tableColumns, y, scrollbarWidth, }: {
|
|
4
|
+
tableColumns: Array<ColumnDef<TDataItem, unknown>>;
|
|
5
|
+
y?: string | number;
|
|
6
|
+
scrollbarWidth: number;
|
|
7
|
+
}): (index: number, isHeader?: boolean) => {
|
|
8
|
+
className: string;
|
|
9
|
+
style: import("react").CSSProperties;
|
|
10
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
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); }
|
|
2
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
3
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
4
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
5
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
6
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
7
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
8
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
9
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
10
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
11
|
+
import classnames from 'classnames';
|
|
12
|
+
import { useCallback } from 'react';
|
|
13
|
+
import { prefixCls } from "../helper";
|
|
14
|
+
export default function useFixed(_ref) {
|
|
15
|
+
var tableColumns = _ref.tableColumns,
|
|
16
|
+
y = _ref.y,
|
|
17
|
+
scrollbarWidth = _ref.scrollbarWidth;
|
|
18
|
+
return useCallback(function (index) {
|
|
19
|
+
var _classnames;
|
|
20
|
+
var isHeader = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
21
|
+
var column = tableColumns[index];
|
|
22
|
+
var _ref2 = column.meta,
|
|
23
|
+
fixed = _ref2.fixed,
|
|
24
|
+
columnWidths = _ref2.columnWidths;
|
|
25
|
+
var style = {};
|
|
26
|
+
if (fixed === 'left') {
|
|
27
|
+
style.left = getFixedColumnOffset(columnWidths, index, true);
|
|
28
|
+
} else if (fixed === 'right') {
|
|
29
|
+
style.right = getFixedColumnOffset(columnWidths, index, false) + (isHeader && y !== undefined ? scrollbarWidth : 0);
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
className: classnames((_classnames = {}, _defineProperty(_classnames, prefixCls('fixed-left'), fixed === 'left'), _defineProperty(_classnames, prefixCls('fixed-right'), fixed === 'right'), _classnames)),
|
|
33
|
+
style: style
|
|
34
|
+
};
|
|
35
|
+
}, [tableColumns, y, scrollbarWidth]);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 获取固定列相对边缘的偏移量
|
|
40
|
+
* @param columns 所有列
|
|
41
|
+
* @param index 当前列的索引
|
|
42
|
+
*/
|
|
43
|
+
function getFixedColumnOffset(columnWidths, index, isLeft) {
|
|
44
|
+
var offset = 0;
|
|
45
|
+
var relativeIndex = index;
|
|
46
|
+
var list = _toConsumableArray(columnWidths);
|
|
47
|
+
if (!isLeft) {
|
|
48
|
+
list.reverse();
|
|
49
|
+
relativeIndex = list.length - index - 1;
|
|
50
|
+
}
|
|
51
|
+
for (var i = 0; i < relativeIndex; i++) {
|
|
52
|
+
var colWidth = columnWidths[i];
|
|
53
|
+
offset += colWidth;
|
|
54
|
+
}
|
|
55
|
+
return offset;
|
|
56
|
+
}
|
|
@@ -5,10 +5,14 @@ import { IScroll } from '../types';
|
|
|
5
5
|
* @param props
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
|
-
export default function
|
|
8
|
+
export default function useScroll(props: IProps): {
|
|
9
9
|
realWidth: number | undefined;
|
|
10
10
|
headerRef: import("react").RefObject<HTMLDivElement>;
|
|
11
|
+
bodyRef: import("react").RefObject<HTMLDivElement>;
|
|
11
12
|
onBodyScroll: (e: React.UIEvent<HTMLDivElement>) => void;
|
|
13
|
+
isPingLeft: boolean;
|
|
14
|
+
isPingRight: boolean;
|
|
15
|
+
scrollbarWidth: any;
|
|
12
16
|
};
|
|
13
17
|
interface IProps {
|
|
14
18
|
scroll: IScroll;
|
|
@@ -1,41 +1,109 @@
|
|
|
1
|
-
|
|
1
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
5
|
+
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
|
|
6
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
|
+
import { scrollbarWidth as getScrollbarWidth } from '@xobotyi/scrollbar-width';
|
|
8
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
9
|
import { getNumberFromPercentageStr } from "../helper";
|
|
10
|
+
var scrollbarWidth = function () {
|
|
11
|
+
var barWidth = getScrollbarWidth();
|
|
12
|
+
if (typeof barWidth === 'undefined') {
|
|
13
|
+
return 0;
|
|
14
|
+
}
|
|
15
|
+
return barWidth;
|
|
16
|
+
}();
|
|
17
|
+
|
|
3
18
|
/**
|
|
4
19
|
* 横向滚动条相关逻辑
|
|
5
20
|
* @param props
|
|
6
21
|
* @returns
|
|
7
22
|
*/
|
|
8
|
-
export default function
|
|
23
|
+
export default function useScroll(props) {
|
|
9
24
|
var scroll = props.scroll,
|
|
10
25
|
totalSize = props.totalSize;
|
|
11
26
|
var headerRef = useRef(null);
|
|
27
|
+
var bodyRef = useRef(null);
|
|
12
28
|
var animationFrameId = useRef(null);
|
|
13
|
-
var
|
|
29
|
+
var _useState = useState(false),
|
|
30
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
31
|
+
isPingLeft = _useState2[0],
|
|
32
|
+
setIsPingLeft = _useState2[1];
|
|
33
|
+
var _useState3 = useState(false),
|
|
34
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
35
|
+
isPingRight = _useState4[0],
|
|
36
|
+
setIsPingRight = _useState4[1];
|
|
37
|
+
var x = scroll.x,
|
|
38
|
+
y = scroll.y;
|
|
14
39
|
var realWidth = useMemo(function () {
|
|
15
40
|
if (!totalSize) {
|
|
16
41
|
return;
|
|
17
42
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
43
|
+
var width = getRealWidth(x, totalSize.width);
|
|
44
|
+
if (y !== undefined) {
|
|
45
|
+
return width - scrollbarWidth;
|
|
46
|
+
}
|
|
47
|
+
return width;
|
|
48
|
+
}, [x, y, totalSize]);
|
|
49
|
+
var changeIsPing = useCallback(function (elem) {
|
|
50
|
+
var scrollLeft = elem.scrollLeft;
|
|
51
|
+
if (scrollLeft <= 0) {
|
|
52
|
+
setIsPingLeft(false);
|
|
53
|
+
} else {
|
|
54
|
+
setIsPingLeft(true);
|
|
55
|
+
}
|
|
56
|
+
if (isScrolledToRight(elem, y !== undefined ? scrollbarWidth : 0)) {
|
|
57
|
+
setIsPingRight(false);
|
|
58
|
+
} else {
|
|
59
|
+
setIsPingRight(true);
|
|
60
|
+
}
|
|
61
|
+
}, [y]);
|
|
62
|
+
useEffect(function () {
|
|
63
|
+
if (realWidth && bodyRef.current) {
|
|
64
|
+
// 待表格渲染完成,宽度稳定后,初始化 ping 状态
|
|
65
|
+
changeIsPing(bodyRef.current);
|
|
66
|
+
}
|
|
67
|
+
}, [realWidth, changeIsPing]);
|
|
68
|
+
var requestAnimationFrame = useCallback(function (callback) {
|
|
69
|
+
// animationFrame 可以保证滚动时,header 和 body 不会出现不同步的情况
|
|
22
70
|
if (animationFrameId.current !== null) {
|
|
23
71
|
window.cancelAnimationFrame(animationFrameId.current);
|
|
24
72
|
}
|
|
25
|
-
|
|
26
|
-
|
|
73
|
+
animationFrameId.current = window.requestAnimationFrame(callback);
|
|
74
|
+
}, []);
|
|
75
|
+
var onBodyScroll = useCallback(function (e) {
|
|
76
|
+
var target = e.target;
|
|
77
|
+
var scrollLeft = target.scrollLeft;
|
|
78
|
+
requestAnimationFrame(function () {
|
|
27
79
|
var _headerRef$current;
|
|
28
80
|
(_headerRef$current = headerRef.current) === null || _headerRef$current === void 0 ? void 0 : _headerRef$current.scrollTo({
|
|
29
81
|
left: scrollLeft
|
|
30
82
|
});
|
|
83
|
+
changeIsPing(target);
|
|
31
84
|
});
|
|
32
|
-
}, []);
|
|
85
|
+
}, [requestAnimationFrame, changeIsPing]);
|
|
33
86
|
return {
|
|
34
87
|
realWidth: realWidth,
|
|
35
88
|
headerRef: headerRef,
|
|
36
|
-
|
|
89
|
+
bodyRef: bodyRef,
|
|
90
|
+
onBodyScroll: onBodyScroll,
|
|
91
|
+
isPingLeft: isPingLeft,
|
|
92
|
+
isPingRight: isPingRight,
|
|
93
|
+
scrollbarWidth: scrollbarWidth
|
|
37
94
|
};
|
|
38
95
|
}
|
|
96
|
+
function isScrolledToRight(element, offsetRight) {
|
|
97
|
+
// 计算最大可滚动值
|
|
98
|
+
var maxScrollLeft = element.scrollWidth - element.clientWidth - offsetRight;
|
|
99
|
+
|
|
100
|
+
// 允许一些误差,避免因为浮点数计算导致的问题
|
|
101
|
+
var DEVIATION = 2;
|
|
102
|
+
|
|
103
|
+
// 判断是否滚动到最右侧
|
|
104
|
+
return element.scrollLeft >= maxScrollLeft - DEVIATION;
|
|
105
|
+
}
|
|
106
|
+
|
|
39
107
|
/**
|
|
40
108
|
* 获取可用实际宽度
|
|
41
109
|
* @param scrollX 横向宽度值
|
package/dist/Table/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
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); }
|
|
2
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
3
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
2
4
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
3
5
|
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
4
6
|
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
@@ -12,15 +14,15 @@ import { flexRender, getCoreRowModel, useReactTable } from '@tanstack/react-tabl
|
|
|
12
14
|
import classnames from 'classnames';
|
|
13
15
|
import _ from 'lodash';
|
|
14
16
|
import ResizeObserver from 'rc-resize-observer';
|
|
15
|
-
import React, { forwardRef, useCallback, useImperativeHandle, useMemo,
|
|
16
|
-
import { Empty,
|
|
17
|
+
import React, { forwardRef, useCallback, useImperativeHandle, useMemo, useState } from 'react';
|
|
18
|
+
import { Empty, Spin } from '..';
|
|
17
19
|
import Pagination from "./components/Pagination";
|
|
18
20
|
import { getTableColumns, prefixCls } from "./helper";
|
|
19
|
-
import
|
|
20
|
-
|
|
21
|
+
import useFixed from "./hooks/useFixed";
|
|
22
|
+
import useScroll from "./hooks/useScroll";
|
|
21
23
|
var HEADER_HEIGHT = 41;
|
|
22
24
|
function Table(props, ref) {
|
|
23
|
-
var _classnames;
|
|
25
|
+
var _classnames, _classnames5;
|
|
24
26
|
var columns = props.columns,
|
|
25
27
|
data = props.data,
|
|
26
28
|
rowKey = props.rowKey,
|
|
@@ -41,21 +43,24 @@ function Table(props, ref) {
|
|
|
41
43
|
_useState2 = _slicedToArray(_useState, 2),
|
|
42
44
|
totalSize = _useState2[0],
|
|
43
45
|
setTotalSize = _useState2[1];
|
|
44
|
-
var tableBodyRef = useRef(null);
|
|
45
46
|
var y = scroll.y;
|
|
46
|
-
var
|
|
47
|
+
var _useScroll = useScroll({
|
|
47
48
|
totalSize: totalSize,
|
|
48
49
|
scroll: scroll
|
|
49
50
|
}),
|
|
50
|
-
realWidth =
|
|
51
|
-
onBodyScroll =
|
|
52
|
-
headerRef =
|
|
51
|
+
realWidth = _useScroll.realWidth,
|
|
52
|
+
onBodyScroll = _useScroll.onBodyScroll,
|
|
53
|
+
headerRef = _useScroll.headerRef,
|
|
54
|
+
bodyRef = _useScroll.bodyRef,
|
|
55
|
+
isPingLeft = _useScroll.isPingLeft,
|
|
56
|
+
isPingRight = _useScroll.isPingRight,
|
|
57
|
+
scrollbarWidth = _useScroll.scrollbarWidth;
|
|
53
58
|
var scrollToFirstRow = useCallback(function () {
|
|
54
|
-
if (!
|
|
59
|
+
if (!bodyRef.current) {
|
|
55
60
|
return;
|
|
56
61
|
}
|
|
57
|
-
|
|
58
|
-
}, []);
|
|
62
|
+
bodyRef.current.scrollTop = 0;
|
|
63
|
+
}, [bodyRef]);
|
|
59
64
|
useImperativeHandle(ref, function () {
|
|
60
65
|
return {
|
|
61
66
|
scrollToFirstRow: scrollToFirstRow
|
|
@@ -67,11 +72,15 @@ function Table(props, ref) {
|
|
|
67
72
|
}
|
|
68
73
|
return getTableColumns({
|
|
69
74
|
columns: columns,
|
|
70
|
-
columnSizing: !!columnSizing,
|
|
71
75
|
totalWidth: realWidth,
|
|
72
76
|
onError: onError
|
|
73
77
|
});
|
|
74
|
-
}, [columns, totalSize,
|
|
78
|
+
}, [columns, totalSize, realWidth, onError]);
|
|
79
|
+
var getColumnFixedInfo = useFixed({
|
|
80
|
+
tableColumns: tableColumns,
|
|
81
|
+
y: y,
|
|
82
|
+
scrollbarWidth: scrollbarWidth
|
|
83
|
+
});
|
|
75
84
|
var tableInstance = useReactTable({
|
|
76
85
|
columns: tableColumns,
|
|
77
86
|
data: data,
|
|
@@ -98,26 +107,37 @@ function Table(props, ref) {
|
|
|
98
107
|
headerStyle.top = _.get(sticky, 'offsetHeader', 0);
|
|
99
108
|
}
|
|
100
109
|
var headerContent = /*#__PURE__*/React.createElement("div", {
|
|
101
|
-
className: classnames(prefixCls('header'), (_classnames = {}, _defineProperty(_classnames, prefixCls('
|
|
110
|
+
className: classnames(prefixCls('header'), (_classnames = {}, _defineProperty(_classnames, prefixCls('overflow-x'), true), _defineProperty(_classnames, prefixCls('sticky'), isStickyWork), _classnames)),
|
|
102
111
|
ref: headerRef,
|
|
103
112
|
style: headerStyle
|
|
104
113
|
}, _.map(headerGroups, function (headerGroup) {
|
|
105
114
|
return /*#__PURE__*/React.createElement("div", {
|
|
106
115
|
className: prefixCls('tr'),
|
|
107
|
-
key: headerGroup.id
|
|
108
|
-
|
|
116
|
+
key: headerGroup.id,
|
|
117
|
+
style: {
|
|
118
|
+
width: realWidth
|
|
119
|
+
}
|
|
120
|
+
}, _.map(headerGroup.headers, function (header, index) {
|
|
109
121
|
var _classnames2;
|
|
122
|
+
var _getColumnFixedInfo = getColumnFixedInfo(index, true),
|
|
123
|
+
fixedClassName = _getColumnFixedInfo.className,
|
|
124
|
+
fixedStyle = _getColumnFixedInfo.style;
|
|
110
125
|
return /*#__PURE__*/React.createElement("div", {
|
|
111
|
-
className: prefixCls('th'),
|
|
112
|
-
style: {
|
|
126
|
+
className: classnames(prefixCls('th'), fixedClassName),
|
|
127
|
+
style: _objectSpread({
|
|
113
128
|
width: header.getSize()
|
|
114
|
-
},
|
|
129
|
+
}, fixedStyle),
|
|
115
130
|
key: header.id
|
|
116
131
|
}, flexRender(header.column.columnDef.header, header.getContext()), columnSizing && /*#__PURE__*/React.createElement("div", {
|
|
117
132
|
onMouseDown: header.getResizeHandler(),
|
|
118
133
|
onTouchStart: header.getResizeHandler(),
|
|
119
134
|
className: classnames(prefixCls('resizer'), (_classnames2 = {}, _defineProperty(_classnames2, prefixCls('self-resizing'), header.column.getIsResizing()), _defineProperty(_classnames2, prefixCls('resizing'), tableInstance.getState().columnSizingInfo.isResizingColumn), _classnames2))
|
|
120
135
|
}));
|
|
136
|
+
}), y !== undefined && /*#__PURE__*/React.createElement("div", {
|
|
137
|
+
className: prefixCls('header-scroll-placeholder'),
|
|
138
|
+
style: {
|
|
139
|
+
flexBasis: scrollbarWidth
|
|
140
|
+
}
|
|
121
141
|
}));
|
|
122
142
|
}));
|
|
123
143
|
var onClickRow = useCallback(function (rowData, rowIndex) {
|
|
@@ -138,7 +158,7 @@ function Table(props, ref) {
|
|
|
138
158
|
var bodyContent = /*#__PURE__*/React.createElement("div", {
|
|
139
159
|
className: prefixCls('body'),
|
|
140
160
|
style: {
|
|
141
|
-
width:
|
|
161
|
+
width: realWidth
|
|
142
162
|
}
|
|
143
163
|
}, rows.length === 0 && !loading ? emptyComponent || /*#__PURE__*/React.createElement("div", {
|
|
144
164
|
className: prefixCls('empty')
|
|
@@ -149,45 +169,30 @@ function Table(props, ref) {
|
|
|
149
169
|
key: row.id,
|
|
150
170
|
className: classnames(prefixCls('tr'), _defineProperty({}, prefixCls('row-hover'), typeof onRowClick === 'function'), rowClassName(row.original, rowIndex)),
|
|
151
171
|
onClick: onClickRow(row.original, rowIndex)
|
|
152
|
-
}, _.map(row.getVisibleCells(), function (cell) {
|
|
172
|
+
}, _.map(row.getVisibleCells(), function (cell, colIndex) {
|
|
173
|
+
var _getColumnFixedInfo2 = getColumnFixedInfo(colIndex),
|
|
174
|
+
fixedClassName = _getColumnFixedInfo2.className,
|
|
175
|
+
fixedStyle = _getColumnFixedInfo2.style;
|
|
153
176
|
return /*#__PURE__*/React.createElement("div", {
|
|
154
|
-
className: prefixCls('td'),
|
|
177
|
+
className: classnames(prefixCls('td'), fixedClassName),
|
|
155
178
|
key: cell.id,
|
|
156
|
-
style: {
|
|
179
|
+
style: _objectSpread({
|
|
157
180
|
width: cell.column.getSize()
|
|
158
|
-
}
|
|
181
|
+
}, fixedStyle)
|
|
159
182
|
}, flexRender(cell.column.columnDef.cell, cell.getContext()));
|
|
160
183
|
}));
|
|
161
184
|
}));
|
|
162
|
-
var tableContent =
|
|
185
|
+
var tableContent = /*#__PURE__*/React.createElement(Spin, {
|
|
163
186
|
spinning: !!loading
|
|
164
187
|
}, /*#__PURE__*/React.createElement("div", {
|
|
165
188
|
className: prefixCls('content')
|
|
166
|
-
}, /*#__PURE__*/React.createElement("div", {
|
|
167
|
-
className: prefixCls('header'),
|
|
189
|
+
}, headerContent, /*#__PURE__*/React.createElement("div", {
|
|
168
190
|
style: {
|
|
169
|
-
height: HEADER_HEIGHT
|
|
170
|
-
}
|
|
171
|
-
}, headerContent), /*#__PURE__*/React.createElement("div", {
|
|
172
|
-
style: {
|
|
173
|
-
height: totalSize.height - HEADER_HEIGHT
|
|
191
|
+
height: y && totalSize ? totalSize.height - HEADER_HEIGHT : 'auto'
|
|
174
192
|
},
|
|
175
|
-
className: prefixCls('body-scroll')
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
ref: tableBodyRef
|
|
179
|
-
}, bodyContent)))) : /*#__PURE__*/React.createElement("div", {
|
|
180
|
-
className: prefixCls('content')
|
|
181
|
-
}, !!loading && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
|
|
182
|
-
className: prefixCls('spin-mask')
|
|
183
|
-
}), /*#__PURE__*/React.createElement(Spin, null, /*#__PURE__*/React.createElement("div", {
|
|
184
|
-
className: prefixCls('spin'),
|
|
185
|
-
style: {
|
|
186
|
-
position: 'absolute'
|
|
187
|
-
}
|
|
188
|
-
}))), headerContent, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(ScrollArea, {
|
|
189
|
-
onViewportScroll: onBodyScroll,
|
|
190
|
-
ref: tableBodyRef
|
|
193
|
+
className: classnames(prefixCls('body-scroll'), _defineProperty({}, prefixCls('scroll-y'), y !== undefined)),
|
|
194
|
+
ref: bodyRef,
|
|
195
|
+
onScroll: onBodyScroll
|
|
191
196
|
}, bodyContent)));
|
|
192
197
|
var tableStyle = {};
|
|
193
198
|
if (y) {
|
|
@@ -206,7 +211,7 @@ function Table(props, ref) {
|
|
|
206
211
|
});
|
|
207
212
|
}
|
|
208
213
|
}, /*#__PURE__*/React.createElement("div", {
|
|
209
|
-
className: classnames(prefixCls('main'),
|
|
214
|
+
className: classnames(prefixCls('main'), (_classnames5 = {}, _defineProperty(_classnames5, prefixCls('overflow-hidden'), !!y), _defineProperty(_classnames5, prefixCls('ping-left'), isPingLeft), _defineProperty(_classnames5, prefixCls('ping-right'), isPingRight), _classnames5))
|
|
210
215
|
}, tableContent)), /*#__PURE__*/React.createElement(Pagination, {
|
|
211
216
|
pagination: pagination,
|
|
212
217
|
scroll: scroll,
|