refactor(projects): finish refactor useTable

This commit is contained in:
Soybean 2024-03-19 01:01:00 +08:00
parent c3efa1b6e0
commit 8630175a3f
6 changed files with 23 additions and 40 deletions

View File

@ -13,5 +13,6 @@
"i18n-ally.keystyle": "nested", "i18n-ally.keystyle": "nested",
"i18n-ally.localesPaths": ["src/locales/langs"], "i18n-ally.localesPaths": ["src/locales/langs"],
"prettier.enable": false, "prettier.enable": false,
"unocss.root": ["./"] "unocss.root": ["./"],
"typescript.tsdk": "node_modules/typescript/lib"
} }

View File

@ -17,22 +17,16 @@ export type TableColumnCheck = {
checked: boolean; checked: boolean;
}; };
export type TransformedData<T extends TableData = TableData> = { export type TransformedData<T> = {
data: T[]; data: T[];
pageNum: number; pageNum: number;
pageSize: number; pageSize: number;
total: number; total: number;
}; };
export type Transformer<T extends TableData = TableData, Response = object> = ( export type Transformer<T, Response> = (response: Response) => TransformedData<T>;
response: Response
) => TransformedData<T>;
export type TableConfig< export type TableConfig<A extends ApiFn, T, C> = {
A extends ApiFn = ApiFn,
T extends TableData = TableData,
C extends TableColumn = TableColumn
> = {
/** api function to get table data */ /** api function to get table data */
apiFn: A; apiFn: A;
/** api params */ /** api params */
@ -67,11 +61,7 @@ export type TableConfig<
immediate?: boolean; immediate?: boolean;
}; };
export default function useTable< export default function useTable<A extends ApiFn, T, C>(config: TableConfig<A, T, C>) {
A extends ApiFn = ApiFn,
T extends TableData = TableData,
C extends TableColumn = TableColumn
>(config: TableConfig<A, T, C>) {
const { loading, startLoading, endLoading } = useLoading(); const { loading, startLoading, endLoading } = useLoading();
const { bool: empty, setBool: setEmpty } = useBoolean(); const { bool: empty, setBool: setEmpty } = useBoolean();

View File

@ -6,10 +6,10 @@ import { useAppStore } from '@/store/modules/app';
import { $t } from '@/locales'; import { $t } from '@/locales';
type TableData = NaiveUI.TableData; type TableData = NaiveUI.TableData;
type GetTableData<A extends NaiveUI.TableApiFn> = NaiveUI.GetTableData<A>;
type TableColumn<T> = NaiveUI.TableColumn<T>;
export function useNaiveTable<T extends TableData = TableData, A extends NaiveUI.TableApiFn<T> = NaiveUI.TableApiFn<T>>( export function useNaiveTable<A extends NaiveUI.TableApiFn>(config: NaiveUI.NaiveTableConfig<A>) {
config: NaiveUI.NaiveTableConfig<T, A>
) {
const scope = effectScope(); const scope = effectScope();
const appStore = useAppStore(); const appStore = useAppStore();
@ -28,7 +28,7 @@ export function useNaiveTable<T extends TableData = TableData, A extends NaiveUI
searchParams, searchParams,
updateSearchParams, updateSearchParams,
resetSearchParams resetSearchParams
} = useTable<A, T, NaiveUI.TableColumn<T>>({ } = useTable<A, GetTableData<A>, TableColumn<GetTableData<A>>>({
apiFn, apiFn,
apiParams, apiParams,
columns: config.columns, columns: config.columns,
@ -64,7 +64,7 @@ export function useNaiveTable<T extends TableData = TableData, A extends NaiveUI
return checks; return checks;
}, },
getColumns: (cols, checks) => { getColumns: (cols, checks) => {
const columnMap = new Map<string, NaiveUI.TableColumn<T>>(); const columnMap = new Map<string, TableColumn<GetTableData<A>>>();
cols.forEach(column => { cols.forEach(column => {
if (isTableColumnHasKey(column)) { if (isTableColumnHasKey(column)) {
@ -76,7 +76,7 @@ export function useNaiveTable<T extends TableData = TableData, A extends NaiveUI
const filteredColumns = checks const filteredColumns = checks
.filter(item => item.checked) .filter(item => item.checked)
.map(check => columnMap.get(check.key) as NaiveUI.TableColumn<T>); .map(check => columnMap.get(check.key) as TableColumn<GetTableData<A>>);
return filteredColumns; return filteredColumns;
}, },
@ -225,8 +225,6 @@ export function getNaiveTableRowKey<T extends TableData>(row: T) {
return row.id; return row.id;
} }
function isTableColumnHasKey<T extends TableData>( function isTableColumnHasKey<T>(column: TableColumn<T>): column is NaiveUI.TableColumnWithKey<T> {
column: NaiveUI.TableColumn<T>
): column is NaiveUI.TableColumnWithKey<T> {
return Boolean((column as NaiveUI.TableColumnWithKey<T>).key); return Boolean((column as NaiveUI.TableColumnWithKey<T>).key);
} }

View File

@ -16,7 +16,7 @@ declare namespace Api {
} }
/** common params of paginating query list data */ /** common params of paginating query list data */
interface PaginatingQueryRecord<T extends NonNullable<unknown>> extends PaginatingCommonParams { interface PaginatingQueryRecord<T = any> extends PaginatingCommonParams {
records: T[]; records: T[];
} }
@ -29,7 +29,7 @@ declare namespace Api {
type EnableStatus = '1' | '2'; type EnableStatus = '1' | '2';
/** common record */ /** common record */
type CommonRecord<T extends NonNullable<unknown>> = { type CommonRecord<T = any> = {
/** record id */ /** record id */
id: number; id: number;
/** record creator */ /** record creator */

View File

@ -16,17 +16,12 @@ declare namespace NaiveUI {
type TableData = Api.Common.CommonRecord<object>; type TableData = Api.Common.CommonRecord<object>;
type TableColumnWithKey<T extends TableData = TableData> = type TableColumnWithKey<T> = SetTableColumnKey<DataTableBaseColumn<T>, T> | SetTableColumnKey<TableColumnGroup<T>, T>;
| SetTableColumnKey<DataTableBaseColumn<T>, T>
| SetTableColumnKey<TableColumnGroup<T>, T>;
type TableColumn<T extends TableData = TableData> = type TableColumn<T> = TableColumnWithKey<T> | DataTableSelectionColumn<T> | DataTableExpandColumn<T>;
| TableColumnWithKey<T>
| DataTableSelectionColumn<T>
| DataTableExpandColumn<T>;
type TableApiFn<T extends TableData = TableData> = ( type TableApiFn<T = any, R = Api.SystemManage.CommonSearchParams> = (
params: Api.SystemManage.CommonSearchParams params: R
) => Promise<FlatResponseData<Api.Common.PaginatingQueryRecord<T>>>; ) => Promise<FlatResponseData<Api.Common.PaginatingQueryRecord<T>>>;
/** /**
@ -37,8 +32,10 @@ declare namespace NaiveUI {
*/ */
type TableOperateType = 'add' | 'edit'; type TableOperateType = 'add' | 'edit';
type NaiveTableConfig<T extends TableData = TableData, A extends TableApiFn<T> = TableApiFn<T>> = Pick< type GetTableData<A extends TableApiFn> = A extends TableApiFn<infer T> ? T : never;
import('@sa/hooks').TableConfig<A, T, TableColumn<T>>,
type NaiveTableConfig<A extends TableApiFn> = Pick<
import('@sa/hooks').TableConfig<A, GetTableData<A>, TableColumn<GetTableData<A>>>,
'apiFn' | 'apiParams' | 'columns' | 'immediate' 'apiFn' | 'apiParams' | 'columns' | 'immediate'
>; >;
} }

View File

@ -1,7 +1,4 @@
<script setup lang="tsx"> <script setup lang="tsx">
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import { NButton, NPopconfirm, NTag } from 'naive-ui'; import { NButton, NPopconfirm, NTag } from 'naive-ui';
import { fetchGetUserList } from '@/service/api'; import { fetchGetUserList } from '@/service/api';
import { $t } from '@/locales'; import { $t } from '@/locales';