-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathusePaneInput.ts
More file actions
100 lines (83 loc) · 3.14 KB
/
Copy pathusePaneInput.ts
File metadata and controls
100 lines (83 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { InputBindingApi, BindingParams, TpChangeEvent } from '@tweakpane/core'
import {
RefObject,
useCallback,
useLayoutEffect,
useRef,
useState,
} from 'react'
import { FolderInstance } from './usePaneFolder'
type InputRef<T> = RefObject<InputBindingApi<unknown, T>>
/**
* Does not return the value and doesn't trigger an update because onChange is specified
*/
export function usePaneInput<T extends Object, K extends keyof T>(
ref: RefObject<FolderInstance<T>>,
key: K,
BindingParams: BindingParams | undefined,
onChange: (event: TpChangeEvent<T[K]>) => void
): [never, (value: T[K]) => void, InputRef<T[K]>]
// Skips BindingParams
/** Does not return the value and doesn't trigger an update because onChange is specified */
export function usePaneInput<T extends Object, K extends keyof T>(
paneRef: RefObject<FolderInstance<T>>,
key: K,
onChange: (event: TpChangeEvent<T[K]>) => void
): [never, (value: T[K]) => void, InputRef<T[K]>]
/**
* Returns the value and triggers an update
*/
export function usePaneInput<T extends Object, K extends keyof T>(
paneRef: RefObject<FolderInstance<T>>,
key: K,
BindingParams?: BindingParams | undefined,
onChange?: undefined
): [T[K], (value: T[K]) => void, InputRef<T[K]>]
export function usePaneInput<T extends Object, K extends keyof T>(
paneRef: RefObject<FolderInstance<T>>,
key: K,
BindingParams?: BindingParams | undefined,
onChange?: undefined
): [T[K], (value: T[K]) => void, InputRef<T[K]>]
export function usePaneInput<T extends Object, K extends keyof T>(
parentRef: RefObject<FolderInstance<T>>,
key: K,
inputParamsArg:
| BindingParams
| ((event: TpChangeEvent<T[K]>) => void)
| undefined = {},
onChangeArg: ((event: TpChangeEvent<T[K]>) => void) | undefined = undefined
) {
const BindingParams = typeof inputParamsArg === 'function' ? {} : inputParamsArg
const onChange =
typeof inputParamsArg === 'function' ? inputParamsArg : onChangeArg
const [value, set] = useState(parentRef.current!.params[key])
const inputRef = useRef<InputBindingApi<unknown, T[K]>>(null!)
const callbackRef = useRef(onChange)
callbackRef.current = onChange
const setValue = useCallback((value: T[K]) => {
// inputRef.current.controller_.binding.target.write(value)
inputRef.current.controller.value.binding.write(value)
inputRef.current.refresh()
}, [])
if (inputRef.current) {
inputRef.current.hidden = Boolean(BindingParams.hidden)
inputRef.current.disabled = Boolean(BindingParams.disabled)
}
useLayoutEffect(() => {
const pane = parentRef.current?.instance
if (pane == null) return
const handler: (event: TpChangeEvent<T[K]>) => void = onChange
? (event) => callbackRef.current!(event)
: (event) => set(event.value)
const input = pane
.addBinding(parentRef.current!.params, key, BindingParams)
.on('change', handler)
inputRef.current = input as InputBindingApi<unknown, T[K]>
// inputRef.current.controller.importState.arguments = input
return () => {
if (input.element) input.dispose()
}
}, [key, onChange])
return [onChange == null ? value : undefined, setValue, inputRef]
}