|
@@ -4,47 +4,51 @@ import {
|
|
|
getCoreRowModel,
|
|
|
} from '@tanstack/react-table';
|
|
|
import {ColumnType} from 'antd/es/table';
|
|
|
-import {useMemo, useState} from 'react';
|
|
|
+import {useMemo, useRef, useState} from 'react';
|
|
|
|
|
|
-export function useTable<T extends Record<string, any>>(
|
|
|
- columns: ColumnType<T>[],
|
|
|
- data: T[],
|
|
|
-) {
|
|
|
- const columnList = useMemo(function () {
|
|
|
- const hepler = createColumnHelper<Record<string, any>>();
|
|
|
+function parseColumn<T>(columns: ColumnType<T>[]) {
|
|
|
+ const hepler = createColumnHelper<Record<string, any>>();
|
|
|
|
|
|
- const tableColumns = columns.map(function ({dataIndex, title, render}) {
|
|
|
- if (render) {
|
|
|
- return hepler.display({
|
|
|
- id: dataIndex!.toString(),
|
|
|
- header: title?.toString(),
|
|
|
- cell(props) {
|
|
|
- return render(
|
|
|
- props.getValue(),
|
|
|
- props.row.original as T,
|
|
|
- props.row.index,
|
|
|
- );
|
|
|
- },
|
|
|
- });
|
|
|
- }
|
|
|
- return hepler.accessor(dataIndex!.toString(), {
|
|
|
+ const tableColumns = columns.map(function (val) {
|
|
|
+ console.log(val);
|
|
|
+ const {dataIndex, title, render} = val;
|
|
|
+ if (render) {
|
|
|
+ return hepler.display({
|
|
|
+ id: dataIndex?.toString() ?? 'id',
|
|
|
header: title?.toString(),
|
|
|
- cell: props => props.getValue(),
|
|
|
+ cell(props) {
|
|
|
+ return render(
|
|
|
+ props.getValue(),
|
|
|
+ props.row.original as T,
|
|
|
+ props.row.index,
|
|
|
+ );
|
|
|
+ },
|
|
|
});
|
|
|
+ }
|
|
|
+ return hepler.accessor(dataIndex!.toString(), {
|
|
|
+ header: title?.toString(),
|
|
|
+ cell: props => props.getValue(),
|
|
|
});
|
|
|
+ });
|
|
|
|
|
|
- tableColumns.unshift(
|
|
|
- hepler.display({
|
|
|
- header: '序号',
|
|
|
- id: 'no',
|
|
|
- cell({row}) {
|
|
|
- return row.index;
|
|
|
- },
|
|
|
- }),
|
|
|
- );
|
|
|
+ tableColumns.unshift(
|
|
|
+ hepler.display({
|
|
|
+ header: '序号',
|
|
|
+ id: 'no',
|
|
|
+ cell({row}) {
|
|
|
+ return row.index;
|
|
|
+ },
|
|
|
+ }),
|
|
|
+ );
|
|
|
+
|
|
|
+ return tableColumns;
|
|
|
+}
|
|
|
|
|
|
- return tableColumns;
|
|
|
- }, []);
|
|
|
+export function useTable<T extends Record<string, any>>(
|
|
|
+ columns: ColumnType<T>[],
|
|
|
+ data: T[],
|
|
|
+) {
|
|
|
+ const columnList = useRef(parseColumn(columns));
|
|
|
|
|
|
const [columnSizing, setColumnSizing] = useState(function () {
|
|
|
const obj: Record<string, number> = {no: 64};
|
|
@@ -60,11 +64,13 @@ export function useTable<T extends Record<string, any>>(
|
|
|
|
|
|
const table = useReactTable({
|
|
|
data,
|
|
|
- columns: columnList,
|
|
|
+ columns: columnList.current,
|
|
|
state: {
|
|
|
columnSizing,
|
|
|
},
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
+ onColumnSizingChange: setColumnSizing,
|
|
|
+ columnResizeMode: 'onChange',
|
|
|
});
|
|
|
|
|
|
return {...table};
|