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

feat: number-field component #4575

Closed
wants to merge 9 commits into from

Conversation

thejasonxie
Copy link

@thejasonxie thejasonxie commented Aug 15, 2024

Needed a nice number input.

Saw that #1818 is becoming stale.

Took inspiration from shadcn's example in that stale PR to create one.

Used react-number-format to handle number formatting

Usage:

<NumberField>
  <NumberFieldInput thousandSeparator="," placeholder="Enter a number" />
  <NumberFieldIncrement />
  <NumberFieldDecrement />
</NumberField>

Screenshot 2024-08-15 at 5 36 33 PM

Copy link

vercel bot commented Aug 15, 2024

@thejasonxie is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

@thejasonxie
Copy link
Author

thejasonxie commented Aug 16, 2024

Sorry, don't merge yet because there is a bug. I also think using react-aria's useNumberField hook is better. Fill fix soon.

@thejasonxie
Copy link
Author

I changed my mind on using react-aria as the installation comes with much more than just the useNumberField hook.

I fixed the bug I was having with controlled state and used react-number-format's useNumericFormat hook instead for full customization.

The merge is good to go once reviewed.

Thanks!

@devongovett
Copy link

Given its popularity, it would be great if Shadcn UI could encourage the use of libraries that have good accessibility and internationalization support. React Aria's number field has been carefully designed to handle those concerns. See our blog post: https://react-spectrum.adobe.com/blog/how-we-internationalized-our-numberfield.html

Looking through the source code for react-number-format I see a lot of hard coded assumptions about the format and numbering system, e.g. decimals, negative signs, latin numerals, etc. It is also missing accessibility support to ensure the values are clearly announced to screen reader users.

Let me know if I can help with React Aria in any way. Would be amazing to collaborate on this.

@thejasonxie
Copy link
Author

thejasonxie commented Sep 6, 2024

@devongovett Yeah I agree, React Aria's number field component/hook would have been better to use in my opinion. The only thing that stopped me from using it is because the React Aria installation comes with a bunch of other React Aria's components when Shadcn is Radix based.

It would be nice if React Aria can provide a way to install components individually.

@devongovett
Copy link

devongovett commented Sep 6, 2024

hmm yeah, you can do that with the hooks - useNumberField comes from the @react-aria/numberfield package. For React Aria Components we decided not to do individual packages because it was too many dependencies to manage, and people often had problems upgrading them when they used many components. Putting them all in one package makes upgrading a lot easier. They should tree-shake well though, so even though the package includes many components you should only get the ones you use in your bundle. Was that your concern or something else?

@thejasonxie
Copy link
Author

Oh great, I didn't know @react-aria/numberfield existed, I will refactor and change to that when i have some time. Thanks @devongovett!

@thejasonxie
Copy link
Author

thejasonxie commented Feb 2, 2025

@devongovett was about to refactor but someone made a PR with react-aria's NumberField component. Probably better than my code if I were to refactor 😂, so i'll be closing this PR.

@thejasonxie thejasonxie closed this Feb 2, 2025
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.

2 participants