Skip to content

Commit

Permalink
chore: replace method
Browse files Browse the repository at this point in the history
  • Loading branch information
Wxh16144 committed Dec 22, 2022
1 parent 3a9b577 commit 04bb673
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 49 deletions.
31 changes: 6 additions & 25 deletions components/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { SpinProps } from '../spin';
import Spin from '../spin';
import type { Breakpoint } from '../_util/responsiveObserver';
import { responsiveArray } from '../_util/responsiveObserver';
import { mergeProps } from '../_util/withDefaultProps';
import Item from './Item';

// CSSINJS
Expand Down Expand Up @@ -74,26 +75,6 @@ export const ListContext = React.createContext<ListConsumerProps>({});

export const ListConsumer = ListContext.Consumer;

/**
* copy https://github.com/ant-design/ant-design/blob/536b8f8cd985dedb4419817b6430ada0494ce44e/components/table/hooks/usePagination.ts#L28-L43
*/
function extendsObject<T extends Object>(...list: T[]) {
const result: T = {} as T;

list.forEach((obj) => {
if (obj) {
Object.keys(obj).forEach((key) => {
const val = (obj as any)[key];
if (val !== undefined) {
(result as any)[key] = val;
}
});
}
});

return result;
}

function List<T>({
pagination = false as ListProps<any>['pagination'],
prefixCls: customizePrefixCls,
Expand Down Expand Up @@ -210,7 +191,7 @@ function List<T>({
hashId,
);

const paginationProps = extendsObject<PaginationConfig>(
const paginationProps = mergeProps(
defaultPaginationProps,
{
total: dataSource.length,
Expand All @@ -220,8 +201,8 @@ function List<T>({
pagination || {},
);

const largestPage = Math.ceil(paginationProps.total! / paginationProps.pageSize!);
if (paginationProps.current! > largestPage) {
const largestPage = Math.ceil(paginationProps.total / paginationProps.pageSize);
if (paginationProps.current > largestPage) {
paginationProps.current = largestPage;
}
const paginationContent = pagination ? (
Expand All @@ -236,9 +217,9 @@ function List<T>({

let splitDataSource = [...dataSource];
if (pagination) {
if (dataSource.length > (paginationProps.current! - 1) * paginationProps.pageSize!) {
if (dataSource.length > (paginationProps.current - 1) * paginationProps.pageSize) {
splitDataSource = [...dataSource].splice(
(paginationProps.current! - 1) * paginationProps.pageSize!,
(paginationProps.current - 1) * paginationProps.pageSize,
paginationProps.pageSize,
);
}
Expand Down
28 changes: 4 additions & 24 deletions components/table/hooks/usePagination.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState } from 'react';
import type { PaginationProps } from '../../pagination';
import type { TablePaginationConfig } from '../interface';
import { mergeProps } from '../../_util/withDefaultProps';

export const DEFAULT_PAGE_SIZE = 10;

Expand All @@ -25,23 +26,6 @@ export function getPaginationParam(
return param;
}

function extendsObject<T extends Object>(...list: T[]) {
const result: T = {} as T;

list.forEach((obj) => {
if (obj) {
Object.keys(obj).forEach((key) => {
const val = (obj as any)[key];
if (val !== undefined) {
(result as any)[key] = val;
}
});
}
});

return result;
}

export default function usePagination(
total: number,
pagination: TablePaginationConfig | false | undefined,
Expand All @@ -60,13 +44,9 @@ export default function usePagination(
}));

// ============ Basic Pagination Config ============
const mergedPagination = extendsObject<Partial<TablePaginationConfig>>(
innerPagination,
paginationObj,
{
total: paginationTotal > 0 ? paginationTotal : total,
},
);
const mergedPagination = mergeProps(innerPagination, paginationObj, {
total: paginationTotal > 0 ? paginationTotal : total,
});

// Reset `current` if data length or pageSize changed
const maxPage = Math.ceil((paginationTotal || total) / mergedPagination.pageSize!);
Expand Down

0 comments on commit 04bb673

Please sign in to comment.