Skip to content
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

fix(ui-pagination): fix Pagination sometimes displaying non li elements in lists, fix key errors too #1839

Merged
merged 2 commits into from
Feb 3, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,17 @@ describe('<Pagination />', () => {
it('by default', async () => {
const { container } = render(
<Pagination variant="compact" labelNext="Next" labelPrev="Prev">
<ul>
{buildPages(5)}
</ul>
{buildPages(5)}
</Pagination>
)

const axeCheck = await runAxeCheck(container)
expect(axeCheck).toBe(true)
})

it('by default with more pages', async () => {
const { container } = render(
<Pagination variant="compact" labelNext="Next" labelPrev="Prev">
<ul>{buildPages(8)}</ul>
{buildPages(8)}
</Pagination>
)
const axeCheck = await runAxeCheck(container)
Expand All @@ -153,7 +150,7 @@ describe('<Pagination />', () => {
labelLast="Last"
withFirstAndLastButton
>
<ul>{buildPages(8)}</ul>
{buildPages(8)}
</Pagination>
)
const axeCheck = await runAxeCheck(container)
Expand All @@ -171,7 +168,7 @@ describe('<Pagination />', () => {
withFirstAndLastButton
showDisabledButtons
>
<ul>{buildPages(8)}</ul>
{buildPages(8)}
</Pagination>
)
const axeCheck = await runAxeCheck(container)
Expand Down Expand Up @@ -966,7 +963,7 @@ describe('<Pagination />', () => {
totalPageNumber={9}
/>
)
expect(container.firstChild).toHaveTextContent('12...9Next Page')
expect(container.firstChild).toHaveTextContent('129Next Page')
})
it('should render the correct pages - 2', () => {
const { container } = render(
Expand All @@ -979,7 +976,7 @@ describe('<Pagination />', () => {
/>
)
expect(container.firstChild).toHaveTextContent(
'Previous Page1...456...9Next Page'
'Previous Page14569Next Page'
)
})
it('should render the correct pages - 3', () => {
Expand Down Expand Up @@ -1011,7 +1008,7 @@ describe('<Pagination />', () => {
/>
)
expect(container.firstChild).toHaveTextContent(
'Previous Page12...456...89Next Page'
'Previous Page1245689Next Page'
)
})
it('should render the correct pages - 5', () => {
Expand Down Expand Up @@ -1056,7 +1053,7 @@ describe('<Pagination />', () => {
siblingCount={1}
/>
)
expect(container.firstChild).toHaveTextContent('123...789Next Page')
expect(container.firstChild).toHaveTextContent('123789Next Page')
})
it('should render the correct ellipsis', () => {
const { container } = render(
Expand Down Expand Up @@ -1098,7 +1095,7 @@ describe('<Pagination />', () => {
/>
)
expect(container.firstChild).toHaveTextContent(
'Previous Page1...567756785679...1000000000000000Next Page'
'Previous Page15677567856791000000000000000Next Page'
)
})
it('should render first and last buttons', () => {
Expand All @@ -1115,7 +1112,7 @@ describe('<Pagination />', () => {
/>
)
expect(container.firstChild).toHaveTextContent(
'First PagePrevious Page1...456...100Next PageLast Page'
'First PagePrevious Page1456100Next PageLast Page'
)
})
it('should render every page if boundary and sibling counts are big enough', () => {
Expand Down
44 changes: 28 additions & 16 deletions packages/ui-pagination/src/Pagination/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE.
*/
/** @jsx jsx */
import React, { Component } from 'react'
import React, { Component, ReactElement, ReactNode } from 'react'

import { View } from '@instructure/ui-view'
import { testable } from '@instructure/ui-testable'
Expand Down Expand Up @@ -104,7 +104,7 @@ class Pagination extends Component<PaginationProps> {
currentPage: 1,
siblingCount: 1,
boundaryCount: 1,
ellipsis: <li style={{all: 'unset'}}>...</li>,
Copy link
Collaborator Author

@matyasf matyasf Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this was a bug too, that its a li should be an internal part of the component. Also they need the `key' prop so React doesnt throw warnings

ellipsis: '…',
renderPageIndicator: (page: number) => page
}

Expand Down Expand Up @@ -317,7 +317,7 @@ class Pagination extends Component<PaginationProps> {
renderPagesInInterval = (from: number, to: number, currentPage: number) => {
if (to - from > 1000)
throw new Error('Pagination: too many pages (more than 1000)')
const pages = []
const pages: ReactElement[] = []
for (let i = from; i <= to; i++) {
pages.push(
<Pagination.Page
Expand All @@ -342,18 +342,26 @@ class Pagination extends Component<PaginationProps> {
boundaryCount,
variant
} = this.props
const pages: any = []
const pages: ReactNode[] = []
if (
totalPageNumber! <= 2 * boundaryCount! ||
totalPageNumber! <= 1 + siblingCount! + boundaryCount! ||
variant === 'full'
) {
return this.renderPagesInInterval(1, totalPageNumber!, currentPage!)
return (
<ul css={this.props.styles?.pageIndicatorList}>
{this.renderPagesInInterval(1, totalPageNumber!, currentPage!)}
</ul>
)
}

if (currentPage! > boundaryCount! + siblingCount! + 1) {
pages.push(this.renderPagesInInterval(1, boundaryCount!, currentPage!))
pages.push(ellipsis)
pages.push(
<li key="ellipsis1" style={{ all: 'unset' }}>
{ellipsis}
</li>
)
if (
currentPage! - siblingCount! >
totalPageNumber! - boundaryCount! + 1
Expand All @@ -365,7 +373,7 @@ class Pagination extends Component<PaginationProps> {
currentPage!
)
)
return pages
return <ul css={this.props.styles?.pageIndicatorList}>{pages}</ul>
}
pages.push(
this.renderPagesInInterval(
Expand All @@ -392,7 +400,11 @@ class Pagination extends Component<PaginationProps> {
currentPage!
)
)
pages.push(ellipsis)
pages.push(
<li key="ellipsis2" style={{ all: 'unset' }}>
{ellipsis}
</li>
)
pages.push(
this.renderPagesInInterval(
totalPageNumber! - boundaryCount! + 1,
Expand All @@ -409,7 +421,7 @@ class Pagination extends Component<PaginationProps> {
)
)
}
return (<ul css={this.props.styles?.pageIndicatorList}>{pages}</ul>)
return <ul css={this.props.styles?.pageIndicatorList}>{pages}</ul>
}

renderPages(currentPageIndex: number) {
Expand All @@ -434,22 +446,22 @@ class Pagination extends Component<PaginationProps> {

if (sliceStart - firstIndex > 1)
visiblePages.unshift(
<span key="first" aria-hidden="true">
&hellip;
</span>
<li style={{ all: 'unset' }} key="first" aria-hidden="true">
{this.props.ellipsis}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now the ellipsis prop works in the legacy code too

</li>
)
if (sliceStart - firstIndex > 0) visiblePages.unshift(firstPage)
if (lastIndex - sliceEnd + 1 > 1)
visiblePages.push(
<span key="last" aria-hidden="true">
&hellip;
</span>
<li style={{ all: 'unset' }} key="last" aria-hidden="true">
{this.props.ellipsis}
</li>
)
if (lastIndex - sliceEnd + 1 > 0) visiblePages.push(lastPage)
}

return (
<View display="inline-block">
<View display="inline-block" as="ul">
{this.transferDisabledPropToChildren(visiblePages)}
</View>
)
Expand Down
Loading