@clevertask/react-sortable-tree 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @clevertask/react-sortable-tree
2
2
 
3
- A customizable React component for rendering and managing tree structures with drag-and-drop functionality.
3
+ A customizable React component for rendering and managing tree structures with drag-and-drop functionality. This is built on top of the [sortable tree Component from the dnd-kit library](https://github.com/clauderic/dnd-kit/blob/master/stories/3%20-%20Examples/Tree/SortableTree.tsx).
4
4
 
5
5
  ## Table of Contents
6
6
 
@@ -10,9 +10,8 @@ A customizable React component for rendering and managing tree structures with d
10
10
  - [Types](#types)
11
11
  - [TreeItem](#treeitem)
12
12
  - [TreeItems](#treeitems)
13
- - [OptimizedTreeStructure](#optimizedtreestructure)
14
13
  - [Helper Functions](#helper-functions)
15
- - [createOptimizedTreeStructure](#createoptimizedtreestructure)
14
+ - [getItemById](#getItemById)
16
15
  - [removeItemById](#removeitembyid)
17
16
  - [setTreeItemProperties](#settreeitemproperties)
18
17
  - [Roadmap](#roadmap)
@@ -29,20 +28,19 @@ npm install @clevertask/react-sortable-tree
29
28
 
30
29
  ```tsx
31
30
  import '@clevertask/react-sortable-tree/dist/style.css';
32
- import { SortableTree, createOptimizedTreeStructure } from '@clevertask/react-sortable-tree';
31
+ import React, { useState } from 'react';
32
+ import { SortableTree, TreeItems } from '@clevertask/react-sortable-tree';
33
33
 
34
34
  function App() {
35
- const [treeStructure, setTreeStructure] = useState(
36
- createOptimizedTreeStructure([
37
- { id: '1', label: 'Item 1', children: [] },
38
- { id: '2', label: 'Item 2', children: [{ id: '3', label: 'Item 2.1', children: [] }] },
39
- ]),
40
- );
35
+ const [items, setItems] = useState<TreeItems>([
36
+ { id: '1', label: 'Item 1', children: [] },
37
+ { id: '2', label: 'Item 2', children: [{ id: '3', label: 'Item 2.1', children: [] }] },
38
+ ]);
41
39
 
42
40
  return (
43
41
  <SortableTree
44
- treeStructure={treeStructure}
45
- setTreeStructure={setTreeStructure}
42
+ items={items}
43
+ setItems={setItems}
46
44
  isCollapsible
47
45
  isRemovable
48
46
  allowNestedItemAddition
@@ -56,8 +54,8 @@ function App() {
56
54
 
57
55
  | Prop | Type | Default | Description |
58
56
  | ------------------------- | --------------------------------------------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------- |
59
- | `treeStructure` | `OptimizedTreeStructure` | Required | The array of tree items to be rendered. |
60
- | `setTreeStructure` | `React.Dispatch<React.SetStateAction<OptimizedTreeStructure>>` | Required | Callback function called when the tree structure changes. |
57
+ | `items` | `TreeItems` | Required | The array of tree items to be rendered. |
58
+ | `setItems` | `React.Dispatch<React.SetStateAction<TreeItems>>` | Required | Callback function called when the tree items array changes. |
61
59
  | `indentationWidth` | `number` | `undefined` | The indentation width for children elements. |
62
60
  | `isCollapsible` | `boolean` | `false` | Determines if tree items can be collapsed/expanded. |
63
61
  | `onLazyLoadChildren` | `(id: UniqueIdentifier, isExpanding: boolean) => Promise<void>` | `undefined` | Callback for lazy loading child items when a parent is expanded. Useful for getting child items from an API endpoint |
@@ -91,79 +89,60 @@ type TreeItem = {
91
89
  type TreeItems = TreeItem[];
92
90
  ```
93
91
 
94
- ### OptimizedTreeStructure
95
-
96
- ```typescript
97
- interface OptimizedTreeStructure {
98
- items: TreeItems;
99
- itemMap: Map<UniqueIdentifier, TreeItem>;
100
- }
101
- ```
102
-
103
- The `OptimizedTreeStructure` is used internally to improve performance for large trees. Use the `createOptimizedTreeStructure` function to create this structure from a `TreeItems` array.
104
-
105
92
  ## Helper Functions
106
93
 
107
- ### createOptimizedTreeStructure
94
+ ### getItemById
108
95
 
109
96
  ```typescript
110
- function createOptimizedTreeStructure(items: TreeItems): OptimizedTreeStructure;
97
+ function getItemById(items: TreeItems, id: UniqueIdentifier): TreeItem | undefined;
111
98
  ```
112
99
 
113
- ### getItemById
114
100
  Retrieves a tree item by its unique identifier.
101
+
102
+ Usage example:
103
+
115
104
  ```typescript
116
- function getItemById(
117
- structure: OptimizedTreeStructure,
118
- id: UniqueIdentifier,
119
- ): TreeItem | undefined;
105
+ const item = getItemById(items, '1');
120
106
  ```
121
107
 
122
108
  ### removeItemById
123
109
 
124
110
  ```typescript
125
- function removeItemById(
126
- structure: OptimizedTreeStructure,
127
- id: UniqueIdentifier,
128
- ): OptimizedTreeStructure;
111
+ function removeItemById(items: TreeItems, id: UniqueIdentifier): TreeItems;
129
112
  ```
130
113
 
131
- This function removes an item from the tree structure by its ID. It returns a new `OptimizedTreeStructure` with the item removed from both the `items` array and the `itemMap`. It also handles removing the item from nested children.
114
+ This function removes an item from the tree structure by its ID. It returns a new `TreeItems` array with the item removed. It also handles removing the item from nested children.
132
115
 
133
116
  Usage example:
134
117
 
135
118
  ```typescript
136
- const updatedStructure = removeItemById(currentStructure, '123');
137
- setTreeStructure(updatedStructure);
119
+ const updatedItems = removeItemById(items, '123');
120
+ setItems(updatedItems);
138
121
  ```
139
122
 
140
123
  ### setTreeItemProperties
141
124
 
142
125
  ```typescript
143
126
  function setTreeItemProperties(
144
- structure: OptimizedTreeStructure,
127
+ items: TreeItems,
145
128
  id: UniqueIdentifier,
146
129
  setter: (value: TreeItem) => Partial<TreeItem>,
147
- ): OptimizedTreeStructure;
130
+ ): TreeItems;
148
131
  ```
149
132
 
150
- This function updates the properties of a specific tree item. It takes a setter function that receives the current item and returns an object with the properties to be updated. It returns a new `OptimizedTreeStructure` with the updated item in both the `items` array and the `itemMap`.
133
+ This function updates the properties of a specific tree item. It takes a setter function that receives the current item and returns an object with the properties to be updated. It returns a new `TreeItems` array with the updated item.
151
134
 
152
135
  Usage example:
153
136
 
154
137
  ```typescript
155
- setTreeStructure((treeStructure) => {
156
- return setTreeItemProperties(treeStructure, '123', (item) => ({
138
+ setItems((items) => {
139
+ return setTreeItemProperties(items, '123', (item) => ({
157
140
  label: 'New Label',
158
141
  collapsed: !item.collapsed,
159
142
  }));
160
143
  });
161
144
  ```
162
145
 
163
- These helper functions are designed to work with the `OptimizedTreeStructure` to ensure efficient updates to the tree. They maintain both the tree hierarchy in the `items` array and the fast lookup `O(1)` capability of the `itemMap`.
164
-
165
- Use this function to create an `OptimizedTreeStructure` from a `TreeItems` array. This is useful when initializing your state.
166
-
167
146
  ## Roadmap
168
147
 
169
148
  We're constantly working to improve @clevertask/react-sortable-tree. Here are some features we're planning to implement:
@@ -1,4 +1,4 @@
1
1
  import { SortableTreeProps } from './types';
2
- declare function PrivateSortableTree({ treeStructure, setTreeStructure, isCollapsible, onLazyLoadChildren, showDropIndicator, indentationWidth, isRemovable, onRemoveItem, allowNestedItemAddition, onAddItem, onDragEnd, onItemClick, }: SortableTreeProps): import("react/jsx-runtime").JSX.Element;
2
+ declare function PrivateSortableTree({ items, setItems, isCollapsible, onLazyLoadChildren, showDropIndicator, indentationWidth, isRemovable, onRemoveItem, allowNestedItemAddition, onAddItem, onDragEnd, onItemClick, renderItem, }: SortableTreeProps): import("react/jsx-runtime").JSX.Element;
3
3
  export declare const SortableTree: import('react').MemoExoticComponent<typeof PrivateSortableTree>;
4
4
  export {};
@@ -17,6 +17,7 @@ export interface Props extends Omit<HTMLAttributes<HTMLLIElement>, 'id'> {
17
17
  onAdd?(): void;
18
18
  onLabelClick?(): void;
19
19
  wrapperRef?(node: HTMLLIElement): void;
20
+ renderedItem?: React.ReactNode;
20
21
  }
21
22
  export declare const _TreeItem: React.ForwardRefExoticComponent<Props & React.RefAttributes<HTMLDivElement>>;
22
23
  export declare const TreeItem: React.MemoExoticComponent<React.ForwardRefExoticComponent<Props & React.RefAttributes<HTMLDivElement>>>;
@@ -1,3 +1,3 @@
1
1
  export * from './types';
2
2
  export { SortableTree } from './SortableTree';
3
- export { setTreeItemProperties, removeItemById, createOptimizedTreeStructure, getItemById, } from './utilities';
3
+ export { setTreeItemProperties, removeItemById, getItemById } from './utilities';
@@ -1,9 +1,5 @@
1
1
  import { MutableRefObject } from 'react';
2
2
  import { UniqueIdentifier } from '@dnd-kit/core';
3
- export interface OptimizedTreeStructure {
4
- items: TreeItems;
5
- itemMap: Map<UniqueIdentifier, TreeItem>;
6
- }
7
3
  /**
8
4
  * Represents an item in the tree structure.
9
5
  */
@@ -64,12 +60,12 @@ export interface SortableTreeProps {
64
60
  /**
65
61
  * The array of tree items to be rendered.
66
62
  */
67
- treeStructure: OptimizedTreeStructure;
63
+ items: TreeItems;
68
64
  /**
69
65
  * Callback function called when the tree structure changes.
70
66
  * @param items - The updated array of tree items.
71
67
  */
72
- setTreeStructure: React.Dispatch<React.SetStateAction<OptimizedTreeStructure>>;
68
+ setItems: React.Dispatch<React.SetStateAction<TreeItems>>;
73
69
  /**
74
70
  * Determines if tree items can be collapsed/expanded.
75
71
  * @default false
@@ -117,6 +113,13 @@ export interface SortableTreeProps {
117
113
  * @param id - The id of the clicked item.
118
114
  */
119
115
  onItemClick?: (id: UniqueIdentifier) => void;
116
+ /**
117
+ * You can place a react component next to the item's label. This is temporal while we
118
+ * figure out a way of rendering a whole custom item
119
+ * @param item
120
+ * @returns
121
+ */
122
+ renderItem?: (item: TreeItem) => React.ReactNode;
120
123
  }
121
124
  /**
122
125
  * Represents the result of a drag operation in the tree.
@@ -1,5 +1,5 @@
1
1
  import { UniqueIdentifier } from '@dnd-kit/core';
2
- import { FlattenedItem, OptimizedTreeStructure, TreeItem, TreeItems } from './types';
2
+ import { FlattenedItem, TreeItem, TreeItems } from './types';
3
3
  export declare const iOS: boolean;
4
4
  export declare function getProjection(items: FlattenedItem[], activeId: UniqueIdentifier, overId: UniqueIdentifier, dragOffset: number, indentationWidth: number): {
5
5
  depth: number;
@@ -10,22 +10,15 @@ export declare function getProjection(items: FlattenedItem[], activeId: UniqueId
10
10
  export declare function flattenTree(items: TreeItems): FlattenedItem[];
11
11
  export declare function buildTree(flattenedItems: FlattenedItem[]): TreeItems;
12
12
  export declare function findItem(items: TreeItem[], itemId: UniqueIdentifier): TreeItem | undefined;
13
- export declare function findItemDeep(structure: OptimizedTreeStructure, itemId: UniqueIdentifier): TreeItem | undefined;
14
- export declare function removeItemById(structure: OptimizedTreeStructure, id: UniqueIdentifier): OptimizedTreeStructure;
15
- export declare function setTreeItemProperties(structure: OptimizedTreeStructure, id: UniqueIdentifier, setter: (value: TreeItem) => Partial<TreeItem>): OptimizedTreeStructure;
13
+ export declare function findItemDeep(items: TreeItems, itemId: UniqueIdentifier): TreeItem | undefined;
14
+ export declare function removeItemById(items: TreeItems, id: UniqueIdentifier): TreeItems;
15
+ export declare function setTreeItemProperties(items: TreeItems, id: UniqueIdentifier, setter: (value: TreeItem) => Partial<TreeItem>): TreeItems;
16
16
  /**
17
17
  * Retrieves a tree item by its unique identifier.
18
- * @param structure The optimized tree structure containing items and their map.
18
+ * @param structure The current tree items array
19
19
  * @param id The unique identifier of the item to retrieve.
20
20
  * @returns The tree item if found, undefined otherwise.
21
21
  */
22
- export declare function getItemById(structure: OptimizedTreeStructure, id: UniqueIdentifier): TreeItem | undefined;
23
- /**
24
- * Creates an optimized tree structure from the given items.
25
- * This structure includes both the original items and a map for efficient item lookup.
26
- * @param items The original tree items to optimize.
27
- * @returns An OptimizedTreeStructure containing the items and their map.
28
- */
29
- export declare function createOptimizedTreeStructure(items: TreeItems): OptimizedTreeStructure;
30
- export declare function getChildCount(treeStructure: OptimizedTreeStructure, id: UniqueIdentifier): number;
22
+ export declare function getItemById(items: TreeItems, id: UniqueIdentifier): TreeItem | undefined;
23
+ export declare function getChildCount(treeStructure: TreeItems, id: UniqueIdentifier): number;
31
24
  export declare function removeChildrenOf(items: FlattenedItem[], ids: UniqueIdentifier[]): FlattenedItem[];