Skip to content

Commit 0614c2f

Browse files
authored
fix: address ai review feedback for pagination a11y
1 parent d52f9af commit 0614c2f

7 files changed

Lines changed: 581 additions & 130 deletions

File tree

src/Options.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { KeyCode } from '@rc-component/util';
22
import React from 'react';
33
import type { PaginationLocale } from './interface';
4+
import getNonEmptyString from './utils/getNonEmptyString';
45

56
export type SizeChangerRender = (info: {
67
disabled: boolean;
@@ -31,10 +32,6 @@ interface OptionsProps {
3132

3233
const defaultPageSizeOptions = [10, 20, 50, 100];
3334

34-
function getNonEmptyString(...values: (string | undefined)[]) {
35-
return values.find((value) => value && value.trim())?.trim();
36-
}
37-
3835
const Options: React.FC<OptionsProps> = (props) => {
3936
const {
4037
pageSizeOptions = defaultPageSizeOptions,

src/Pager.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ const Pager: React.FC<PagerProps> = (props) => {
5252
onKeyPress(e, onClick, page);
5353
};
5454

55-
const pager = itemRender(page, 'page', <a rel="nofollow">{page}</a>);
55+
const pager = itemRender(
56+
page,
57+
'page',
58+
<a tabIndex={-1} aria-hidden="true" rel="nofollow">
59+
{page}
60+
</a>,
61+
);
5662
const pagerLabel = `${pageLabel || 'Page'} ${page}`.trim();
5763

5864
return pager ? (

src/Pagination.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import zhCN from './locale/zh_CN';
1111
import Options from './Options';
1212
import type { PagerProps } from './Pager';
1313
import Pager from './Pager';
14+
import getNonEmptyString from './utils/getNonEmptyString';
1415

1516
const defaultItemRender: PaginationProps['itemRender'] = (_, __, element) =>
1617
element;
@@ -32,10 +33,6 @@ function calculatePage(p: number | undefined, pageSize: number, total: number) {
3233
return Math.floor((total - 1) / _pageSize) + 1;
3334
}
3435

35-
function getNonEmptyString(...values: (string | undefined)[]) {
36-
return values.find((value) => value && value.trim())?.trim();
37-
}
38-
3936
const Pagination: React.FC<PaginationProps> = (props) => {
4037
const {
4138
// cls
@@ -120,13 +117,14 @@ const Pagination: React.FC<PaginationProps> = (props) => {
120117

121118
function getItemIcon(
122119
icon: React.ReactNode | React.ComponentType<PaginationProps>,
123-
label: string,
120+
_label: string,
124121
title?: string,
125122
) {
126123
let iconNode = icon || (
127124
<button
128125
type="button"
129-
aria-label={label}
126+
tabIndex={-1}
127+
aria-hidden="true"
130128
title={title}
131129
className={`${prefixCls}-item-link`}
132130
/>
@@ -159,7 +157,6 @@ const Pagination: React.FC<PaginationProps> = (props) => {
159157

160158
const shouldDisplayQuickJumper = total > pageSize ? showQuickJumper : false;
161159
const pageLabelText = getNonEmptyString(locale.page, 'Page');
162-
const pageSizeLabelText = getNonEmptyString(locale.page_size, 'Page Size');
163160

164161
/**
165162
* prevent "up arrow" key reseting cursor position within textbox
@@ -254,7 +251,7 @@ const Pagination: React.FC<PaginationProps> = (props) => {
254251
handleChange(jumpNextPage);
255252
}
256253

257-
function runIfEnter(
254+
function runIfEnterOrSpace(
258255
event: React.KeyboardEvent<HTMLLIElement>,
259256
callback: (...args: any[]) => void,
260257
...restParams: any[]
@@ -272,19 +269,19 @@ const Pagination: React.FC<PaginationProps> = (props) => {
272269
}
273270

274271
function runIfEnterPrev(event: React.KeyboardEvent<HTMLLIElement>) {
275-
runIfEnter(event, prevHandle);
272+
runIfEnterOrSpace(event, prevHandle);
276273
}
277274

278275
function runIfEnterNext(event: React.KeyboardEvent<HTMLLIElement>) {
279-
runIfEnter(event, nextHandle);
276+
runIfEnterOrSpace(event, nextHandle);
280277
}
281278

282279
function runIfEnterJumpPrev(event: React.KeyboardEvent<HTMLLIElement>) {
283-
runIfEnter(event, jumpPrevHandle);
280+
runIfEnterOrSpace(event, jumpPrevHandle);
284281
}
285282

286283
function runIfEnterJumpNext(event: React.KeyboardEvent<HTMLLIElement>) {
287-
runIfEnter(event, jumpNextHandle);
284+
runIfEnterOrSpace(event, jumpNextHandle);
288285
}
289286

290287
function renderPrev(prevPage: number) {
@@ -354,7 +351,7 @@ const Pagination: React.FC<PaginationProps> = (props) => {
354351
const pagerProps: PagerProps = {
355352
rootPrefixCls: prefixCls,
356353
onClick: handleChange,
357-
onKeyPress: runIfEnter,
354+
onKeyPress: runIfEnterOrSpace,
358355
showTitle,
359356
itemRender,
360357
pageLabel: pageLabelText,
@@ -628,11 +625,7 @@ const Pagination: React.FC<PaginationProps> = (props) => {
628625
{simple ? simplePager : pagerList}
629626
{next}
630627
<Options
631-
locale={{
632-
...locale,
633-
page_size: pageSizeLabelText,
634-
page: getNonEmptyString(locale.page, pageLabelText, 'Page'),
635-
}}
628+
locale={locale}
636629
rootPrefixCls={prefixCls}
637630
disabled={disabled}
638631
selectPrefixCls={selectPrefixCls}

src/utils/getNonEmptyString.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function getNonEmptyString(...values: unknown[]) {
2+
return values
3+
.find((value) => typeof value === 'string' && value.trim())
4+
?.trim();
5+
}

0 commit comments

Comments
 (0)