Skip to content

Commit 25bc20d

Browse files
committed
Add pageSizeChanger props for handling the page size Select component showSearch, size options, and onChange together.
1 parent 8a2b7fa commit 25bc20d

File tree

8 files changed

+245
-32
lines changed

8 files changed

+245
-32
lines changed

README.md

Lines changed: 30 additions & 29 deletions
Large diffs are not rendered by default.

docs/demo/pageSizeChanger.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: pageSizeChanger
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/pageSizeChanger.tsx"></code>

docs/examples/pageSizeChanger.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import Pagination from 'rc-pagination';
3+
import Select from 'rc-select';
4+
import '../../assets/index.less';
5+
6+
export default () => {
7+
const pageSizeOnChange = (size) => {
8+
console.log({ size });
9+
};
10+
11+
return (
12+
<>
13+
<Pagination
14+
defaultCurrent={1}
15+
total={50}
16+
selectComponentClass={Select}
17+
showSizeChanger
18+
pageSizeChanger={{
19+
options: [10, 25, 50, 75, 100],
20+
showSearch: false,
21+
onChange: pageSizeOnChange,
22+
}}
23+
/>
24+
</>
25+
);
26+
};

src/Options.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface OptionsProps {
2525
selectComponentClass: React.ComponentType<Partial<InternalSelectProps>> & {
2626
Option?: React.ComponentType<Partial<OptionProps>>;
2727
};
28+
showSearch: boolean;
29+
onChange: (size: number) => void;
2830
}
2931

3032
const defaultPageSizeOptions = ['10', '20', '50', '100'];
@@ -42,6 +44,8 @@ const Options: React.FC<OptionsProps> = (props) => {
4244
selectPrefixCls,
4345
disabled,
4446
buildOptionText,
47+
showSearch = false,
48+
onChange,
4549
} = props;
4650

4751
const [goInputText, setGoInputText] = React.useState('');
@@ -59,6 +63,7 @@ const Options: React.FC<OptionsProps> = (props) => {
5963

6064
const changeSizeHandle = (value: number) => {
6165
changeSize?.(Number(value));
66+
onChange?.(Number(value));
6267
};
6368

6469
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
@@ -128,7 +133,7 @@ const Options: React.FC<OptionsProps> = (props) => {
128133
<Select
129134
disabled={disabled}
130135
prefixCls={selectPrefixCls}
131-
showSearch={false}
136+
showSearch={showSearch}
132137
className={`${prefixCls}-size-changer`}
133138
optionLabelProp="children"
134139
popupMatchSelectWidth={false}

src/Pagination.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const defaultItemRender: PaginationProps['itemRender'] = (
1616
element,
1717
) => element;
1818

19+
const defaultPageSizeChanger: PaginationProps['pageSizeChanger'] = {
20+
showSearch: false,
21+
};
22+
1923
function noop() {}
2024

2125
function isInteger(v: number) {
@@ -65,6 +69,7 @@ const Pagination: React.FC<PaginationProps> = (props) => {
6569
showTotal,
6670
showSizeChanger: showSizeChangerProp,
6771
pageSizeOptions,
72+
pageSizeChanger = defaultPageSizeChanger,
6873

6974
// render
7075
itemRender = defaultItemRender,
@@ -588,9 +593,11 @@ const Pagination: React.FC<PaginationProps> = (props) => {
588593
selectPrefixCls={selectPrefixCls}
589594
changeSize={showSizeChanger ? changePageSize : null}
590595
pageSize={pageSize}
591-
pageSizeOptions={pageSizeOptions}
596+
pageSizeOptions={pageSizeOptions || pageSizeChanger?.options}
592597
quickGo={shouldDisplayQuickJumper ? handleChange : null}
593598
goButton={gotoButton}
599+
onChange={pageSizeChanger?.onChange}
600+
showSearch={pageSizeChanger?.showSearch}
594601
/>
595602
</ul>
596603
);

src/interface.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ export interface PaginationLocale {
1717
page_size?: string;
1818
}
1919

20+
export interface pageSizeChangerLocale {
21+
options?: string[] | number[];
22+
showSearch?: boolean;
23+
onChange?: (size: number) => void;
24+
}
25+
2026
export interface PaginationData {
2127
className: string;
2228
selectPrefixCls: string;
2329
prefixCls: string;
2430
pageSizeOptions: string[] | number[];
31+
pageSizeChanger: pageSizeChangerLocale;
2532

2633
current: number;
2734
defaultCurrent: number;
@@ -37,7 +44,7 @@ export interface PaginationData {
3744
showPrevNextJumpers: boolean;
3845
showQuickJumper: boolean | object;
3946
showTitle: boolean;
40-
simple: boolean | { readOnly?: boolean; };
47+
simple: boolean | { readOnly?: boolean };
4148
disabled: boolean;
4249

4350
locale: PaginationLocale;

tests/__snapshots__/demo.test.tsx.snap

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,6 +2874,134 @@ exports[`Example more 1`] = `
28742874
</div>
28752875
`;
28762876

2877+
exports[`Example pageSizeChanger 1`] = `
2878+
<div>
2879+
<ul
2880+
class="rc-pagination"
2881+
>
2882+
<li
2883+
aria-disabled="true"
2884+
class="rc-pagination-prev rc-pagination-disabled"
2885+
title="上一页"
2886+
>
2887+
<button
2888+
aria-label="prev page"
2889+
class="rc-pagination-item-link"
2890+
disabled=""
2891+
type="button"
2892+
/>
2893+
</li>
2894+
<li
2895+
class="rc-pagination-item rc-pagination-item-1 rc-pagination-item-active"
2896+
tabindex="0"
2897+
title="1"
2898+
>
2899+
<a
2900+
rel="nofollow"
2901+
>
2902+
1
2903+
</a>
2904+
</li>
2905+
<li
2906+
class="rc-pagination-item rc-pagination-item-2"
2907+
tabindex="0"
2908+
title="2"
2909+
>
2910+
<a
2911+
rel="nofollow"
2912+
>
2913+
2
2914+
</a>
2915+
</li>
2916+
<li
2917+
class="rc-pagination-item rc-pagination-item-3"
2918+
tabindex="0"
2919+
title="3"
2920+
>
2921+
<a
2922+
rel="nofollow"
2923+
>
2924+
3
2925+
</a>
2926+
</li>
2927+
<li
2928+
class="rc-pagination-item rc-pagination-item-4"
2929+
tabindex="0"
2930+
title="4"
2931+
>
2932+
<a
2933+
rel="nofollow"
2934+
>
2935+
4
2936+
</a>
2937+
</li>
2938+
<li
2939+
class="rc-pagination-item rc-pagination-item-5"
2940+
tabindex="0"
2941+
title="5"
2942+
>
2943+
<a
2944+
rel="nofollow"
2945+
>
2946+
5
2947+
</a>
2948+
</li>
2949+
<li
2950+
aria-disabled="false"
2951+
class="rc-pagination-next"
2952+
tabindex="0"
2953+
title="下一页"
2954+
>
2955+
<button
2956+
aria-label="next page"
2957+
class="rc-pagination-item-link"
2958+
type="button"
2959+
/>
2960+
</li>
2961+
<li
2962+
class="rc-pagination-options"
2963+
>
2964+
<div
2965+
aria-label="页码"
2966+
class="rc-select rc-pagination-options-size-changer rc-select-single"
2967+
>
2968+
<div
2969+
class="rc-select-selector"
2970+
>
2971+
<span
2972+
class="rc-select-selection-search"
2973+
>
2974+
<input
2975+
aria-autocomplete="list"
2976+
aria-controls="rc_select_TEST_OR_SSR_list"
2977+
aria-expanded="false"
2978+
aria-haspopup="listbox"
2979+
aria-label="页码"
2980+
aria-owns="rc_select_TEST_OR_SSR_list"
2981+
autocomplete="off"
2982+
class="rc-select-selection-search-input"
2983+
id="rc_select_TEST_OR_SSR"
2984+
readonly=""
2985+
role="combobox"
2986+
style="opacity: 0;"
2987+
type="search"
2988+
unselectable="on"
2989+
value=""
2990+
/>
2991+
</span>
2992+
<span
2993+
class="rc-select-selection-item"
2994+
title="10 条/页"
2995+
>
2996+
10 条/页
2997+
</span>
2998+
</div>
2999+
</div>
3000+
</li>
3001+
</ul>
3002+
</div>
3003+
`;
3004+
28773005
exports[`Example showTitle 1`] = `
28783006
<div>
28793007
<div>

tests/pageSizeChanger.text.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import { render, fireEvent } from '@testing-library/react';
3+
import Select from 'rc-select';
4+
import Pagination from '../src';
5+
6+
describe('Pagination with pageSizeChanger', () => {
7+
it('should onChange called when pageSize change', () => {
8+
const onChange = jest.fn();
9+
const { container, getByRole } = render(
10+
<Pagination
11+
defaultCurrent={1}
12+
total={500}
13+
selectComponentClass={Select}
14+
showSizeChanger
15+
pageSizeChanger={{
16+
options: [10, 25, 50, 75, 100],
17+
showSearch: false,
18+
onChange: onChange,
19+
}}
20+
/>,
21+
);
22+
fireEvent.mouseDown(getByRole('combobox'));
23+
expect(container.querySelectorAll('.rc-select-item')[2]).toHaveTextContent(
24+
'50 条/页',
25+
);
26+
const pageSize1 = container.querySelectorAll('.rc-select-item')[0];
27+
fireEvent.click(pageSize1);
28+
expect(onChange).toHaveBeenCalled();
29+
expect(onChange).toHaveBeenLastCalledWith(10);
30+
});
31+
});

0 commit comments

Comments
 (0)