Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Options.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { KeyCode } from '@rc-component/util';
import React from 'react';
import type { PaginationLocale } from './interface';
import isEnterOrSpaceKey from './isEnterOrSpaceKey';

export type SizeChangerRender = (info: {
disabled: boolean;
Expand All @@ -21,7 +22,7 @@ interface OptionsProps {
selectPrefixCls?: string;
pageSize: number;
pageSizeOptions?: number[];
goButton?: boolean | string;
goButton?: boolean | React.ReactNode;
changeSize?: (size: number) => void;
quickGo?: (value: number) => void;
buildOptionText?: (value: number | string) => string;
Expand Down Expand Up @@ -149,6 +150,21 @@ const Options: React.FC<OptionsProps> = (props) => {
>
{locale.jump_to_confirm}
</button>
) : typeof goButton === 'string' ? (
<span
role="button"
tabIndex={disabled ? -1 : 0}
aria-disabled={disabled || undefined}
onClick={go}
onKeyDown={(event) => {
if (isEnterOrSpaceKey(event)) {
event.preventDefault();
go({ type: 'click' });
}
}}
>
{goButton}
</span>
) : (
<span onClick={go} onKeyUp={go}>
{goButton}
Expand Down
20 changes: 19 additions & 1 deletion src/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ const Pagination: React.FC<PaginationProps> = (props) => {
}

function runIfEnterOrSpace(
event: React.KeyboardEvent<HTMLLIElement>,
event: React.KeyboardEvent<HTMLElement>,
callback: (...args: any[]) => void,
...restParams: any[]
) {
Expand Down Expand Up @@ -378,6 +378,24 @@ const Pagination: React.FC<PaginationProps> = (props) => {
{locale.jump_to_confirm}
</button>
);
} else if (typeof goButton === 'string') {
gotoButton = (
<span
role="button"
tabIndex={disabled ? -1 : 0}
aria-disabled={disabled || undefined}
onClick={handleGoTO}
onKeyDown={(event) =>
runIfEnterOrSpace(event, () => {
if (String(internalInputVal) !== '') {
handleChange(internalInputVal);
}
})
}
>
{goButton}
</span>
);
Comment thread
nrps9909 marked this conversation as resolved.
} else {
gotoButton = (
<span onClick={handleGoTO} onKeyUp={handleGoTO}>
Expand Down
65 changes: 65 additions & 0 deletions tests/jumper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,71 @@ describe('simple quick jumper', () => {
).toBeTruthy();
});

it('supports keyboard activation for a text goButton', () => {
wrapper = render(
<Pagination
onChange={onChange}
defaultCurrent={1}
total={25}
showQuickJumper={{ goButton: 'Go' }}
/>,
);

const input = wrapper.container.querySelector(
'.rc-pagination-options-quick-jumper input',
);
const goButton = wrapper.getByRole('button', { name: 'Go' });

fireEvent.change(input, { target: { value: '2' } });
fireEvent.keyDown(goButton, { key: ' ', keyCode: 32, which: 32 });

expect(onChange).toHaveBeenLastCalledWith(2, 10);
});

it('supports keyboard activation for a text goButton in simple mode', () => {
wrapper = render(
<Pagination
simple
onChange={onChange}
defaultCurrent={1}
total={25}
showQuickJumper={{ goButton: 'Go' }}
/>,
);

const input = wrapper.container.querySelector(
'.rc-pagination-simple-pager input',
);
const goButton = wrapper.getByRole('button', { name: 'Go' });

fireEvent.change(input, { target: { value: '2' } });
fireEvent.keyDown(goButton, { key: 'Enter', keyCode: 13, which: 13 });

expect(onChange).toHaveBeenLastCalledWith(2, 10);
});

it('does not activate a text goButton with empty simple input', () => {
wrapper = render(
<Pagination
simple
onChange={onChange}
defaultCurrent={2}
total={25}
showQuickJumper={{ goButton: 'Go' }}
/>,
);

const input = wrapper.container.querySelector(
'.rc-pagination-simple-pager input',
);
const goButton = wrapper.getByRole('button', { name: 'Go' });

fireEvent.change(input, { target: { value: '' } });
fireEvent.keyDown(goButton, { key: ' ', keyCode: 32, which: 32 });

expect(onChange).not.toHaveBeenCalled();
});

it('goButton defaultly hidden', () => {
wrapper = render(
<Pagination
Expand Down
Loading