Skip to content

Commit

Permalink
Fix set interval (#369)
Browse files Browse the repository at this point in the history
* fix: allow interval to be set externally with [number, number]

* ci
  • Loading branch information
dbismut authored Aug 3, 2022
1 parent 2c63c34 commit 81acf37
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-kiwis-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'leva': patch
---

fix: allow interval to be set externally with [number, number]
6 changes: 4 additions & 2 deletions packages/leva/src/components/Interval/interval-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ export const schema = (o: any, s: any) =>
export const format = (v: Interval) => ({ min: v[0], max: v[1] })

export const sanitize = (
value: InternalInterval,
value: InternalInterval | Interval,
{ bounds: [MIN, MAX] }: InternalIntervalSettings,
prevValue: any
): Interval => {
// value can be passed as an array externally
const _value: InternalInterval = Array.isArray(value) ? format(value as Interval) : value
const _newValue = { min: prevValue[0], max: prevValue[1] }
const { min, max } = { ..._newValue, ...value }
const { min, max } = { ..._newValue, ..._value }
return [clamp(Number(min), MIN, Math.max(MIN, max)), clamp(Number(max), Math.min(MAX, min), MAX)]
}

Expand Down
7 changes: 6 additions & 1 deletion packages/leva/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,12 @@ export const Store = function (this: StoreType) {
try {
//@ts-expect-error (we always update inputs with a value)
updateInput(data[path], value, undefined, undefined, fromPanel)
} catch {}
} catch (e) {
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line no-console
console.warn(`[This message will only show in development]: \`set\` for path ${path} has failed.`, e)
}
}
})
return { data }
})
Expand Down
8 changes: 4 additions & 4 deletions packages/leva/src/utils/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ type SanitizeProps = {
settings: object | undefined
}

type ValueErrorType = { type: string; message: string; previousValue: any; error?: Error }
type ValueErrorType = { type: string; message: string; previousValue: any; error?: unknown }

const ValueError = function (this: ValueErrorType, message: string, value: any, error?: Error) {
const ValueError = function (this: ValueErrorType, message: string, value: any, error?: unknown) {
this.type = 'LEVA_ERROR'
this.message = 'LEVA: ' + message
this.previousValue = value
this.error = error
} as unknown as { new (message: string, value: any, error?: Error): ValueErrorType }
} as unknown as { new (message: string, value: any, error?: unknown): ValueErrorType }

export function sanitizeValue({ type, value, settings }: SanitizeProps, newValue: any, path: string, store: StoreType) {
// sanitizeValue can accept a new value in the form of fn(oldValue). This
Expand All @@ -157,7 +157,7 @@ export function sanitizeValue({ type, value, settings }: SanitizeProps, newValue
let sanitizedNewValue
try {
sanitizedNewValue = sanitize(type, _newValue, settings, value, path, store)
} catch (e: any) {
} catch (e) {
throw new ValueError(`The value \`${newValue}\` did not result in a correct value.`, value, e)
}
if (dequal(sanitizedNewValue, value)) {
Expand Down

1 comment on commit 81acf37

@vercel
Copy link

@vercel vercel bot commented on 81acf37 Aug 3, 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-git-main-pmndrs.vercel.app
leva.pmnd.rs

Please sign in to comment.