Skip to content

Commit

Permalink
Fix: number parsing in string (#356)
Browse files Browse the repository at this point in the history
We consider a string as a number when its suffix is less than 4 characters long.
  • Loading branch information
dbismut authored Jul 7, 2022
1 parent cc5710e commit 29aece8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nice-years-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'leva': patch
---

Fix a an issue where a string starting with a number would be considered as a number with a potentially long unit. Now we consider a string as a number when its suffix is less than 4 characters long.
12 changes: 11 additions & 1 deletion packages/leva/src/components/Number/number-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { getStep, clamp } from '../../utils'
import type { InternalNumberSettings, NumberInput } from './number-types'

export const schema = (o: any) => typeof o === 'number' || (typeof o === 'string' && !isNaN(parseFloat(o)))
export const schema = (v: any) => {
if (typeof v === 'number') return true
// we consider a string as a number if it starts with a number and it's suffix is less than 4 characters
if (typeof v === 'string') {
const _v = parseFloat(v)
if (isNaN(_v)) return false
const suffix = v.substring(('' + _v).length).trim()
return suffix.length < 4
}
return false
}

export const sanitize = (v: any, { min = -Infinity, max = Infinity, suffix }: InternalNumberSettings) => {
const _v = parseFloat(v as string)
Expand Down

1 comment on commit 29aece8

@vercel
Copy link

@vercel vercel bot commented on 29aece8 Jul 7, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

leva – ./

leva.vercel.app
leva-pmndrs.vercel.app
leva.pmnd.rs
leva-git-main-pmndrs.vercel.app

Please sign in to comment.