Skip to content

feat: Add builit-in formatter, and ability to pass custom children#17

Open
drake-nathan wants to merge 1 commit intothijssmudde:mainfrom
drake-nathan:custom-children-formatter
Open

feat: Add builit-in formatter, and ability to pass custom children#17
drake-nathan wants to merge 1 commit intothijssmudde:mainfrom
drake-nathan:custom-children-formatter

Conversation

@drake-nathan
Copy link
Copy Markdown

Description

My company, Paylocity, is using this lib to build our Pagination component. We have many instances where we need page numbers in the thousands, and we want those localized and formatted properly. This PR adds that functionality, as well as the ability to pass custom children to the Page Button.

Changes Made

  • Create a default formatter with Intl.NumberFormat() to format page numbers based on locale. Note that we do not want to explicitly define a locale, we want to inherit the user's runtime locale.
  • Add a formatter prop that can accept a function to replace the default formatter.
  • Change children to accept a ReactNode OR a function that takes the page numbers and returns a ReactNode
  • Put all this together in a getPageContent() function that uses the internal page and default formatter by default, but is overridden by children.
  • Add tests and Stories to verify changes.
  • Consolidate some types.

Please let me know what you think about these changes. I'm happy to adjust the implementation as needed.

@netlify
Copy link
Copy Markdown

netlify bot commented Feb 17, 2025

Deploy Preview for react-headless-pagination failed.

Name Link
🔨 Latest commit 03724cf
🔍 Latest deploy log https://app.netlify.com/sites/react-headless-pagination/deploys/67b3b21665a8bc000850c011


type ButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
as?: React.ReactElement;
children?: string | React.ReactNode;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Both of these props are already inherited from ButtonHTMLAttributes, and string is already included in ReactNode.

);

describe("Pagination", () => {
const mockFormat = jest.fn((number) => {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

We need to mock this, otherwise the test would fail in any locale outside the US.

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.

1 participant