-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Noam Gaash <[email protected]>
- Loading branch information
Showing
6 changed files
with
331 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { TreeView } from '@mui/x-tree-view/TreeView'; | ||
import { TreeItem } from '@mui/x-tree-view/TreeItem'; | ||
import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; | ||
import ChevronRightIcon from '@mui/icons-material/ChevronRight'; | ||
|
||
interface BaseTreeNode { | ||
id: string; | ||
name: string; | ||
} | ||
|
||
// Generic TreeNode interface with additional properties | ||
interface TreeNode<T extends BaseTreeNode> extends BaseTreeNode { | ||
children?: TreeNode<T>[]; | ||
// You can now include additional properties from T | ||
} | ||
|
||
// Generic CustomTreeViewProps interface | ||
interface CustomTreeViewProps<T> { | ||
data: T; | ||
name: string; | ||
id: string; | ||
} | ||
|
||
// A utility function to transform any object into a TreeNode structure | ||
const objectToTreeNode = ( | ||
obj: any, | ||
key: string = 'root', | ||
nodeId: string = '0' | ||
): TreeNode<BaseTreeNode> => { | ||
const isPrimitive = (value: any) => value !== Object(value) || value === null; | ||
|
||
const children = Object.keys(obj).map((k, index) => { | ||
const value = obj[k]; | ||
if (isPrimitive(value)) { | ||
// Handle primitive types directly | ||
return { | ||
id: `${nodeId}-${index}`, | ||
name: `${k}: ${value}`, | ||
}; | ||
} else if (Array.isArray(value)) { | ||
// If it's an array, create a node that lists all elements | ||
return { | ||
id: `${nodeId}-${index}`, | ||
name: k, | ||
children: value.map((item, itemIndex) => { | ||
// Handle primitive items in the array directly | ||
if (isPrimitive(item)) { | ||
return { | ||
id: `${nodeId}-${index}-${itemIndex}`, | ||
name: `${item}`, | ||
}; | ||
} | ||
// Recursively handle objects in the array | ||
return objectToTreeNode(item, k, `${nodeId}-${index}-${itemIndex}`); | ||
}), | ||
}; | ||
} else { | ||
// Recursively handle objects | ||
return objectToTreeNode(value, k, `${nodeId}-${index}`); | ||
} | ||
}); | ||
|
||
return { | ||
id: nodeId, | ||
name: obj.name || key, | ||
children: children.length ? children : undefined, | ||
}; | ||
}; | ||
|
||
// Render tree function utilizing the TreeNode interface | ||
const renderTree = <T extends BaseTreeNode>(nodes: TreeNode<T>): JSX.Element => ( | ||
<TreeItem key={nodes.id} nodeId={nodes.id} label={nodes.name}> | ||
{nodes.children?.map((node) => renderTree(node))} | ||
</TreeItem> | ||
); | ||
|
||
// CustomTreeView component using TypeScript | ||
const CustomTreeView = <T,>({ data, name, id }: CustomTreeViewProps<T>) => { | ||
const dataAsTreeNode = objectToTreeNode({ id, name, children: [data] }); | ||
return ( | ||
<TreeView | ||
defaultCollapseIcon={<ExpandMoreIcon />} | ||
defaultExpandIcon={<ChevronRightIcon />} | ||
defaultEndIcon={<div style={{ width: 24 }} />} | ||
> | ||
{renderTree(dataAsTreeNode)} | ||
</TreeView> | ||
); | ||
}; | ||
|
||
export default CustomTreeView; |
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,50 @@ | ||
// CustomTreeView.stories.tsx | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import CustomTreeView from './CustomTreeView'; | ||
import '../../shared/shared.css'; // Assuming you have some shared styles | ||
|
||
// Define a base type for your tree nodes, considering the object structure you provided earlier | ||
interface CustomTreeNode { | ||
id: string; | ||
name: string; | ||
children?: CustomTreeNode[]; | ||
} | ||
|
||
// Example data for the story | ||
const exampleData = { | ||
id: 'root', | ||
name: 'Root Node', | ||
children: [ | ||
{ | ||
id: '1', | ||
name: 'Child Node 1', | ||
children: [ | ||
{ id: '1-1', name: 'Grandchild Node 1-1' }, | ||
{ id: '1-2', name: 'Grandchild Node 1-2' }, | ||
], | ||
}, | ||
{ | ||
id: '2', | ||
name: 'Child Node 2', | ||
}, | ||
], | ||
}; | ||
|
||
const meta: Meta<typeof CustomTreeView> = { | ||
title: 'Components/CustomTreeView', | ||
component: CustomTreeView, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
tags: ['tree', 'view', 'react', 'mui'], // Adjust tags as needed | ||
} satisfies Meta<typeof CustomTreeView>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
data: exampleData, // Using the example data for the default story | ||
}, | ||
}; |
Oops, something went wrong.