File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import TreeHeader from './TreeHeader'
33import type { DisplayResult } from '../../../types'
44import { MatchList } from './MatchList'
55import { memo , useEffect , useRef } from 'react'
6+ import { useToggleResult } from './useListState'
67import * as stylex from '@stylexjs/stylex'
78
89const styles = stylex . create ( {
@@ -52,7 +53,7 @@ interface TreeItemProps {
5253}
5354
5455const TreeItem = ( { className, matches } : TreeItemProps ) => {
55- const [ isExpanded , toggleIsExpanded ] = useBoolean ( true )
56+ const [ isExpanded , toggleIsExpanded ] = useToggleResult ( matches [ 0 ] . file )
5657 const { isScrolled, ref } = useStickyShadow ( )
5758 const props = stylex . props ( styles . treeItem )
5859
Original file line number Diff line number Diff line change 1+ // maintains data list's UI state
2+ // e.g. toggle expand selected item
3+
4+ import { useCallback } from 'react'
5+ import { useBoolean } from 'react-use'
6+
7+ const collapseMap = new Map < string , boolean > ( )
8+
9+ export function useToggleResult ( filePath : string ) {
10+ const collapsedBefore = collapseMap . get ( filePath )
11+ const [ isExpanded , toggle ] = useBoolean ( ! collapsedBefore )
12+ const toggleIsExpanded = useCallback ( ( ) => {
13+ toggleResult ( filePath )
14+ toggle ( )
15+ } , [ filePath , toggle ] )
16+ return [ isExpanded , toggleIsExpanded ] as const
17+ }
18+
19+ function toggleResult ( filePath : string ) {
20+ if ( collapseMap . has ( filePath ) ) {
21+ collapseMap . delete ( filePath )
22+ } else {
23+ collapseMap . set ( filePath , true )
24+ }
25+ }
26+
27+ // let activeItem: DisplayResult | null = null
28+
29+ // function setActiveItem(match: DisplayResult) {
30+
31+ // }
You can’t perform that action at this time.
0 commit comments