|
|
@@ -1,18 +1,29 @@
|
|
|
import {Spin} from 'antd';
|
|
|
import 'antd/lib/table/style';
|
|
|
-import {ColumnsType} from 'antd/es/table';
|
|
|
+import {ColumnType, ColumnsType} from 'antd/es/table';
|
|
|
import {
|
|
|
createPageContext,
|
|
|
createSearchContext,
|
|
|
usePage,
|
|
|
useTableSearchState,
|
|
|
} from '@hooks';
|
|
|
-import {PAGE_SIZE_LIST, calcColumnsWidth} from '@utils';
|
|
|
-import {ReactElement, useMemo} from 'react';
|
|
|
+import {PAGE_SIZE_LIST} from '@utils';
|
|
|
+import {ReactElement} from 'react';
|
|
|
import {useTable} from './hooks';
|
|
|
import {flexRender} from '@tanstack/react-table';
|
|
|
import css from './index.module.css';
|
|
|
-import classNames from 'classnames';
|
|
|
+import HeaderTh from './header-th';
|
|
|
+import {
|
|
|
+ DndContext,
|
|
|
+ useSensor,
|
|
|
+ PointerSensor,
|
|
|
+ closestCenter,
|
|
|
+} from '@dnd-kit/core';
|
|
|
+import {
|
|
|
+ SortableContext,
|
|
|
+ horizontalListSortingStrategy,
|
|
|
+} from '@dnd-kit/sortable';
|
|
|
+import {restrictToHorizontalAxis} from '@dnd-kit/modifiers';
|
|
|
|
|
|
type Props<T> = {
|
|
|
columns: ColumnsType<T>;
|
|
|
@@ -37,72 +48,77 @@ function Table<T extends Record<string, any>>(props: Props<T>): ReactElement {
|
|
|
} = props;
|
|
|
const [{page, pageSize}, {onPageChange}] = usePage(pageContext);
|
|
|
const [isSearching] = useTableSearchState(searchContext);
|
|
|
- const colWidth = calcColumnsWidth(columns);
|
|
|
|
|
|
- const {getHeaderGroups, getRowModel, getCenterTotalSize} = useTable(
|
|
|
+ const [{getHeaderGroups, getRowModel, getCenterTotalSize}, onDrag] = useTable(
|
|
|
columns,
|
|
|
data,
|
|
|
);
|
|
|
+ const sensor = useSensor(PointerSensor);
|
|
|
|
|
|
return (
|
|
|
- <Spin spinning={isSearching}>
|
|
|
- <div className={css.tableWrapper}>
|
|
|
- <table className={css.table} style={{width: getCenterTotalSize()}}>
|
|
|
- <thead className={css.tableHead}>
|
|
|
- {getHeaderGroups().map(function ({id, headers}) {
|
|
|
- return (
|
|
|
- <tr key={id}>
|
|
|
- {headers.map(header => (
|
|
|
- <th key={header.id} style={{width: header.getSize()}}>
|
|
|
- {header.isPlaceholder
|
|
|
- ? null
|
|
|
- : flexRender(
|
|
|
- header.column.columnDef.header,
|
|
|
- header.getContext(),
|
|
|
- )}
|
|
|
-
|
|
|
- <div
|
|
|
- onMouseDown={header.getResizeHandler()}
|
|
|
- className={classNames(css.resizer, {
|
|
|
- [css.resizing]: header.column.getIsResizing(),
|
|
|
- })}
|
|
|
- />
|
|
|
- </th>
|
|
|
- ))}
|
|
|
- </tr>
|
|
|
- );
|
|
|
- })}
|
|
|
- </thead>
|
|
|
- <tbody className={css.tableBody}>
|
|
|
- {getRowModel().rows.map(function ({id, getVisibleCells}) {
|
|
|
- return (
|
|
|
- <tr key={id}>
|
|
|
- {getVisibleCells().map(function ({id, column, getContext}) {
|
|
|
- const align =
|
|
|
- (
|
|
|
- column.columnDef.meta as {
|
|
|
- align: 'left' | 'right' | 'center';
|
|
|
- }
|
|
|
- )?.align ?? 'left';
|
|
|
- return (
|
|
|
- <td
|
|
|
- key={id}
|
|
|
- style={{
|
|
|
- width: column.getSize(),
|
|
|
- textAlign: align,
|
|
|
- }}
|
|
|
- >
|
|
|
- {flexRender(column.columnDef.cell, getContext())}
|
|
|
- </td>
|
|
|
- );
|
|
|
- })}
|
|
|
- </tr>
|
|
|
- );
|
|
|
- })}
|
|
|
- </tbody>
|
|
|
- </table>
|
|
|
- </div>
|
|
|
- </Spin>
|
|
|
+ <DndContext
|
|
|
+ sensors={[sensor]}
|
|
|
+ modifiers={[restrictToHorizontalAxis]}
|
|
|
+ collisionDetection={closestCenter}
|
|
|
+ autoScroll
|
|
|
+ onDragEnd={onDrag}
|
|
|
+ >
|
|
|
+ <SortableContext
|
|
|
+ items={(columns as ColumnType<T>[]).map(val =>
|
|
|
+ val.dataIndex!.toString(),
|
|
|
+ )}
|
|
|
+ strategy={horizontalListSortingStrategy}
|
|
|
+ >
|
|
|
+ <Spin spinning={isSearching}>
|
|
|
+ <div className={css.tableWrapper} id='table_wrapper'>
|
|
|
+ <table className={css.table} style={{width: getCenterTotalSize()}}>
|
|
|
+ <thead className={css.tableHead}>
|
|
|
+ {getHeaderGroups().map(function ({id, headers}) {
|
|
|
+ return (
|
|
|
+ <tr key={id}>
|
|
|
+ {headers.map(header => (
|
|
|
+ <HeaderTh key={header.id} header={header} />
|
|
|
+ ))}
|
|
|
+ </tr>
|
|
|
+ );
|
|
|
+ })}
|
|
|
+ </thead>
|
|
|
+ <tbody className={css.tableBody}>
|
|
|
+ {getRowModel().rows.map(function ({id, getVisibleCells}) {
|
|
|
+ return (
|
|
|
+ <tr key={id}>
|
|
|
+ {getVisibleCells().map(function ({
|
|
|
+ id,
|
|
|
+ column,
|
|
|
+ getContext,
|
|
|
+ }) {
|
|
|
+ const align =
|
|
|
+ (
|
|
|
+ column.columnDef.meta as {
|
|
|
+ align: 'left' | 'right' | 'center';
|
|
|
+ }
|
|
|
+ )?.align ?? 'left';
|
|
|
+ return (
|
|
|
+ <td
|
|
|
+ key={id}
|
|
|
+ style={{
|
|
|
+ width: column.getSize(),
|
|
|
+ textAlign: align,
|
|
|
+ }}
|
|
|
+ >
|
|
|
+ {flexRender(column.columnDef.cell, getContext())}
|
|
|
+ </td>
|
|
|
+ );
|
|
|
+ })}
|
|
|
+ </tr>
|
|
|
+ );
|
|
|
+ })}
|
|
|
+ </tbody>
|
|
|
+ </table>
|
|
|
+ </div>
|
|
|
+ </Spin>
|
|
|
+ </SortableContext>
|
|
|
+ </DndContext>
|
|
|
);
|
|
|
}
|
|
|
|