-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TreeView] Extract some logic outside of the
useTreeView
hook (#13845)
- Loading branch information
1 parent
d132a06
commit 569413a
Showing
3 changed files
with
94 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/x-tree-view/src/internals/useTreeView/useTreeViewBuildContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import * as React from 'react'; | ||
import { TreeViewContextValue, TreeViewItemPluginsRunner } from '../TreeViewProvider'; | ||
import { | ||
ConvertSignaturesIntoPlugins, | ||
TreeItemWrapper, | ||
TreeRootWrapper, | ||
TreeViewAnyPluginSignature, | ||
TreeViewInstance, | ||
TreeViewPublicAPI, | ||
} from '../models'; | ||
import { TreeViewCorePluginSignatures } from '../corePlugins'; | ||
|
||
export const useTreeViewBuildContext = <TSignatures extends readonly TreeViewAnyPluginSignature[]>({ | ||
plugins, | ||
instance, | ||
publicAPI, | ||
rootRef, | ||
}: { | ||
plugins: ConvertSignaturesIntoPlugins<readonly [...TreeViewCorePluginSignatures, ...TSignatures]>; | ||
instance: TreeViewInstance<TSignatures>; | ||
publicAPI: TreeViewPublicAPI<TSignatures>; | ||
rootRef: React.RefObject<HTMLUListElement>; | ||
}): TreeViewContextValue<TSignatures> => { | ||
const runItemPlugins: TreeViewItemPluginsRunner = (itemPluginProps) => { | ||
let finalRootRef: React.RefCallback<HTMLLIElement> | null = null; | ||
let finalContentRef: React.RefCallback<HTMLElement> | null = null; | ||
|
||
plugins.forEach((plugin) => { | ||
if (!plugin.itemPlugin) { | ||
return; | ||
} | ||
|
||
const itemPluginResponse = plugin.itemPlugin({ | ||
props: itemPluginProps, | ||
rootRef: finalRootRef, | ||
contentRef: finalContentRef, | ||
}); | ||
if (itemPluginResponse?.rootRef) { | ||
finalRootRef = itemPluginResponse.rootRef; | ||
} | ||
if (itemPluginResponse?.contentRef) { | ||
finalContentRef = itemPluginResponse.contentRef; | ||
} | ||
}); | ||
|
||
return { | ||
contentRef: finalContentRef, | ||
rootRef: finalRootRef, | ||
}; | ||
}; | ||
|
||
const wrapItem: TreeItemWrapper<TSignatures> = ({ itemId, children }) => { | ||
let finalChildren: React.ReactNode = children; | ||
// The wrappers are reversed to ensure that the first wrapper is the outermost one. | ||
for (let i = plugins.length - 1; i >= 0; i -= 1) { | ||
const plugin = plugins[i]; | ||
if (plugin.wrapItem) { | ||
finalChildren = plugin.wrapItem({ itemId, children: finalChildren, instance }); | ||
} | ||
} | ||
|
||
return finalChildren; | ||
}; | ||
|
||
const wrapRoot: TreeRootWrapper<TSignatures> = ({ children }) => { | ||
let finalChildren: React.ReactNode = children; | ||
// The wrappers are reversed to ensure that the first wrapper is the outermost one. | ||
for (let i = plugins.length - 1; i >= 0; i -= 1) { | ||
const plugin = plugins[i]; | ||
if (plugin.wrapRoot) { | ||
finalChildren = plugin.wrapRoot({ children: finalChildren, instance }); | ||
} | ||
} | ||
|
||
return finalChildren; | ||
}; | ||
|
||
return { | ||
runItemPlugins, | ||
wrapItem, | ||
wrapRoot, | ||
instance, | ||
rootRef, | ||
publicAPI, | ||
} as TreeViewContextValue<TSignatures>; | ||
}; |
Empty file.