Skip to content

Commit

Permalink
feat: Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed Jun 18, 2024
1 parent 74bb2bf commit 580a8ce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
6 changes: 6 additions & 0 deletions app/js/hooks/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export default function useList<I, E = unknown, T = I>(
}, []);

const onChange = useCallback<OnChange>((page, pageSize) => {
const { pagination } = opitonsRef.current;

if (pagination) {
pagination.onChange?.(page, pageSize);
}

fetch({ pagination: { page, pageSize } });
}, []);

Expand Down
75 changes: 34 additions & 41 deletions app/js/hooks/usePagingOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,60 @@
* @module usePagingOptions
*/

import memoizeOne from 'memoize-one';
import { useCallback } from 'react';
import { PaginationProps } from 'antd';
import useLatestRef from './useLatestRef';
import { useCallback, useMemo } from 'react';

/**
* @function showTotal
* @param total 总条数
*/
function showTotal(total: number): string {
return `共 ${total} 条`;
export interface UsePagingOptions {
(pageSize: number): PagingOptions | undefined;
}

const DEFAULT_PAGE_SIZE_OPTIONS = [20, 30, 50, 80];

type PagingOptions = Omit<PaginationProps, 'total' | 'current' | 'pageSize' | 'defaultCurrent' | 'defaultPageSize'>;

export interface Options extends Omit<PagingOptions, 'pageSizeOptions'> {
pageSizeOptions?: number[];
}

type PagingOptions = Omit<PaginationProps, 'total' | 'current' | 'pageSize' | 'defaultCurrent' | 'defaultPageSize'>;

/**
* @function normalizePagingOptions
* @param pageSize 页大小
* @param opitons 分页配置
* @function showTotal
* @param total 总条数
*/
function normalizePagingOptions(pageSize: number, opitons?: Options | false): PagingOptions | undefined {
if (opitons !== false) {
const { pageSizeOptions = DEFAULT_PAGE_SIZE_OPTIONS } = opitons || {};

if (__DEV__) {
if (!pageSizeOptions.includes(pageSize)) {
console.error(new ReferenceError(`page size ${pageSize} not in options [${pageSizeOptions.join(', ')}]`));
}
}

return {
showTotal,
size: 'default',
responsive: true,
showSizeChanger: true,
showQuickJumper: true,
...opitons,
pageSizeOptions: pageSizeOptions.map(item => item.toString())
};
}
function showTotal(total: number): string {
return `共 ${total} 条`;
}

const DEFAULT_PAGE_SIZE_OPTIONS = [20, 30, 50, 80];

/**
* @function usePagingOptions
* @description [hook] 分页处理
* @param opitons 分页配置
*/
export default function usePagingOptions(opitons?: Options | false): (pageSize: number) => PagingOptions | undefined {
export default function usePagingOptions(opitons?: Options | false): UsePagingOptions {
const opitonsRef = useLatestRef(opitons);

const memoizeNormalizePagingOptions = useMemo(() => {
return memoizeOne(normalizePagingOptions);
}, []);
return useCallback<UsePagingOptions>(pageSize => {
const { current: opitons = {} } = opitonsRef;

if (opitons !== false) {
const { pageSizeOptions = DEFAULT_PAGE_SIZE_OPTIONS } = opitons;

return useCallback((pageSize: number): PagingOptions | undefined => {
return memoizeNormalizePagingOptions(pageSize, opitonsRef.current);
if (__DEV__) {
if (!pageSizeOptions.includes(pageSize)) {
console.error(new ReferenceError(`page size ${pageSize} not in options [${pageSizeOptions.join(', ')}]`));
}
}

return {
showTotal,
size: 'default',
responsive: true,
showSizeChanger: true,
showQuickJumper: true,
hideOnSinglePage: true,
...opitons,
pageSizeOptions: pageSizeOptions.map(item => item.toString())
};
}
}, []);
}

0 comments on commit 580a8ce

Please sign in to comment.