Skip to content

Commit 444b9a0

Browse files
committed
refactor: reuse Pagination component in Table and List
Replace custom pagination buttons in Table and List with the shared Pagination component, removing duplicated UI code and styles.
1 parent 5275a17 commit 444b9a0

6 files changed

Lines changed: 28 additions & 119 deletions

File tree

components/list/list.tsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import classNames from 'classnames';
33
import { ConfigContext } from '../config-provider/config-context';
44
import { getPrefixCls } from '../_utils/general';
55
import { useVirtualScroll } from '../_utils/use-virtual-scroll';
6+
import Pagination from '../pagination';
67
import { ListProps } from './types';
78

89
const ITEM_HEIGHT_MAP = { sm: 41, md: 49, lg: 57 } as const;
@@ -128,7 +129,6 @@ const List = React.forwardRef<HTMLDivElement, ListProps>((props, ref) => {
128129
const showPagination = pagination && !isVirtual;
129130
const paginationConfig = pagination && typeof pagination === 'object' ? pagination : undefined;
130131
const totalItems = paginationConfig?.total ?? dataSource.length;
131-
const totalPages = Math.ceil(totalItems / pageSize);
132132
const activePage = paginationConfig?.current ?? currentPage;
133133

134134
const bodyCls = classNames(`${prefixCls}__body`, {
@@ -150,21 +150,17 @@ const List = React.forwardRef<HTMLDivElement, ListProps>((props, ref) => {
150150
)}
151151
</div>
152152
{footer && <div className={`${prefixCls}__footer`}>{footer}</div>}
153-
{showPagination && totalPages > 1 && (
154-
<div className={`${prefixCls}__pagination`}>
155-
{Array.from({ length: totalPages }, (_, i) => i + 1).map((page) => (
156-
<button
157-
key={page}
158-
type="button"
159-
className={classNames(`${prefixCls}__page-btn`, {
160-
[`${prefixCls}__page-btn_active`]: page === activePage,
161-
})}
162-
onClick={() => handlePageChange(page)}
163-
>
164-
{page}
165-
</button>
166-
))}
167-
</div>
153+
{showPagination && (
154+
<Pagination
155+
current={activePage}
156+
total={totalItems}
157+
pageSize={pageSize}
158+
align={paginationConfig?.align ?? 'right'}
159+
size={paginationConfig?.size}
160+
disabled={paginationConfig?.disabled}
161+
onChange={(page) => handlePageChange(page)}
162+
style={{ padding: 16 }}
163+
/>
168164
)}
169165
</div>
170166
);

components/list/style/_index.scss

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,37 +61,6 @@
6161
color: var(--ty-color-text-secondary, #{$gray-500});
6262
}
6363

64-
&__pagination {
65-
display: flex;
66-
justify-content: flex-end;
67-
padding: 16px;
68-
gap: 4px;
69-
}
70-
71-
&__page-btn {
72-
display: inline-flex;
73-
align-items: center;
74-
justify-content: center;
75-
min-width: 32px;
76-
height: 32px;
77-
padding: 0 6px;
78-
border: 1px solid var(--ty-list-border, #{$gray-300});
79-
border-radius: $border-radius;
80-
background: transparent;
81-
cursor: pointer;
82-
font-size: $font-size-sm;
83-
84-
&:hover {
85-
color: var(--ty-color-primary, #{$primary-color});
86-
border-color: var(--ty-color-primary, #{$primary-color});
87-
}
88-
89-
&_active {
90-
color: var(--ty-color-primary, #{$primary-color});
91-
border-color: var(--ty-color-primary, #{$primary-color});
92-
font-weight: 500;
93-
}
94-
}
9564
}
9665

9766
.#{$prefix}-list-item {

components/list/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { BaseProps, SizeType } from '../_utils/props';
3+
import { PaginationProps } from '../pagination/types';
34

45
export interface ListProps<T = any>
56
extends BaseProps,
@@ -26,7 +27,7 @@ export interface ListGridType {
2627
column?: number;
2728
}
2829

29-
export interface ListPaginationProps {
30+
export interface ListPaginationProps extends Pick<PaginationProps, 'size' | 'align' | 'disabled'> {
3031
current?: number;
3132
pageSize?: number;
3233
total?: number;

components/table/style/_index.scss

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -140,42 +140,4 @@
140140
color: var(--ty-color-text-secondary, #{$gray-500});
141141
}
142142

143-
// Pagination
144-
&__pagination {
145-
display: flex;
146-
justify-content: flex-end;
147-
padding: 16px 0;
148-
gap: 4px;
149-
}
150-
151-
&__page-btn {
152-
display: inline-flex;
153-
align-items: center;
154-
justify-content: center;
155-
min-width: 32px;
156-
height: 32px;
157-
padding: 0 6px;
158-
border: 1px solid var(--ty-table-border, #{$gray-300});
159-
border-radius: $border-radius;
160-
background: transparent;
161-
cursor: pointer;
162-
font-size: $font-size-sm;
163-
color: var(--ty-color-text, #{$gray-700});
164-
165-
&:hover:not(:disabled) {
166-
color: var(--ty-color-primary, #{$primary-color});
167-
border-color: var(--ty-color-primary, #{$primary-color});
168-
}
169-
170-
&:disabled {
171-
opacity: 0.4;
172-
cursor: not-allowed;
173-
}
174-
175-
&_active {
176-
color: var(--ty-color-primary, #{$primary-color});
177-
border-color: var(--ty-color-primary, #{$primary-color});
178-
font-weight: 500;
179-
}
180-
}
181143
}

components/table/table.tsx

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import classNames from 'classnames';
33
import { ConfigContext } from '../config-provider/config-context';
44
import { getPrefixCls } from '../_utils/general';
55
import { useVirtualScroll } from '../_utils/use-virtual-scroll';
6+
import Pagination from '../pagination';
67
import { TableProps, ColumnType, SortOrder } from './types';
78

89
const ROW_HEIGHT_MAP = { sm: 40, md: 48, lg: 56 } as const;
@@ -121,7 +122,6 @@ const Table = React.forwardRef<HTMLDivElement, TableProps>((props, ref) => {
121122

122123
const paginationConfig = pagination && typeof pagination === 'object' ? pagination : undefined;
123124
const totalItems = paginationConfig?.total ?? dataSource.length;
124-
const totalPages = Math.ceil(totalItems / pageSize);
125125
const activePage = paginationConfig?.current ?? currentPage;
126126

127127
const handleSort = (col: ColumnType) => {
@@ -362,37 +362,17 @@ const Table = React.forwardRef<HTMLDivElement, TableProps>((props, ref) => {
362362
</tbody>
363363
</table>
364364
</div>
365-
{showPagination && totalPages > 1 && (
366-
<div className={`${prefixCls}__pagination`}>
367-
<button
368-
type="button"
369-
className={`${prefixCls}__page-btn`}
370-
disabled={activePage <= 1}
371-
onClick={() => handlePageChange(activePage - 1)}
372-
>
373-
374-
</button>
375-
{Array.from({ length: totalPages }, (_, i) => i + 1).map((page) => (
376-
<button
377-
key={page}
378-
type="button"
379-
className={classNames(`${prefixCls}__page-btn`, {
380-
[`${prefixCls}__page-btn_active`]: page === activePage,
381-
})}
382-
onClick={() => handlePageChange(page)}
383-
>
384-
{page}
385-
</button>
386-
))}
387-
<button
388-
type="button"
389-
className={`${prefixCls}__page-btn`}
390-
disabled={activePage >= totalPages}
391-
onClick={() => handlePageChange(activePage + 1)}
392-
>
393-
394-
</button>
395-
</div>
365+
{showPagination && (
366+
<Pagination
367+
current={activePage}
368+
total={totalItems}
369+
pageSize={pageSize}
370+
align={paginationConfig?.align ?? 'right'}
371+
size={paginationConfig?.size}
372+
disabled={paginationConfig?.disabled}
373+
onChange={(page) => handlePageChange(page)}
374+
style={{ padding: '16px 0' }}
375+
/>
396376
)}
397377
</div>
398378
);

components/table/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { BaseProps, SizeType } from '../_utils/props';
3+
import { PaginationProps } from '../pagination/types';
34

45
export type SortOrder = 'ascend' | 'descend' | null;
56
export type ColumnAlign = 'left' | 'center' | 'right';
@@ -24,7 +25,7 @@ export type RowSelection<T = any> = {
2425
type?: 'checkbox' | 'radio';
2526
};
2627

27-
export interface TablePaginationConfig {
28+
export interface TablePaginationConfig extends Pick<PaginationProps, 'size' | 'align' | 'disabled'> {
2829
current?: number;
2930
pageSize?: number;
3031
total?: number;

0 commit comments

Comments
 (0)