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

Add a custom render function for page link text #334

Merged
merged 5 commits into from Mar 23, 2021
Merged

Add a custom render function for page link text #334

merged 5 commits into from Mar 23, 2021

Conversation

reediculous456
Copy link
Contributor

@ramzitm91
Copy link

Thanks for this amazing PR, I need it for my project right now..
Can you please add how to use this custom label render?
Thanks a lot!

@reediculous456
Copy link
Contributor Author

@ramzitm91
Thanks for this amazing PR, I need it for my project right now..
Can you please add how to use this custom label render?
Thanks a lot!

Sure! I feel like the test I wrote gives the best example if you switch over to the code tab of this PR, but here's another explanation for you.

The paginate component now has a new property called pageLabelRender that excepts a function, returning the text you want displayed on the paginate buttons. It defaults to (index) => index + 1 so that if you don't include it you'll get the behavior that was expected before this was added.
To change the label render, you just need to reference the original data array in the render function.

const data = [
  { name: 'Item 1' },
  { name: 'Item 2' },
  { name: 'Item 3' },
  { name: 'Item 4' },
  { name: 'Item 5' },
];

  <PaginationBoxView
    pageCount={data.length}
    pageLabelRender={(index) => data[index]?.name}
  />

@AdeleD
Copy link
Owner

AdeleD commented Mar 22, 2021

Hello,
Thank you for your PR!

Could we:

  • Default the pageLabelRender prop to: (page) => page
  • Keep {page} in the PageView component.
  • And, in your example, do:
<PaginationBoxView
     pageCount={data.length}
     pageLabelRender={(page) => {
        const pageIndex = page - 1;
        return data[pageIndex]?.name;
    }}
/>

Please? :)

@reediculous456
Copy link
Contributor Author

@AdeleD

I made the suggested changes except I left the {page} out of the PageView component because adding it there with the new page render function causes the page number to print twice by default and causes the page number to print with the custom render even if not desired

@AdeleD AdeleD merged commit ae8092e into AdeleD:master Mar 23, 2021
@AdeleD
Copy link
Owner

AdeleD commented Mar 23, 2021

@AdeleD

I made the suggested changes except I left the {page} out of the PageView component because adding it there with the new page render function causes the page number to print twice by default and causes the page number to print with the custom render even if not desired

You're right, thanks.

Thank you very much for your work on this PR. I've just renamed the prop pageLabelRender to pageLabelBuilder to be consistent with the other existing props: hrefBuilder and ariaLabelBuilder.
It has been released in version 7.1.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants