-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathdevTool.tsx
More file actions
60 lines (53 loc) · 1.48 KB
/
devTool.tsx
File metadata and controls
60 lines (53 loc) · 1.48 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
import { createStore, StateMachineProvider } from 'little-state-machine';
import * as React from 'react';
import { Control, FieldValues, useFormContext } from 'react-hook-form';
import { v4 as generateUUID } from 'uuid';
import { DevToolUI, DevtoolUIProps } from './devToolUI';
import { useExportControlToExtension } from './extension/useExportControlToExtension';
import type { PLACEMENT } from './position';
if (typeof window !== 'undefined') {
createStore(
{
visible: false,
isCollapse: false,
filterName: '',
},
{
name: '__REACT_HOOK_FORM_DEVTOOLS__',
middleWares: [],
storageType: window.localStorage,
},
);
}
export const DevTool = <
TFieldValues extends FieldValues,
TTransformedValues = FieldValues,
>(
props?: {
id?: string;
control?: Control<TFieldValues, any, TTransformedValues>;
} & Pick<DevtoolUIProps, 'placement' | 'styles'>,
) => {
const methods = useFormContext();
const uuid = React.useRef('');
React.useEffect(() => {
uuid.current = generateUUID();
}, []);
const { isExtensionEnabled } = useExportControlToExtension({
id: props?.id ?? uuid.current,
control: props?.control ?? methods.control,
});
if (isExtensionEnabled) {
return null;
}
return (
<StateMachineProvider>
<DevToolUI
control={props?.control ?? methods.control}
placement={props?.placement}
styles={props?.styles}
/>
</StateMachineProvider>
);
};
export type { PLACEMENT };