Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hooks): useTable supports passing in the default transformer clo… #598

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions src/hooks/common/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,33 @@ export function useTable<A extends NaiveUI.TableApiFn>(config: NaiveUI.NaiveTabl

const isMobile = computed(() => appStore.isMobile);

const { apiFn, apiParams, immediate, showTotal } = config;
const { apiFn, transformer, apiParams, immediate, showTotal } = config;

const SELECTION_KEY = '__selection__';

const EXPAND_KEY = '__expand__';

const defaultTransformer = (res: Awaited<ReturnType<A>>) => {
const { records = [], current = 1, size = 10, total = 0 } = res.data || {};

// Ensure that the size is greater than 0, If it is less than 0, it will cause paging calculation errors.
const pageSize = size <= 0 ? 10 : size;

const recordsWithIndex = records.map((item, index) => {
return {
...item,
index: (current - 1) * pageSize + index + 1
};
});

return {
data: recordsWithIndex,
pageNum: current,
pageSize,
total
};
};

const {
loading,
empty,
Expand All @@ -37,26 +58,7 @@ export function useTable<A extends NaiveUI.TableApiFn>(config: NaiveUI.NaiveTabl
apiFn,
apiParams,
columns: config.columns,
transformer: res => {
const { records = [], current = 1, size = 10, total = 0 } = res.data || {};

// Ensure that the size is greater than 0, If it is less than 0, it will cause paging calculation errors.
const pageSize = size <= 0 ? 10 : size;

const recordsWithIndex = records.map((item, index) => {
return {
...item,
index: (current - 1) * pageSize + index + 1
};
});

return {
data: recordsWithIndex,
pageNum: current,
pageSize,
total
};
},
transformer: transformer || defaultTransformer,
getColumnChecks: cols => {
const checks: NaiveUI.TableColumnCheck[] = [];

Expand Down
3 changes: 3 additions & 0 deletions src/typings/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ declare namespace Api {
total: number;
}

/** common search params of table */
type CommonSearchParams = Pick<Common.PaginatingCommonParams, 'current' | 'size'>;

/** common params of paginating query list data */
interface PaginatingQueryRecord<T = any> extends PaginatingCommonParams {
records: T[];
Expand Down
9 changes: 8 additions & 1 deletion src/typings/naive-ui.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ declare namespace NaiveUI {

type TableColumn<T> = TableColumnWithKey<T> | DataTableSelectionColumn<T> | DataTableExpandColumn<T>;

type TableApiFn<T = any, R = Api.SystemManage.CommonSearchParams> = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

先单独提这个修复吧

type TableApiFn<T = any, R = Api.Common.CommonSearchParams> = (
params: R
) => Promise<FlatResponseData<Api.Common.PaginatingQueryRecord<T>>>;

Expand All @@ -50,5 +50,12 @@ declare namespace NaiveUI {
* @default false
*/
showTotal?: boolean;
/**
* the custom transformer function
*
* @param res the response data
* @returns the transformed data
*/
transformer?: (res: Awaited<ReturnType<A>>) => { data: any[]; pageNum: number; pageSize: number; total: number };
Copy link
Member

@honghuangdc honghuangdc Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我本地试了下,自定义的transformer的类型还不好处理

};
}
Loading