Skip to content

Commit 662646a

Browse files
committed
Fix focus item after change current page
close ant-design/ant-design#8948
1 parent b954015 commit 662646a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/Pagination.jsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function defaultItemRender(page, type, element) {
2020

2121
export default class Pagination extends React.Component {
2222
static propTypes = {
23+
prefixCls: PropTypes.string,
2324
current: PropTypes.number,
2425
defaultCurrent: PropTypes.number,
2526
total: PropTypes.number,
@@ -110,6 +111,20 @@ export default class Pagination extends React.Component {
110111
}
111112
}
112113

114+
componentDidUpdate(prevProps, prevState) {
115+
// When current page change, fix focused style of prev item
116+
// A hacky solution of https://github.com/ant-design/ant-design/issues/8948
117+
const { prefixCls } = this.props;
118+
if (prevState.current !== this.state.current && this.paginationNode) {
119+
const lastCurrentNode = this.paginationNode.querySelector(
120+
`.${prefixCls}-item-${prevState.current}`
121+
);
122+
if (lastCurrentNode && document.activeElement === lastCurrentNode) {
123+
lastCurrentNode.blur();
124+
}
125+
}
126+
}
127+
113128
getJumpPrevPage() {
114129
return Math.max(1, this.state.current - (this.props.showLessItems ? 3 : 5));
115130
}
@@ -118,6 +133,10 @@ export default class Pagination extends React.Component {
118133
return Math.min(this.calculatePage(), this.state.current + (this.props.showLessItems ? 3 : 5));
119134
}
120135

136+
savePaginationNode = (node) => {
137+
this.paginationNode = node;
138+
}
139+
121140
calculatePage = (p) => {
122141
let pageSize = p;
123142
if (typeof pageSize === 'undefined') {
@@ -517,6 +536,7 @@ export default class Pagination extends React.Component {
517536
className={`${prefixCls} ${props.className}`}
518537
style={props.style}
519538
unselectable="unselectable"
539+
ref={this.savePaginationNode}
520540
>
521541
{totalText}
522542
<li

0 commit comments

Comments
 (0)