-
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.
feat(map): New alternative PR in light of CiCD issues (#566)
- Loading branch information
Showing
5 changed files
with
286 additions
and
17 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,89 @@ | ||
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 | ||
} | ||
|
||
const isPrimitive = (value: unknown) => value !== Object(value) || value === null | ||
// A utility function to transform any object into a TreeNode structure | ||
const objectToTreeNode = <T extends Record<string, unknown>>( | ||
obj: T, | ||
key: string = 'root', | ||
nodeId: string = '0', | ||
): TreeNode<BaseTreeNode> => { | ||
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 as Record<string, unknown>, k, `${nodeId}-${index}`) | ||
} | ||
}) | ||
|
||
return { | ||
id: nodeId, | ||
name: (obj.name || key) as string, | ||
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,43 @@ | ||
// CustomTreeView.stories.tsx | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
import CustomTreeView from './CustomTreeView' | ||
import '../../shared/shared.css' // Assuming you have some shared styles | ||
|
||
// 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 | ||
}, | ||
} |
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