@clevertask/react-sortable-tree 0.0.1 → 0.0.3

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
@@ -32,17 +32,17 @@ import '@clevertask/react-sortable-tree/dist/style.css';
32
32
  import { SortableTree, createOptimizedTreeStructure } from '@clevertask/react-sortable-tree';
33
33
 
34
34
  function App() {
35
- const [treeStructure, setTreeStructure] = useState(createOptimizedTreeStructure([
36
- { id: '1', label: 'Item 1', children: [] },
37
- { id: '2', label: 'Item 2', children: [
38
- { id: '3', label: 'Item 2.1', children: [] }
39
- ] },
40
- ]));
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
+ );
41
41
 
42
42
  return (
43
43
  <SortableTree
44
- items={treeStructure.items}
45
- onItemsChange={setTreeStructure}
44
+ treeStructure={treeStructure}
45
+ setTreeStructure={setTreeStructure}
46
46
  isCollapsible
47
47
  isRemovable
48
48
  allowNestedItemAddition
@@ -54,20 +54,20 @@ function App() {
54
54
 
55
55
  ## Props
56
56
 
57
- | Prop | Type | Default | Description |
58
- |------|------|---------|-------------|
59
- | `items` | `TreeItems` | Required | The array of tree items to be rendered. |
60
- | `onItemsChange` | `React.Dispatch<React.SetStateAction<OptimizedTreeStructure>>` | Required | Callback function called when the tree structure changes. |
61
- | `indentationWidth` | `number` | `undefined` | The indentation width for children elements. |
62
- | `isCollapsible` | `boolean` | `false` | Determines if tree items can be collapsed/expanded. |
63
- | `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 |
64
- | `showDropIndicator` | `boolean` | `false` | Determines if a drop indicator should be shown when dragging items. |
65
- | `isRemovable` | `boolean` | `false` | Determines if items can be removed from the tree. |
66
- | `onRemoveItem` | `(id: UniqueIdentifier) => void` | `undefined` | Callback function called when an item is removed from the tree. |
67
- | `allowNestedItemAddition` | `boolean` | `false` | Determines if new items can be added as children to existing items. |
68
- | `onAddItem` | `(parentId: UniqueIdentifier \| null) => void` | `undefined` | Callback function called when a new item is added to the tree. |
69
- | `onDragEnd` | `(result: DropResult) => void` | `undefined` | Callback function called when a drag operation ends. |
70
- | `onItemClick` | `(id: UniqueIdentifier) => void` | `undefined` | Callback function called when an item in the tree is clicked. |
57
+ | Prop | Type | Default | Description |
58
+ | ------------------------- | --------------------------------------------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------- |
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. |
61
+ | `indentationWidth` | `number` | `undefined` | The indentation width for children elements. |
62
+ | `isCollapsible` | `boolean` | `false` | Determines if tree items can be collapsed/expanded. |
63
+ | `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 |
64
+ | `showDropIndicator` | `boolean` | `false` | Determines if a drop indicator should be shown when dragging items. |
65
+ | `isRemovable` | `boolean` | `false` | Determines if items can be removed from the tree. |
66
+ | `onRemoveItem` | `(id: UniqueIdentifier) => void` | `undefined` | Callback function called when an item is removed from the tree. |
67
+ | `allowNestedItemAddition` | `boolean` | `false` | Determines if new items can be added as children to existing items. |
68
+ | `onAddItem` | `(parentId: UniqueIdentifier \| null) => void` | `undefined` | Callback function called when a new item is added to the tree. |
69
+ | `onDragEnd` | `(result: DropResult) => void` | `undefined` | Callback function called when a drag operation ends. |
70
+ | `onItemClick` | `(id: UniqueIdentifier) => void` | `undefined` | Callback function called when an item in the tree is clicked. |
71
71
 
72
72
  ## Types
73
73
 
@@ -107,13 +107,25 @@ The `OptimizedTreeStructure` is used internally to improve performance for large
107
107
  ### createOptimizedTreeStructure
108
108
 
109
109
  ```typescript
110
- function createOptimizedTreeStructure(items: TreeItems): OptimizedTreeStructure
110
+ function createOptimizedTreeStructure(items: TreeItems): OptimizedTreeStructure;
111
+ ```
112
+
113
+ ### getItemById
114
+ Retrieves a tree item by its unique identifier.
115
+ ```typescript
116
+ function getItemById(
117
+ structure: OptimizedTreeStructure,
118
+ id: UniqueIdentifier,
119
+ ): TreeItem | undefined;
111
120
  ```
112
121
 
113
122
  ### removeItemById
114
123
 
115
124
  ```typescript
116
- function removeItemById(structure: OptimizedTreeStructure, id: UniqueIdentifier): OptimizedTreeStructure
125
+ function removeItemById(
126
+ structure: OptimizedTreeStructure,
127
+ id: UniqueIdentifier,
128
+ ): OptimizedTreeStructure;
117
129
  ```
118
130
 
119
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.
@@ -131,8 +143,8 @@ setTreeStructure(updatedStructure);
131
143
  function setTreeItemProperties(
132
144
  structure: OptimizedTreeStructure,
133
145
  id: UniqueIdentifier,
134
- setter: (value: TreeItem) => Partial<TreeItem>
135
- ): OptimizedTreeStructure
146
+ setter: (value: TreeItem) => Partial<TreeItem>,
147
+ ): OptimizedTreeStructure;
136
148
  ```
137
149
 
138
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`.
@@ -141,11 +153,10 @@ Usage example:
141
153
 
142
154
  ```typescript
143
155
  setTreeStructure((treeStructure) => {
144
- return setTreeItemProperties(
145
- treeStructure,
146
- '123',
147
- (item) => ({ label: 'New Label', collapsed: !item.collapsed })
148
- );
156
+ return setTreeItemProperties(treeStructure, '123', (item) => ({
157
+ label: 'New Label',
158
+ collapsed: !item.collapsed,
159
+ }));
149
160
  });
150
161
  ```
151
162
 
@@ -1,4 +1,4 @@
1
1
  import { SortableTreeProps } from './types';
2
- declare function PrivateSortableTree({ items, onItemsChange: setItems, isCollapsible, onLazyLoadChildren, showDropIndicator, indentationWidth, isRemovable, onRemoveItem, allowNestedItemAddition, onAddItem, onDragEnd, onItemClick, }: SortableTreeProps): import("react/jsx-runtime").JSX.Element;
2
+ declare function PrivateSortableTree({ treeStructure, setTreeStructure, isCollapsible, onLazyLoadChildren, showDropIndicator, indentationWidth, isRemovable, onRemoveItem, allowNestedItemAddition, onAddItem, onDragEnd, onItemClick, }: SortableTreeProps): import("react/jsx-runtime").JSX.Element;
3
3
  export declare const SortableTree: import('react').MemoExoticComponent<typeof PrivateSortableTree>;
4
4
  export {};
@@ -1,5 +1,5 @@
1
1
  import { default as React, HTMLAttributes } from 'react';
2
- export interface Props extends Omit<HTMLAttributes<HTMLLIElement>, "id"> {
2
+ export interface Props extends Omit<HTMLAttributes<HTMLLIElement>, 'id'> {
3
3
  childCount?: number;
4
4
  clone?: boolean;
5
5
  collapsed?: boolean;
@@ -64,12 +64,12 @@ export interface SortableTreeProps {
64
64
  /**
65
65
  * The array of tree items to be rendered.
66
66
  */
67
- items: TreeItems;
67
+ treeStructure: OptimizedTreeStructure;
68
68
  /**
69
69
  * Callback function called when the tree structure changes.
70
70
  * @param items - The updated array of tree items.
71
71
  */
72
- onItemsChange: React.Dispatch<React.SetStateAction<OptimizedTreeStructure>>;
72
+ setTreeStructure: React.Dispatch<React.SetStateAction<OptimizedTreeStructure>>;
73
73
  /**
74
74
  * Determines if tree items can be collapsed/expanded.
75
75
  * @default false
@@ -81,7 +81,7 @@ export interface SortableTreeProps {
81
81
  * @param id - The id of the parent item being expanded.
82
82
  * @param isExpanding - True if the item is being expanded, false if collapsing.
83
83
  */
84
- onLazyLoadChildren?: (id: UniqueIdentifier, isExpanding: boolean) => Promise<void>;
84
+ onLazyLoadChildren?: (id: UniqueIdentifier, isExpanding: boolean) => void;
85
85
  /**
86
86
  * Determines if a drop indicator should be shown when dragging items.
87
87
  * @default false
@@ -10,9 +10,22 @@ 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(items: TreeItems, itemId: UniqueIdentifier): TreeItem | undefined;
13
+ export declare function findItemDeep(structure: OptimizedTreeStructure, itemId: UniqueIdentifier): TreeItem | undefined;
14
14
  export declare function removeItemById(structure: OptimizedTreeStructure, id: UniqueIdentifier): OptimizedTreeStructure;
15
15
  export declare function setTreeItemProperties(structure: OptimizedTreeStructure, id: UniqueIdentifier, setter: (value: TreeItem) => Partial<TreeItem>): OptimizedTreeStructure;
16
+ /**
17
+ * Retrieves a tree item by its unique identifier.
18
+ * @param structure The optimized tree structure containing items and their map.
19
+ * @param id The unique identifier of the item to retrieve.
20
+ * @returns The tree item if found, undefined otherwise.
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
+ */
16
29
  export declare function createOptimizedTreeStructure(items: TreeItems): OptimizedTreeStructure;
17
- export declare function getChildCount(items: TreeItems, id: UniqueIdentifier): number;
30
+ export declare function getChildCount(treeStructure: OptimizedTreeStructure, id: UniqueIdentifier): number;
18
31
  export declare function removeChildrenOf(items: FlattenedItem[], ids: UniqueIdentifier[]): FlattenedItem[];