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

Supporting % (and other non-alphanumeric) suffix in number #349

Closed
Corei13 opened this issue Jun 26, 2022 · 1 comment · Fixed by #350
Closed

Supporting % (and other non-alphanumeric) suffix in number #349

Corei13 opened this issue Jun 26, 2022 · 1 comment · Fixed by #350

Comments

@Corei13
Copy link

Corei13 commented Jun 26, 2022

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

I have some value in the format x% that I want to keep as string, but leva was parsing them as numbers without any suffix.

Here is the diff that solved my problem:

diff --git a/node_modules/leva/src/components/Number/number-plugin.ts b/node_modules/leva/src/components/Number/number-plugin.ts
index ce107ad..415d611 100644
--- a/node_modules/leva/src/components/Number/number-plugin.ts
+++ b/node_modules/leva/src/components/Number/number-plugin.ts
@@ -21,7 +21,7 @@ export const normalize = ({ value, ...settings }: NumberInput) => {
   const _value = clamp(parseFloat(value as string), min, max)
   let suffix
   if (!Number.isFinite(value)) {
-    const match = String(value).match(/[A-Z]+/i)
+    const match = String(value).match(/[A-Z%]+/i)
     if (match) suffix = match[0]
   }
 

This issue body was partially generated by patch-package.

@dbismut
Copy link
Collaborator

dbismut commented Jun 27, 2022

@Corei13 thanks for spotting this. Should be fixed in latest leva release (supporting all kinds of units).

  let _value = parseFloat(value as string)
  const suffix = typeof value === 'string' ? value.substring(('' + _value).length) : undefined
  _value = clamp(_value, min, max)

@dbismut dbismut closed this as completed Jun 27, 2022
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 a pull request may close this issue.

2 participants