import * as React from 'react' ; import { TreeChangeActionsContextProps , TreeItemIndex , TreeRef } from '../types' ; import { PropsWithChildren } from 'react' ; import { useDragAndDrop } from '../controlledEnvironment/DragAndDropProvider' ; import { useTreeEnvironment } from '../controlledEnvironment/ControlledTreeEnvironment' ; import { useCreatedTreeRef } from './useCreatedTreeRef' ; import { useTree } from '../tree/Tree' ; import { useEnvironmentActions } from '../environmentActions/EnvironmentActionsProvider' ; const EnvironmentActionsContext = React . createContext < TreeChangeActionsContextProps > ( null as any ) ; export const useTreeActions = ( ) => React . useContext ( EnvironmentActionsContext ) ; export const TreeActionsProvider = React . forwardRef < TreeRef , PropsWithChildren < Record < string , unknown > > > ( ( props , ref ) => { useTreeEnvironment ( ) ; const tree = useTree ( ) ; useDragAndDrop ( ) ; const envActions = useEnvironmentActions ( ) ; const actions = { abortRenamingItem ( ) { tree . setRenamingItem ( null ) ; } , abortSearch ( ) { tree . setSearch ( null ) ; } , collapseItem ( itemId ) { envActions . collapseItem ( itemId , tree . treeId ) ; } , completeRenamingItem ( ) { } , expandItem ( itemId ) { envActions . expandItem ( itemId , tree . treeId ) ; } , focusItem ( itemId ) { envActions . focusItem ( itemId , tree . treeId ) ; } , focusTree ( autoFocus = true ) { envActions . focusTree ( tree . treeId , autoFocus ) ; } , invokePrimaryAction ( itemId ) { envActions . invokePrimaryAction ( itemId , tree . treeId ) ; } , moveFocusDown ( ) { envActions . moveFocusDown ( tree . treeId ) ; } , moveFocusUp ( ) { envActions . moveFocusUp ( tree . treeId ) ; } , renameItem ( itemId , name ) { envActions . renameItem ( itemId , name , tree . treeId ) ; } , selectItems ( itemsIds ) { envActions . selectItems ( itemsIds , tree . treeId ) ; } , setSearch ( search ) { tree . setSearch ( search ) ; } , startRenamingItem ( itemId ) { tree . setRenamingItem ( itemId ) ; } , stopRenamingItem ( ) { tree . setRenamingItem ( null ) ; } , toggleItemExpandedState ( itemId ) { envActions . toggleItemExpandedState ( itemId , tree . treeId ) ; } , toggleItemSelectStatus ( itemId ) { envActions . toggleItemSelectStatus ( itemId , tree . treeId ) ; } } ; useCreatedTreeRef ( ref , actions ) ; return < EnvironmentActionsContext . Provider value = { actions } > { props . children } < /EnvironmentActionsContext.Provider>; } ) ;