1
1
import { ToggleArrowRightIcon } from '@sanity/icons'
2
2
import { ThemeFontWeightKey } from '@sanity/ui/theme'
3
- import { memo , useCallback , useEffect , useId , useMemo , useRef } from 'react'
3
+ import { memo , useCallback , useEffect , useId , useMemo , useRef , useState } from 'react'
4
4
import { styled } from 'styled-components'
5
5
import { Box , BoxProps , Flex , Text } from '../../primitives'
6
6
import {
@@ -64,18 +64,21 @@ export const TreeItem = memo(function TreeItem(
64
64
weight,
65
65
...restProps
66
66
} = props
67
- const rootRef = useRef < HTMLLIElement | null > ( null )
67
+ const [ rootElement , setRootElement ] = useState < HTMLLIElement | null > ( null )
68
68
const treeitemRef = useRef < HTMLDivElement | null > ( null )
69
69
const tree = useTree ( )
70
70
const { path, registerItem, setExpanded, setFocusedElement} = tree
71
71
const _id = useId ( )
72
72
const id = idProp || _id
73
- const itemPath = useMemo ( ( ) => path . concat ( [ id || '' ] ) , [ id , path ] )
74
- const itemKey = itemPath . join ( '/' )
73
+ const [ itemPath , itemKey ] = useMemo ( ( ) => {
74
+ const result = path . concat ( [ id || '' ] )
75
+
76
+ return [ result , result . join ( '/' ) ]
77
+ } , [ id , path ] )
75
78
const itemState = tree . state [ itemKey ]
76
- const focused = tree . focusedElement === rootRef . current
79
+ const focused = tree . focusedElement === rootElement
77
80
const expanded = itemState ?. expanded === undefined ? expandedProp : itemState ?. expanded || false
78
- const tabIndex = tree . focusedElement && tree . focusedElement === rootRef . current ? 0 : - 1
81
+ const tabIndex = tree . focusedElement && tree . focusedElement === rootElement ? 0 : - 1
79
82
const contextValue = useMemo (
80
83
( ) => ( { ...tree , level : tree . level + 1 , path : itemPath } ) ,
81
84
[ itemPath , tree ] ,
@@ -94,28 +97,28 @@ export const TreeItem = memo(function TreeItem(
94
97
) {
95
98
event . stopPropagation ( )
96
99
setExpanded ( itemKey , ! expanded )
97
- setFocusedElement ( rootRef . current )
100
+ setFocusedElement ( rootElement )
98
101
}
99
102
} ,
100
- [ expanded , itemKey , onClick , setExpanded , setFocusedElement ] ,
103
+ [ expanded , itemKey , onClick , rootElement , setExpanded , setFocusedElement ] ,
101
104
)
102
105
103
106
const handleKeyDown = useCallback (
104
107
( event : React . KeyboardEvent < HTMLElement > ) => {
105
108
if ( focused && event . key === 'Enter' ) {
106
- const el = treeitemRef . current || rootRef . current
109
+ const el = treeitemRef . current || rootElement
107
110
108
111
el ?. click ( )
109
112
}
110
113
} ,
111
- [ focused ] ,
114
+ [ focused , rootElement ] ,
112
115
)
113
116
114
117
useEffect ( ( ) => {
115
- if ( ! rootRef . current ) return
118
+ if ( ! rootElement ) return
116
119
117
- return registerItem ( rootRef . current , itemPath . join ( '/' ) , expanded , selected )
118
- } , [ expanded , itemPath , registerItem , selected ] )
120
+ return registerItem ( rootElement , itemPath . join ( '/' ) , expanded , selected )
121
+ } , [ expanded , itemPath , registerItem , rootElement , selected ] )
119
122
120
123
const content = (
121
124
< Flex padding = { padding } >
@@ -154,7 +157,7 @@ export const TreeItem = memo(function TreeItem(
154
157
data-ui = "TreeItem"
155
158
{ ...restProps }
156
159
onClick = { handleClick }
157
- ref = { rootRef }
160
+ ref = { setRootElement }
158
161
role = "none"
159
162
>
160
163
< TreeItemBox
@@ -187,7 +190,7 @@ export const TreeItem = memo(function TreeItem(
187
190
aria-expanded = { expanded }
188
191
onClick = { handleClick }
189
192
onKeyDown = { handleKeyDown }
190
- ref = { rootRef }
193
+ ref = { setRootElement }
191
194
role = "treeitem"
192
195
tabIndex = { tabIndex }
193
196
>
0 commit comments