Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 210 additions & 45 deletions example/components/deep-tree.tsx
Original file line number Diff line number Diff line change
@@ -1,58 +1,223 @@
import React from 'react';
import React, { useState } from 'react';
import DeepTree, { DataNode, treeUtils } from '../../index';

interface DataNode {
readonly content: any;
readonly children: DataNode[];
}
export type { DataNode };
// Demo data showcasing the enhanced features
const demoData: DataNode[] = [
{
id: 'docs',
content: 'Documentation',
expanded: true,
children: [
{
id: 'api-docs',
content: 'API Documentation',
children: [
{
id: 'auth-api',
content: 'Authentication API',
children: []

Check notice on line 18 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L18

Missing trailing comma. (trailing-comma)
},
{
id: 'user-api',
content: 'User Management API',
children: []

Check notice on line 23 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L23

Missing trailing comma. (trailing-comma)
}

Check notice on line 24 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L24

Missing trailing comma. (trailing-comma)
]

Check notice on line 25 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L25

Missing trailing comma. (trailing-comma)
},
{
id: 'guides',
content: 'User Guides',
children: [
{
id: 'quick-start',
content: 'Quick Start Guide',
children: []

Check notice on line 34 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L34

Missing trailing comma. (trailing-comma)
},
{
id: 'advanced',
content: 'Advanced Configuration',
children: []

Check notice on line 39 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L39

Missing trailing comma. (trailing-comma)
}

Check notice on line 40 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L40

Missing trailing comma. (trailing-comma)
]

Check notice on line 41 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L41

Missing trailing comma. (trailing-comma)
}

Check notice on line 42 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L42

Missing trailing comma. (trailing-comma)
]

Check notice on line 43 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L43

Missing trailing comma. (trailing-comma)
},
{
id: 'src',
content: 'Source Code',
expanded: false,
children: [
{
id: 'components',
content: 'React Components',
children: [
{
id: 'tree-component',
content: 'DeepTree Component',
children: []

Check notice on line 57 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L57

Missing trailing comma. (trailing-comma)
},
{
id: 'search-component',
content: 'SearchBox Component',
children: []

Check notice on line 62 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L62

Missing trailing comma. (trailing-comma)
}

Check notice on line 63 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L63

Missing trailing comma. (trailing-comma)
]

Check notice on line 64 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L64

Missing trailing comma. (trailing-comma)
},
{
id: 'utils',
content: 'Utility Functions',
children: [
{
id: 'tree-utils',
content: 'Tree Manipulation Utils',
children: []

Check notice on line 73 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L73

Missing trailing comma. (trailing-comma)
}

Check notice on line 74 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L74

Missing trailing comma. (trailing-comma)
]

Check notice on line 75 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L75

Missing trailing comma. (trailing-comma)
}

Check notice on line 76 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L76

Missing trailing comma. (trailing-comma)
]

Check notice on line 77 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L77

Missing trailing comma. (trailing-comma)
}

Check notice on line 78 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L78

Missing trailing comma. (trailing-comma)
];

interface TreeNodeProps {
data: DataNode;
ListItem?: React.ElementType;
List?: React.ElementType;
ContentFrame?: React.ElementType;
}
export const EnhancedDeepTreeDemo: React.FC = () => {
const [searchQuery, setSearchQuery] = useState('');
const [selectedNode, setSelectedNode] = useState<DataNode | null>(null);

export function TreeNode({ data, ListItem = 'li', List = 'ul', ContentFrame = 'div' }: TreeNodeProps) {
const { content, children } = data;
let item;
let treeNodesList;
const handleNodeClick = (node: DataNode) => {
setSelectedNode(node);
};

const customRenderNode = (node: DataNode, isLeaf: boolean) => {
const getIcon = () => {
if (node.id === 'docs') return 'πŸ“š';
if (node.id === 'src') return 'πŸ’»';
if (node.content.includes('Component')) return 'βš›οΈ';
if (node.content.includes('API')) return 'πŸ”—';
if (node.content.includes('Guide')) return 'πŸ“–';
if (isLeaf) return 'πŸ“„';
return 'πŸ“';
};

return (
<div style={{

Check notice on line 101 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L101

Trailing whitespace. (no-trailing-whitespace)
display: 'flex',

Check notice on line 102 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L102

Trailing whitespace. (no-trailing-whitespace)
alignItems: 'center',
padding: '2px 4px',
borderRadius: '3px',
background: selectedNode?.id === node.id ? '#e3f2fd' : 'transparent'

Check notice on line 106 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L106

Missing trailing comma. (trailing-comma)
}}>
<span style={{ marginRight: '6px' }}>{getIcon()}</span>
<span>{node.content}</span>
</div>
);
};

if (content !== undefined) {
item = <ContentFrame>{content}</ContentFrame>;
}
if (children && children.length > 0) {
const treeNodes = children.map((element, index) => (
<TreeNode data={element} key={index} ListItem={ListItem} List={List} ContentFrame={ContentFrame} />
));
treeNodesList = <List>{treeNodes}</List>;
}
return (
<ListItem>
{item}
{treeNodesList}
</ListItem>
<div style={{ maxWidth: '800px', margin: '0 auto', padding: '20px' }}>
<h2>Enhanced React Deep Tree Demo</h2>

Check notice on line 117 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L117

Trailing whitespace. (no-trailing-whitespace)
<div style={{ marginBottom: '20px' }}>
<input
type="text"
placeholder="Search in tree..."
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
style={{
width: '100%',
padding: '8px 12px',
fontSize: '14px',
border: '1px solid #ddd',
borderRadius: '4px',
boxSizing: 'border-box'

Check notice on line 130 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L130

Missing trailing comma. (trailing-comma)
}}
/>
</div>

<div style={{ display: 'flex', gap: '20px' }}>
<div style={{ flex: 1 }}>
<h3>Interactive Tree</h3>
<div style={{
border: '1px solid #ddd',
borderRadius: '4px',
padding: '10px',
background: '#fafafa',
minHeight: '300px'

Check notice on line 143 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L143

Missing trailing comma. (trailing-comma)
}}>
<DeepTree
data={demoData}
renderNode={customRenderNode}
searchQuery={searchQuery}
searchMethod={treeUtils.defaultSearchMethod}
onNodeClick={handleNodeClick}
/>
</div>
</div>

<div style={{ width: '250px' }}>
<h3>Tree Statistics</h3>
<div style={{
border: '1px solid #ddd',
borderRadius: '4px',
padding: '10px',
background: '#f9f9f9'

Check notice on line 161 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L161

Missing trailing comma. (trailing-comma)
}}>
<p><strong>Total nodes:</strong> {treeUtils.countNodes(demoData)}</p>
<p><strong>Max depth:</strong> {treeUtils.getMaxDepth(demoData)}</p>
{searchQuery && (
<p><strong>Matching nodes:</strong> {
treeUtils.filterNodes(demoData, node =>

Check notice on line 167 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L167

Trailing whitespace. (no-trailing-whitespace)
treeUtils.defaultSearchMethod(node, searchQuery)

Check notice on line 168 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L168

Missing trailing comma. (trailing-comma)
).length
}</p>
)}
</div>

{selectedNode && (
<div style={{ marginTop: '20px' }}>
<h3>Selected Node</h3>
<div style={{
border: '1px solid #ddd',
borderRadius: '4px',
padding: '10px',
background: '#f0f8ff',
fontSize: '12px'

Check notice on line 182 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L182

Missing trailing comma. (trailing-comma)
}}>
<p><strong>ID:</strong> {selectedNode.id}</p>
<p><strong>Content:</strong> {selectedNode.content}</p>
<p><strong>Children:</strong> {selectedNode.children.length}</p>
<p><strong>Has Path:</strong> {
treeUtils.findNodePath(demoData, selectedNode.id!)?.join(' β†’ ') || 'N/A'
}</p>
</div>
</div>
)}
</div>
</div>
</div>
);
}
};

interface DeepTreeProps {
data: DataNode[];
ListItem?: React.ElementType;
List?: React.ElementType;
ContentFrame?: React.ElementType;
TreeFrame?: React.ElementType;
// Maintain backward compatibility
export function TreeNode({ data, ListItem = 'li', List = 'ul', ContentFrame = 'div' }: any) {
return (
<DeepTree

Check notice on line 202 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L202

Trailing whitespace. (no-trailing-whitespace)
data={[data]}

Check notice on line 203 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L203

Trailing whitespace. (no-trailing-whitespace)
ListItem={ListItem}
List={List}
ContentFrame={ContentFrame}
/>
);
}

export function DeepTree({ data, ListItem = 'li', List = 'ul', ContentFrame = 'div', TreeFrame='div' }: DeepTreeProps) {
export function DeepTree_Legacy({ data, ListItem = 'li', List = 'ul', ContentFrame = 'div', TreeFrame = 'div' }: any) {
return (
<TreeFrame>
<List>
{data.map((object, index) => (
<TreeNode key={index} data={object} ListItem={ListItem} List={List} ContentFrame={ContentFrame} />
))}
</List>
</TreeFrame>
<DeepTree

Check notice on line 213 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L213

Trailing whitespace. (no-trailing-whitespace)
data={data}
ListItem={ListItem}
List={List}
ContentFrame={ContentFrame}
TreeFrame={TreeFrame}
/>
);
}

export default DeepTree;
export default EnhancedDeepTreeDemo;

Check notice on line 223 in example/components/deep-tree.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/deep-tree.tsx#L223

File should end with a newline. (eofline)
77 changes: 77 additions & 0 deletions examples/basic-usage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/basic-usage.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading