Skip to content

Commit f8240b6

Browse files
committed
chore: forward ref
1 parent a542f1c commit f8240b6

4 files changed

Lines changed: 23 additions & 13 deletions

File tree

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import type { JsonNode } from '@zag-js/json-tree-utils'
2+
import { forwardRef } from 'react'
23
import { TreeView } from '../tree-view'
34

45
export interface JsonTreeViewRootProviderProps extends TreeView.RootProviderProps<JsonNode> {}
56

6-
export const JsonTreeViewRootProvider = (props: JsonTreeViewRootProviderProps) => {
7-
return <TreeView.RootProvider data-scope="json-tree-view" {...props} />
8-
}
7+
export const JsonTreeViewRootProvider = forwardRef<HTMLDivElement, JsonTreeViewRootProviderProps>((props, ref) => {
8+
return <TreeView.RootProvider data-scope="json-tree-view" {...props} ref={ref} />
9+
})
10+
11+
JsonTreeViewRootProvider.displayName = 'JsonTreeViewRootProvider'
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { type JsonNode, getRootNode, nodeToString, nodeToValue } from '@zag-js/json-tree-utils'
2-
import { useMemo } from 'react'
2+
import { forwardRef, useMemo } from 'react'
33
import { TreeView, createTreeCollection } from '../tree-view'
44

5-
export interface JsonTreeViewRootProps<T = unknown> extends Omit<TreeView.RootProps<JsonNode>, 'collection'> {
6-
data: T
5+
export interface JsonTreeViewRootProps extends Omit<TreeView.RootProps<JsonNode>, 'collection'> {
6+
data: unknown
77
}
88

9-
export function JsonTreeViewRoot<T = unknown>(props: JsonTreeViewRootProps<T>) {
9+
export const JsonTreeViewRoot = forwardRef<HTMLDivElement, JsonTreeViewRootProps>((props, ref) => {
1010
const { data, ...restProps } = props
1111

1212
const collection = useMemo(() => {
@@ -17,5 +17,9 @@ export function JsonTreeViewRoot<T = unknown>(props: JsonTreeViewRootProps<T>) {
1717
})
1818
}, [data])
1919

20-
return <TreeView.Root data-scope="json-tree-view" typeahead={false} collection={collection} {...restProps} />
21-
}
20+
return (
21+
<TreeView.Root data-scope="json-tree-view" collection={collection} {...restProps} ref={ref} typeahead={false} />
22+
)
23+
})
24+
25+
JsonTreeViewRoot.displayName = 'JsonTreeViewRoot'
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
import { forwardRef } from 'react'
12
import { createSplitProps } from '../../utils/create-split-props'
23
import { TreeView, useTreeViewContext } from '../tree-view'
34
import { JsonTreeViewNode, type JsonTreeViewNodeBaseProps } from './json-tree-view-node'
45

56
export interface JsonTreeViewTreeProps extends TreeView.TreeProps, JsonTreeViewNodeBaseProps {}
67

7-
export const JsonTreeViewTree = (props: JsonTreeViewTreeProps) => {
8+
export const JsonTreeViewTree = forwardRef<HTMLDivElement, JsonTreeViewTreeProps>((props, ref) => {
89
const [nodeProps, treeProps] = createSplitProps<JsonTreeViewNodeBaseProps>()(props, ['arrowIcon', 'showIndentGuide'])
910
const tree = useTreeViewContext()
1011
const children = tree.collection.getNodeChildren(tree.collection.rootNode)
1112
return (
12-
<TreeView.Tree data-scope="json-tree-view" {...treeProps}>
13+
<TreeView.Tree data-scope="json-tree-view" {...treeProps} ref={ref}>
1314
{children.map((child, index) => (
1415
<JsonTreeViewNode key={child.id} node={child} indexPath={[index]} {...nodeProps} />
1516
))}
1617
</TreeView.Tree>
1718
)
18-
}
19+
})
20+
21+
JsonTreeViewTree.displayName = 'JsonTreeViewTree'

packages/react/src/components/tree-view/use-tree-view.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Optional } from '../../types'
66
import type { TreeCollection, TreeNode } from '../collection'
77

88
export interface UseTreeViewProps<T extends TreeNode>
9-
extends Optional<Omit<treeView.Props, 'dir' | 'getRootNode' | 'collection'>, 'id'> {
9+
extends Optional<Omit<treeView.Props<T>, 'dir' | 'getRootNode' | 'collection'>, 'id'> {
1010
/**
1111
* The collection of tree nodes
1212
*/

0 commit comments

Comments
 (0)