|
1 |
| -# react-deep-tree |
| 1 | +# React Deep Tree |
2 | 2 |
|
3 |
| -An attempt to make traversing trees with deep-deep structure user-friendly. Started as a part of our [UI prototype](https://github.com/linksplatform/InfiniteDepthTreeWebUIPrototype). |
| 3 | +A React component for rendering infinite depth tree structures with navigation, search, and keyboard controls. This package is adapted from the [InfiniteDepthTreeWebUIPrototype](https://github.com/linksplatform/InfiniteDepthTreeWebUIPrototype) repository and provides a React-based implementation for client-side tree navigation. |
4 | 4 |
|
5 |
| -## Install |
| 5 | +## Features |
6 | 6 |
|
7 |
| -```Shell |
8 |
| -npm i react-deep-tree |
| 7 | +- ✨ **Infinite Depth**: Support for unlimited levels of nested tree structures |
| 8 | +- ⌨️ **Keyboard Navigation**: Arrow keys, mouse wheel navigation |
| 9 | +- 🔍 **Search Functionality**: Real-time search with filtering |
| 10 | +- 🎨 **Customizable Components**: Replace default HTML elements with custom components |
| 11 | +- 🖱️ **Mouse Interactions**: Click to select, smooth scrolling |
| 12 | +- 📱 **Responsive Design**: Mobile-friendly layout |
| 13 | +- 🎯 **TypeScript Support**: Full TypeScript definitions included |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```bash |
| 18 | +npm install react-deep-tree |
9 | 19 | ```
|
10 | 20 |
|
11 |
| -## [Use](https://codesandbox.io/s/intelligent-bird-j5vit) |
| 21 | +## Quick Start |
12 | 22 |
|
13 |
| -```JavaScript |
14 |
| -import React from "react"; |
15 |
| -import DeepTree from "react-deep-tree"; |
16 |
| -import type { DataNode } from "react-deep-tree"; |
| 23 | +```tsx |
| 24 | +import DeepTree, { DataNode } from 'react-deep-tree'; |
17 | 25 |
|
18 |
| -const data : DataNode[] = [ |
19 |
| - { |
20 |
| - content: 'Text of a first level of the first element', |
21 |
| - children: [ |
22 |
| - { |
23 |
| - content: 'Text of a second level of the first element', |
24 |
| - children: [], |
25 |
| - }, |
26 |
| - { |
27 |
| - content: 'Text of a second level of the first element', |
28 |
| - children: [], |
29 |
| - }, |
30 |
| - ], |
31 |
| - }, |
| 26 | +const data: DataNode[] = [ |
32 | 27 | {
|
33 |
| - content: 'Text of a first level of the second element', |
| 28 | + content: 'Root Item', |
34 | 29 | children: [
|
35 |
| - { |
36 |
| - content: 'Text of a second level of the second element', |
37 |
| - children: [], |
38 |
| - }, |
39 |
| - { |
40 |
| - content: 'Text of a second level of the second element', |
41 |
| - children: [], |
42 |
| - }, |
43 |
| - ], |
44 |
| - }, |
| 30 | + { content: 'Child 1' }, |
| 31 | + { |
| 32 | + content: 'Child 2', |
| 33 | + children: [ |
| 34 | + { content: 'Grandchild 1' }, |
| 35 | + { content: 'Grandchild 2' } |
| 36 | + ] |
| 37 | + } |
| 38 | + ] |
| 39 | + } |
45 | 40 | ];
|
46 | 41 |
|
47 | 42 | export default function App() {
|
48 | 43 | return <DeepTree data={data} />;
|
49 | 44 | }
|
50 | 45 | ```
|
| 46 | + |
| 47 | +## Advanced Usage |
| 48 | + |
| 49 | +### With Navigation and Search |
| 50 | + |
| 51 | +```tsx |
| 52 | +import DeepTree from 'react-deep-tree'; |
| 53 | + |
| 54 | +function NavigableTree() { |
| 55 | + return ( |
| 56 | + <DeepTree |
| 57 | + data={data} |
| 58 | + enableNavigation={true} |
| 59 | + enableSearch={true} |
| 60 | + onNodeSelect={(node, index) => { |
| 61 | + console.log('Selected:', node.content); |
| 62 | + }} |
| 63 | + /> |
| 64 | + ); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Custom Components |
| 69 | + |
| 70 | +```tsx |
| 71 | +const CustomListItem = ({ children, ...props }) => ( |
| 72 | + <div className="my-custom-item" {...props}> |
| 73 | + {children} |
| 74 | + </div> |
| 75 | +); |
| 76 | + |
| 77 | +function CustomTree() { |
| 78 | + return ( |
| 79 | + <DeepTree |
| 80 | + data={data} |
| 81 | + ListItem={CustomListItem} |
| 82 | + ContentFrame="span" |
| 83 | + TreeFrame="section" |
| 84 | + /> |
| 85 | + ); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## API Reference |
| 90 | + |
| 91 | +### DeepTree Props |
| 92 | + |
| 93 | +| Prop | Type | Default | Description | |
| 94 | +|------|------|---------|-------------| |
| 95 | +| `data` | `DataNode[]` | - | **Required.** The tree data structure | |
| 96 | +| `enableNavigation` | `boolean` | `false` | Enable keyboard/mouse navigation | |
| 97 | +| `enableSearch` | `boolean` | `false` | Show search input by default | |
| 98 | +| `onNodeSelect` | `(node: DataNode, index: number) => void` | - | Callback when a node is selected | |
| 99 | +| `selectedIndex` | `number` | - | Controlled selected index | |
| 100 | +| `ListItem` | `React.ElementType` | `'li'` | Component for list items | |
| 101 | +| `List` | `React.ElementType` | `'ul'` | Component for lists | |
| 102 | +| `ContentFrame` | `React.ElementType` | `'div'` | Component for content wrapper | |
| 103 | +| `TreeFrame` | `React.ElementType` | `'div'` | Component for tree wrapper | |
| 104 | +| `className` | `string` | `''` | CSS class for the tree container | |
| 105 | +| `style` | `React.CSSProperties` | `{}` | Inline styles for the tree container | |
| 106 | + |
| 107 | +### DataNode Interface |
| 108 | + |
| 109 | +```tsx |
| 110 | +interface DataNode { |
| 111 | + readonly content: any; |
| 112 | + readonly children?: DataNode[]; |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### Navigation Hook |
| 117 | + |
| 118 | +You can also use the navigation functionality separately: |
| 119 | + |
| 120 | +```tsx |
| 121 | +import { useTreeNavigation } from 'react-deep-tree'; |
| 122 | + |
| 123 | +function MyComponent() { |
| 124 | + const navigation = useTreeNavigation({ |
| 125 | + data: myData, |
| 126 | + enableNavigation: true, |
| 127 | + onNodeSelect: handleSelect |
| 128 | + }); |
| 129 | + |
| 130 | + // Access navigation state |
| 131 | + console.log(navigation.selectedIndex); |
| 132 | + console.log(navigation.query); |
| 133 | + console.log(navigation.showQuery); |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Keyboard Shortcuts |
| 138 | + |
| 139 | +When `enableNavigation` is true: |
| 140 | + |
| 141 | +- `↑/↓ Arrow Keys`: Navigate up/down through items |
| 142 | +- `Mouse Wheel`: Navigate through items |
| 143 | +- `/`: Toggle search input |
| 144 | +- `Escape`: Close search input |
| 145 | + |
| 146 | +## Styling |
| 147 | + |
| 148 | +The component includes default styling inspired by the original prototype. You can: |
| 149 | + |
| 150 | +1. **Use the included CSS file:** |
| 151 | + ```tsx |
| 152 | + import 'react-deep-tree/styles.css'; |
| 153 | + ``` |
| 154 | + |
| 155 | +2. **Override with custom styles:** |
| 156 | + ```css |
| 157 | + .deep-tree { |
| 158 | + background-color: your-color; |
| 159 | + } |
| 160 | + ``` |
| 161 | + |
| 162 | +3. **Use custom components** for complete control over styling |
| 163 | + |
| 164 | +## Examples |
| 165 | + |
| 166 | +Check the `/example` directory for comprehensive examples including: |
| 167 | + |
| 168 | +- Basic tree rendering |
| 169 | +- Navigation-enabled trees |
| 170 | +- Search functionality |
| 171 | +- Custom styled components |
| 172 | +- Interactive demos |
| 173 | + |
| 174 | +## Development |
| 175 | + |
| 176 | +```bash |
| 177 | +# Install dependencies |
| 178 | +npm install |
| 179 | + |
| 180 | +# Build the library |
| 181 | +npm run build |
| 182 | + |
| 183 | +# Run linting |
| 184 | +npm run lint |
| 185 | +``` |
| 186 | + |
| 187 | +## License |
| 188 | + |
| 189 | +LGPL-3.0 - See LICENSE file for details. |
| 190 | + |
| 191 | +## Contributing |
| 192 | + |
| 193 | +This project is part of the Links Platform ecosystem. Contributions are welcome! Please read the contributing guidelines before submitting PRs. |
| 194 | + |
| 195 | +## Credits |
| 196 | + |
| 197 | +Adapted from the [InfiniteDepthTreeWebUIPrototype](https://github.com/linksplatform/InfiniteDepthTreeWebUIPrototype) by Links Platform. |
0 commit comments