Skip to content

Commit

Permalink
feat(pagination): add forwardedRef for pagination container div (#10239)
Browse files Browse the repository at this point in the history
* feat(pagination): add forwardedRef for pagination container div

* feat(Pagination): forwardRef to pagination next, too

* fix(pagination): remove Element in forwardedRef proptype definition

* feat(pagination): update public api snapshot

* feat: pass forwardedRef to pagination

Co-authored-by: Alessandra Davila <[email protected]>
  • Loading branch information
kevinsperrine and Alessandra Davila authored Jan 31, 2022
1 parent e76c42a commit 430693f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
7 changes: 7 additions & 0 deletions packages/react/src/components/Pagination/Pagination-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,5 +437,12 @@ describe('Pagination', () => {
});
});
});

it('should render with ref', () => {
const ref = React.createRef();
mount(<Pagination pageSizes={[10]} totalItems={5} forwardedRef={ref} />);

expect(ref.current).toHaveClass(`${prefix}--pagination`);
});
});
});
3 changes: 2 additions & 1 deletion packages/react/src/components/Pagination/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ export default class Pagination extends Component {
onChange, // eslint-disable-line no-unused-vars
page: pageNumber, // eslint-disable-line no-unused-vars
size,
forwardedRef, // eslint-disable-line react/prop-types
...other
} = this.props;

Expand Down Expand Up @@ -312,7 +313,7 @@ export default class Pagination extends Component {
const pageSizes = mapPageSizesToObject(_pageSizes);

return (
<div className={classNames} {...other}>
<div className={classNames} ref={forwardedRef} {...other}>
<div className={`${prefix}--pagination__left`}>
<label
id={`${prefix}-pagination-select-${inputId}-count-label`}
Expand Down
57 changes: 30 additions & 27 deletions packages/react/src/components/Pagination/next/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,34 @@ function getPageSize(pageSizes, pageSize) {
return pageSizes[0].value;
}

function Pagination({
backwardText = 'Previous page',
className: customClassName,
disabled = false,
forwardText = 'Next page',
id,
isLastPage = false,
itemText = (min, max) => `${min}${max} items`,
itemRangeText = (min, max, total) => `${min}${max} of ${total} items`,
itemsPerPageText = 'Items per page:',
onChange,
pageNumberText: _pageNumberText = 'Page Number',
pageRangeText = (_current, total) =>
`of ${total} ${total === 1 ? 'page' : 'pages'}`,
page: controlledPage = 1,
pageInputDisabled,
pageSize: controlledPageSize,
pageSizeInputDisabled,
pageSizes: controlledPageSizes,
pageText = (page) => `page ${page}`,
pagesUnknown = false,
size,
totalItems,
...rest
}) {
const Pagination = React.forwardRef(function Pagination(
{
backwardText = 'Previous page',
className: customClassName,
disabled = false,
forwardText = 'Next page',
id,
isLastPage = false,
itemText = (min, max) => `${min}${max} items`,
itemRangeText = (min, max, total) => `${min}${max} of ${total} items`,
itemsPerPageText = 'Items per page:',
onChange,
pageNumberText: _pageNumberText = 'Page Number',
pageRangeText = (_current, total) =>
`of ${total} ${total === 1 ? 'page' : 'pages'}`,
page: controlledPage = 1,
pageInputDisabled,
pageSize: controlledPageSize,
pageSizeInputDisabled,
pageSizes: controlledPageSizes,
pageText = (page) => `page ${page}`,
pagesUnknown = false,
size,
totalItems,
...rest
},
ref
) {
const prefix = usePrefix();
const inputId = useFallbackId(id);
const [pageSizes, setPageSizes] = useState(() => {
Expand Down Expand Up @@ -189,7 +192,7 @@ function Pagination({
}

return (
<div className={className} {...rest}>
<div className={className} ref={ref} {...rest}>
<div className={`${prefix}--pagination__left`}>
<label
id={`${prefix}-pagination-select-${inputId}-count-label`}
Expand Down Expand Up @@ -268,7 +271,7 @@ function Pagination({
</div>
</div>
);
}
});

Pagination.propTypes = {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,11 @@ describe('Pagination', () => {
});
});
});

it('should render with ref', () => {
const ref = React.createRef();
mount(<Pagination pageSizes={[10]} totalItems={5} ref={ref} />);

expect(ref.current).toHaveClass(`${prefix}--pagination`);
});
});

0 comments on commit 430693f

Please sign in to comment.