Repository navigation
fix: move pagination control semantics off list items #728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import React from 'react'; | ||
|
|
||
| interface PaginationItemProps extends React.LiHTMLAttributes<HTMLLIElement> { | ||
| disabled?: boolean; | ||
| } | ||
|
|
||
| /** Keep the list item structural when itemRender supplies a native control. */ | ||
| const PaginationItem: React.FC<PaginationItemProps> = ({ | ||
| children, | ||
| disabled, | ||
| onClick, | ||
| onKeyDown, | ||
| tabIndex, | ||
| 'aria-label': ariaLabel, | ||
| 'aria-current': ariaCurrent, | ||
| 'aria-disabled': ariaDisabled, | ||
| ...restProps | ||
| }) => { | ||
| const nativeControl = | ||
| React.isValidElement<React.HTMLProps<HTMLElement>>(children) && | ||
| (children.type === 'button' || children.type === 'a'); | ||
|
|
||
| if (!nativeControl) { | ||
| // Arbitrary components need not forward DOM props. Preserve their existing | ||
| // content and delegated interaction instead of wrapping interactive nodes. | ||
| return ( | ||
| <li | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. React Doctor · Keyboard & screen reader users can't trigger this Fix → Put interactions on a button or link, or add an interactive role. |
||
| {...restProps} | ||
| onClick={onClick} | ||
| onKeyDown={onKeyDown} | ||
| tabIndex={tabIndex} | ||
| aria-label={ariaLabel} | ||
| aria-current={ariaCurrent} | ||
| aria-disabled={ariaDisabled} | ||
| > | ||
| {React.isValidElement(children) && | ||
| typeof children.type !== 'string' && | ||
| ariaDisabled !== undefined | ||
| ? React.cloneElement( | ||
| children as React.ReactElement<{ disabled?: boolean }>, | ||
| { disabled }, | ||
| ) | ||
| : children} | ||
| </li> | ||
| ); | ||
| } | ||
|
|
||
| const button = children.type === 'button'; | ||
| const link = !button && !!children.props.href; | ||
| const controlDisabled = disabled || (button && children.props.disabled); | ||
| const control = React.cloneElement(children, { | ||
| 'aria-hidden': undefined, | ||
| 'aria-label': children.props['aria-label'] ?? ariaLabel, | ||
| 'aria-current': ariaCurrent, | ||
| 'aria-disabled': controlDisabled || ariaDisabled, | ||
| tabIndex: controlDisabled ? -1 : 0, | ||
| ...(button ? { type: 'button', disabled: controlDisabled } : {}), | ||
| ...(!button && !link | ||
| ? { | ||
| role: 'button', | ||
| onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => { | ||
| children.props.onKeyDown?.(event); | ||
| if (!event.defaultPrevented) { | ||
| onKeyDown?.(event as React.KeyboardEvent<HTMLLIElement>); | ||
| } | ||
| }, | ||
| } | ||
| : {}), | ||
| onClick: (event: React.MouseEvent<HTMLElement>) => { | ||
| if (controlDisabled) { | ||
| event.preventDefault(); | ||
| return; | ||
| } | ||
| children.props.onClick?.(event); | ||
| if (!event.isPropagationStopped()) { | ||
| onClick?.(event as React.MouseEvent<HTMLLIElement>); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| // Native buttons synthesize clicks for Enter/Space; handling keydown on the | ||
| // parent as well would activate the same action twice. | ||
| return ( | ||
| <li {...restProps} aria-disabled={ariaDisabled}> | ||
| {control} | ||
| </li> | ||
| ); | ||
| }; | ||
|
|
||
| export default PaginationItem; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
React Doctor ·
react-doctor/no-high-complexity-react-function(warning)PaginationItemhas cyclomatic complexity 16, cognitive complexity 15, and maximum nesting depth 2, so its React logic is hard to understand and change. Extract independent branches into components or hooks.Fix → Extract independent render branches and state logic into focused components or hooks until the control flow is easy to follow.
Docs