-
-
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] Rework how items are being rendered in Rich Tree View comp…
…onents (#14749)
- Loading branch information
1 parent
6ce41a1
commit 526ef4c
Showing
6 changed files
with
114 additions
and
121 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
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
91 changes: 91 additions & 0 deletions
91
packages/x-tree-view/src/internals/components/RichTreeViewItems.tsx
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,91 @@ | ||
import * as React from 'react'; | ||
import useSlotProps from '@mui/utils/useSlotProps'; | ||
import { SlotComponentProps } from '@mui/utils'; | ||
|
||
import { TreeItem, TreeItemProps } from '../../TreeItem'; | ||
import { TreeItem2Props } from '../../TreeItem2'; | ||
import { TreeViewItemId } from '../../models'; | ||
import { TreeViewItemToRenderProps } from '../plugins/useTreeViewItems'; | ||
|
||
interface RichTreeViewItemsOwnerState { | ||
itemId: TreeViewItemId; | ||
label: string; | ||
} | ||
|
||
export interface RichTreeViewItemsSlots { | ||
/** | ||
* Custom component for the item. | ||
* @default TreeItem. | ||
*/ | ||
item?: React.JSXElementConstructor<TreeItemProps> | React.JSXElementConstructor<TreeItem2Props>; | ||
} | ||
|
||
export interface RichTreeViewItemsSlotProps { | ||
item?: SlotComponentProps<typeof TreeItem, {}, RichTreeViewItemsOwnerState>; | ||
} | ||
|
||
export interface RichTreeViewItemsProps { | ||
itemsToRender: TreeViewItemToRenderProps[]; | ||
/** | ||
* Overridable component slots. | ||
* @default {} | ||
*/ | ||
slots?: RichTreeViewItemsSlots; | ||
/** | ||
* The props used for each component slot. | ||
* @default {} | ||
*/ | ||
slotProps?: RichTreeViewItemsSlotProps; | ||
} | ||
|
||
function WrappedTreeItem({ | ||
slots, | ||
slotProps, | ||
label, | ||
id, | ||
itemId, | ||
itemsToRender, | ||
}: Pick<RichTreeViewItemsProps, 'slots' | 'slotProps'> & | ||
Pick<TreeItemProps, 'id' | 'itemId' | 'children'> & { | ||
label: string; | ||
isContentHidden?: boolean; | ||
itemsToRender: TreeViewItemToRenderProps[] | undefined; | ||
}) { | ||
const Item = slots?.item ?? TreeItem; | ||
const { ownerState, ...itemProps } = useSlotProps({ | ||
elementType: Item, | ||
externalSlotProps: slotProps?.item, | ||
additionalProps: { itemId, id, label }, | ||
ownerState: { itemId, label }, | ||
}); | ||
|
||
const children = React.useMemo( | ||
() => | ||
itemsToRender ? ( | ||
<RichTreeViewItems itemsToRender={itemsToRender} slots={slots} slotProps={slotProps} /> | ||
) : null, | ||
[itemsToRender, slots, slotProps], | ||
); | ||
|
||
return <Item {...itemProps}>{children}</Item>; | ||
} | ||
|
||
export function RichTreeViewItems(props: RichTreeViewItemsProps) { | ||
const { itemsToRender, slots, slotProps } = props; | ||
|
||
return ( | ||
<React.Fragment> | ||
{itemsToRender.map((item) => ( | ||
<WrappedTreeItem | ||
slots={slots} | ||
slotProps={slotProps} | ||
key={item.itemId} | ||
label={item.label} | ||
id={item.id} | ||
itemId={item.itemId} | ||
itemsToRender={item.children} | ||
/> | ||
))} | ||
</React.Fragment> | ||
); | ||
} |
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
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
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