|
|
@@ -4,6 +4,9 @@ import {
|
|
|
getCoreRowModel,
|
|
|
Header,
|
|
|
RowSelectionState,
|
|
|
+ SortingState,
|
|
|
+ getSortedRowModel,
|
|
|
+ ColumnSort,
|
|
|
} from '@tanstack/react-table';
|
|
|
import {
|
|
|
CSSProperties,
|
|
|
@@ -20,12 +23,14 @@ import {createTableSettingContext, useContextSection} from '@hooks';
|
|
|
import TableCheck from './Check';
|
|
|
import {LDColumnsType} from '.';
|
|
|
import {TABLE_CELL_WIDTH} from '@utils';
|
|
|
+import {useLatest} from 'ahooks';
|
|
|
|
|
|
function parseColumn<T extends Record<string, unknown>>(
|
|
|
columns: LDColumnsType<T>[],
|
|
|
enableSelect?: boolean,
|
|
|
) {
|
|
|
const helper = createColumnHelper<Record<string, any>>();
|
|
|
+ let hasSort = false;
|
|
|
|
|
|
const parseColumns: LDColumnsType<T>[] = [
|
|
|
{
|
|
|
@@ -47,8 +52,11 @@ function parseColumn<T extends Record<string, unknown>>(
|
|
|
return prev + next.width;
|
|
|
}, 0);
|
|
|
|
|
|
- const tableColumns = parseColumns.map(function(val) {
|
|
|
- const {dataIndex, title, render, align, fixed, width} = val;
|
|
|
+ const columnList = parseColumns.map(function(val) {
|
|
|
+ const {dataIndex, title, render, align, fixed, width, sort} = val;
|
|
|
+
|
|
|
+ if (!hasSort && sort) hasSort = true;
|
|
|
+
|
|
|
let fixedStyle: CSSProperties = {};
|
|
|
|
|
|
if (fixed === 'left') {
|
|
|
@@ -66,6 +74,7 @@ function parseColumn<T extends Record<string, unknown>>(
|
|
|
fixed,
|
|
|
fixedStyle,
|
|
|
disabledSort: Boolean(fixed) || dataIndex === 'no',
|
|
|
+ sort,
|
|
|
};
|
|
|
|
|
|
if (render) {
|
|
|
@@ -90,7 +99,7 @@ function parseColumn<T extends Record<string, unknown>>(
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- enableSelect && tableColumns.unshift(
|
|
|
+ enableSelect && columnList.unshift(
|
|
|
helper.accessor('select', {
|
|
|
header({table}) {
|
|
|
return (
|
|
|
@@ -120,7 +129,7 @@ function parseColumn<T extends Record<string, unknown>>(
|
|
|
}),
|
|
|
);
|
|
|
|
|
|
- return tableColumns;
|
|
|
+ return {columnList, hasSort};
|
|
|
}
|
|
|
|
|
|
export function useTable<T extends Record<string, any>>(
|
|
|
@@ -131,6 +140,7 @@ export function useTable<T extends Record<string, any>>(
|
|
|
rowSelection?: RowSelectionState;
|
|
|
setRowSelection?: Dispatch<SetStateAction<RowSelectionState>>;
|
|
|
rawKey?: keyof T;
|
|
|
+ onSortingChange?: (state: ColumnSort | null) => void;
|
|
|
},
|
|
|
) {
|
|
|
const {
|
|
|
@@ -138,9 +148,10 @@ export function useTable<T extends Record<string, any>>(
|
|
|
rowSelection,
|
|
|
setRowSelection,
|
|
|
rawKey,
|
|
|
+ onSortingChange,
|
|
|
} = options;
|
|
|
|
|
|
- const columnList = useMemo(
|
|
|
+ const {columnList, hasSort} = useMemo(
|
|
|
function() {
|
|
|
return parseColumn(columns, Boolean(setRowSelection));
|
|
|
},
|
|
|
@@ -148,6 +159,7 @@ export function useTable<T extends Record<string, any>>(
|
|
|
);
|
|
|
|
|
|
const preloadData = useContextSection(settingContext, state => state[0]);
|
|
|
+
|
|
|
const [columnSizing, setColumnSizing] = useState(function() {
|
|
|
if (preloadData?.tableWidth)
|
|
|
return JSON.parse(preloadData.tableWidth) as Record<string, number>;
|
|
|
@@ -161,7 +173,6 @@ export function useTable<T extends Record<string, any>>(
|
|
|
|
|
|
return obj;
|
|
|
});
|
|
|
-
|
|
|
const [columnOrder, setColumnOrder] = useState(function() {
|
|
|
if (preloadData?.tableOrder)
|
|
|
return JSON.parse(preloadData?.tableOrder) as string[];
|
|
|
@@ -170,6 +181,11 @@ export function useTable<T extends Record<string, any>>(
|
|
|
|
|
|
return ['select', 'no', ...nextList];
|
|
|
});
|
|
|
+ const [sorting, setSorting] = useState<SortingState>([]);
|
|
|
+ const onSortingChangeFn = useLatest(onSortingChange);
|
|
|
+ useEffect(function() {
|
|
|
+ onSortingChangeFn.current?.(sorting.length ? sorting[0] : null);
|
|
|
+ }, [onSortingChangeFn, sorting]);
|
|
|
|
|
|
const table = useReactTable({
|
|
|
data,
|
|
|
@@ -178,13 +194,16 @@ export function useTable<T extends Record<string, any>>(
|
|
|
columnSizing,
|
|
|
columnOrder,
|
|
|
rowSelection,
|
|
|
+ sorting,
|
|
|
},
|
|
|
+ getSortedRowModel: onSortingChange ? void 0 : getSortedRowModel(),
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
onColumnSizingChange: setColumnSizing,
|
|
|
columnResizeMode: 'onChange',
|
|
|
onColumnOrderChange: setColumnOrder,
|
|
|
onRowSelectionChange: setRowSelection,
|
|
|
getRowId: row => row[rawKey as string | undefined ?? 'id'],
|
|
|
+ onSortingChange: setSorting,
|
|
|
});
|
|
|
|
|
|
type ActiveState = Header<Record<string, any>, unknown> | null;
|
|
|
@@ -222,7 +241,7 @@ export function useTable<T extends Record<string, any>>(
|
|
|
}
|
|
|
|
|
|
return [
|
|
|
- {...table, active},
|
|
|
+ {...table, active, hasSort},
|
|
|
{onDragStart, onDragEnd},
|
|
|
] as const;
|
|
|
}
|