Skip to content

Commit

Permalink
feat: useControls returns a get function (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoHamuy authored Aug 31, 2022
1 parent ad924ec commit 466f307
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-moose-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'leva': minor
---

feat: `useControls` returns a `get` function
9 changes: 6 additions & 3 deletions packages/leva/src/useControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ type SchemaOrFn<S extends Schema = Schema> = S | (() => S)
type FunctionReturnType<S extends Schema> = [
SchemaToValues<S>,
(value: {
[K in keyof Partial<SchemaToValues<S, true>>]: any
}) => void
[K in keyof Partial<SchemaToValues<S, true>>]: SchemaToValues<S, true>[K]
}) => void,
<T extends keyof SchemaToValues<S, true>>(path: T) => SchemaToValues<S, true>[T]
]

type ReturnType<F extends SchemaOrFn> = F extends SchemaOrFn<infer S>
Expand Down Expand Up @@ -179,6 +180,8 @@ export function useControls<S extends Schema, F extends SchemaOrFn<S> | string,
[store, mappedPaths]
)

const get = useCallback((path: string) => store.get(mappedPaths[path].path), [store, mappedPaths])

useEffect(() => {
// We initialize the store with the initialData in useEffect.
// Note that doing this while rendering (ie in useMemo) would make
Expand Down Expand Up @@ -225,6 +228,6 @@ export function useControls<S extends Schema, F extends SchemaOrFn<S> | string,
return () => unsubscriptions.forEach((unsub) => unsub())
}, [onEditStartPaths, onEditEndPaths, store])

if (schemaIsFunction) return [values, set] as any
if (schemaIsFunction) return [values, set, get] as any
return values as any
}
52 changes: 48 additions & 4 deletions packages/leva/stories/controlled-inputs.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react'
import { ComponentPropsWithoutRef, forwardRef, useRef } from 'react'
import { useDrag } from '@use-gesture/react'
import { Meta, Story } from '@storybook/react'
import { useDrag } from '@use-gesture/react'
import React, { ComponentPropsWithoutRef, forwardRef, useRef } from 'react'
import Reset from './components/decorator-reset'

import { useControls } from '../src'
import { folder, useControls } from '../src'

export default {
title: 'Misc/Controlled inputs',
Expand All @@ -31,6 +30,51 @@ export const ExternalUpdatesWithSet: Story = () => {
)
}

export const ExternalUpdatesWithGetAndSet: Story = () => {
const [{ counter }, set, get] = useControls(() => ({ counter: 0 }))
const [{ counter: counter2, counterB }, set2, get2] = useControls('folder', () => ({
counter: 0,
folder2: folder({
counterB: 0,
}),
}))

const onClick = () => {
set({ counter: get('counter') + 1 })
}

const onClick2 = () => {
set2({ counter: get2('counter') + 1 })
}

const onClick3 = () => {
set2({ counterB: get2('counterB') + 1 })
}

return (
<form style={formStyles}>
<label>
counter: {counter}{' '}
<button type="button" onClick={onClick}>
➕ inc
</button>
</label>
<label>
folder.counter: {counter2}{' '}
<button type="button" onClick={onClick2}>
➕ inc
</button>
</label>
<label>
folder.folder2.counterB: {counterB}{' '}
<button type="button" onClick={onClick3}>
➕ inc
</button>
</label>
</form>
)
}

const Circle = forwardRef<HTMLDivElement, ComponentPropsWithoutRef<'div'>>((props, ref) => {
return (
<div
Expand Down

1 comment on commit 466f307

@vercel
Copy link

@vercel vercel bot commented on 466f307 Aug 31, 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-pmndrs.vercel.app
leva.vercel.app
leva.pmnd.rs
leva-git-main-pmndrs.vercel.app

Please sign in to comment.