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

[功能]: useTable支持传入transformer #595

Closed
Azir-11 opened this issue Aug 11, 2024 · 1 comment
Closed

[功能]: useTable支持传入transformer #595

Azir-11 opened this issue Aug 11, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@Azir-11
Copy link
Member

Azir-11 commented Aug 11, 2024

描述

useTable函数支持传入transformer,未传入时使用defaultTransformer,增强该hook的扩展性

建议的解决方案

useTable的签名更改为:

useTable<A extends NaiveUI.TableApiFn>(
  config: NaiveUI.NaiveTableConfig<A> & {
    transformer?: (res: Awaited<ReturnType<A>>) => { data: any[]; pageNum: number; pageSize: number; total: number };
  }
)

提供默认的转换函数:

const defaultTransformer = (res: Awaited<ReturnType<A>>) => {
    const { list = [], page = 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 = list.map((item, index) => {
      return {
        ...item,
        index: (page - 1) * pageSize + index + 1
      };
    });

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

给useHookTable传入:

transformer: transformer || defaultTransformer

替代方案

No response

额外的上下文

No response

@Azir-11 Azir-11 self-assigned this Aug 11, 2024
@Azir-11 Azir-11 added the enhancement New feature or request label Aug 11, 2024
@Azir-11 Azir-11 removed their assignment Aug 28, 2024
@honghuangdc
Copy link
Member

我尝试过了,由于类型问题,暂时没法做自定义的transformer,只能根据类型限定返回结果,就像在 useHookTable基础上做封装

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants