|
| 1 | +import type { Meta } from "@storybook/react"; |
| 2 | +import React, { Fragment } from "react"; |
| 3 | +import { |
| 4 | + ItemInstance, |
| 5 | + asyncDataLoaderFeature, |
| 6 | + createOnDropHandler, |
| 7 | + dragAndDropFeature, |
| 8 | + hotkeysCoreFeature, |
| 9 | + insertItemsAtTarget, |
| 10 | + keyboardDragAndDropFeature, |
| 11 | + renamingFeature, |
| 12 | + searchFeature, |
| 13 | + selectionFeature, |
| 14 | +} from "@headless-tree/core"; |
| 15 | +import { AssistiveTreeDescription, useTree } from "@headless-tree/react"; |
| 16 | +import cn from "classnames"; |
| 17 | +import { DemoItem, createDemoData } from "../../utils/data"; |
| 18 | + |
| 19 | +const meta = { |
| 20 | + title: "React/Guides/External Data Management/Async Data", |
| 21 | + tags: ["guide", "external data"], |
| 22 | +} satisfies Meta; |
| 23 | + |
| 24 | +export default meta; |
| 25 | + |
| 26 | +let newItemId = 0; |
| 27 | + |
| 28 | +const getCssClass = (item: ItemInstance<DemoItem>) => |
| 29 | + cn("treeitem", { |
| 30 | + focused: item.isFocused(), |
| 31 | + expanded: item.isExpanded(), |
| 32 | + selected: item.isSelected(), |
| 33 | + folder: item.isFolder(), |
| 34 | + drop: item.isDragTarget(), |
| 35 | + searchmatch: item.isMatchingSearch(), |
| 36 | + }); |
| 37 | + |
| 38 | +// story-start |
| 39 | +// Data is of type `Record<string, DemoItem>` |
| 40 | +const { asyncDataLoader, data } = createDemoData(); |
| 41 | + |
| 42 | +export const AsyncData = () => { |
| 43 | + const tree = useTree<DemoItem>({ |
| 44 | + initialState: { |
| 45 | + expandedItems: ["fruit"], |
| 46 | + selectedItems: ["banana", "orange"], |
| 47 | + }, |
| 48 | + rootItemId: "root", |
| 49 | + createLoadingItemData: () => ({ name: "Loading..." }), |
| 50 | + getItemName: (item) => item.getItemData().name, |
| 51 | + isItemFolder: (item) => !!item.getItemData().children, |
| 52 | + canReorder: true, |
| 53 | + onDrop: createOnDropHandler((item, newChildren) => { |
| 54 | + data[item.getId()].children = newChildren; |
| 55 | + }), |
| 56 | + onRename: (item, value) => { |
| 57 | + data[item.getId()].name = value; |
| 58 | + item.updateCachedData({ ...item.getItemData(), name: value }); // TODO mention in rename docs |
| 59 | + }, |
| 60 | + indent: 20, |
| 61 | + dataLoader: asyncDataLoader, |
| 62 | + features: [ |
| 63 | + asyncDataLoaderFeature, |
| 64 | + selectionFeature, |
| 65 | + hotkeysCoreFeature, |
| 66 | + dragAndDropFeature, |
| 67 | + keyboardDragAndDropFeature, |
| 68 | + renamingFeature, |
| 69 | + searchFeature, |
| 70 | + ], |
| 71 | + |
| 72 | + // For dragging foreign items into the tree: |
| 73 | + canDropForeignDragObject: (_, target) => target.item.isFolder(), |
| 74 | + onDropForeignDragObject: async (dataTransfer, target) => { |
| 75 | + const newId = `new-${newItemId++}`; |
| 76 | + data[newId] = { name: dataTransfer.getData("text/plain") }; |
| 77 | + await insertItemsAtTarget([newId], target, (item, newChildrenIds) => { |
| 78 | + data[item.getId()].children = newChildrenIds; |
| 79 | + }); |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + return ( |
| 84 | + <> |
| 85 | + {tree.isSearchOpen() && ( |
| 86 | + <div className="searchbox"> |
| 87 | + <input {...tree.getSearchInputElementProps()} /> |
| 88 | + <span>({tree.getSearchMatchingItems().length} matches)</span> |
| 89 | + </div> |
| 90 | + )} |
| 91 | + <div {...tree.getContainerProps()} className="tree"> |
| 92 | + <AssistiveTreeDescription tree={tree} /> |
| 93 | + {tree.getItems().map((item) => ( |
| 94 | + <Fragment key={item.getId()}> |
| 95 | + {item.isRenaming() ? ( |
| 96 | + <div |
| 97 | + className="renaming-item" |
| 98 | + style={{ marginLeft: `${item.getItemMeta().level * 20}px` }} |
| 99 | + > |
| 100 | + <input {...item.getRenameInputProps()} /> |
| 101 | + </div> |
| 102 | + ) : ( |
| 103 | + <div className="outeritem"> |
| 104 | + <button |
| 105 | + {...item.getProps()} |
| 106 | + style={{ paddingLeft: `${item.getItemMeta().level * 20}px` }} |
| 107 | + > |
| 108 | + <div className={getCssClass(item)}>{item.getItemName()}</div> |
| 109 | + </button> |
| 110 | + <button |
| 111 | + onClick={() => { |
| 112 | + const parent = item.getParent(); |
| 113 | + if (!parent) return; |
| 114 | + delete data[item.getId()]; |
| 115 | + const newParentChildren = data[ |
| 116 | + parent.getId() |
| 117 | + ].children?.filter((id) => id !== item.getId()); |
| 118 | + data[parent.getId()].children = newParentChildren; |
| 119 | + parent.updateCachedChildrenIds(newParentChildren ?? []); |
| 120 | + tree.rebuildTree(); |
| 121 | + }} |
| 122 | + > |
| 123 | + delete |
| 124 | + </button> |
| 125 | + <button onClick={() => item.startRenaming()}>rename</button> |
| 126 | + </div> |
| 127 | + )} |
| 128 | + </Fragment> |
| 129 | + ))} |
| 130 | + <div style={tree.getDragLineStyle()} className="dragline" /> |
| 131 | + </div> |
| 132 | + |
| 133 | + <div className="actionbar"> |
| 134 | + <button className="actionbtn" onClick={() => tree.openSearch()}> |
| 135 | + Search items |
| 136 | + </button> |
| 137 | + <button |
| 138 | + className="actionbtn" |
| 139 | + onClick={() => tree.getItemInstance("fruit").startRenaming()} |
| 140 | + > |
| 141 | + Rename Fruit |
| 142 | + </button> |
| 143 | + <button |
| 144 | + className="actionbtn" |
| 145 | + onClick={() => { |
| 146 | + const parent = tree.getItemInstance("fruit").getParent(); |
| 147 | + if (!parent) return; |
| 148 | + delete data.fruit; |
| 149 | + const newParentChildren = data[parent.getId()].children?.filter( |
| 150 | + (id) => id !== "fruit", |
| 151 | + ); |
| 152 | + data[parent.getId()].children = newParentChildren; |
| 153 | + parent.updateCachedChildrenIds(newParentChildren ?? []); |
| 154 | + tree.rebuildTree(); |
| 155 | + }} |
| 156 | + > |
| 157 | + Delete Fruit |
| 158 | + </button> |
| 159 | + <button |
| 160 | + className="actionbtn" |
| 161 | + onClick={() => { |
| 162 | + if (!data.fruit) return; |
| 163 | + const newId = `new-${newItemId++}`; |
| 164 | + data[newId] = { name: "New item" }; |
| 165 | + data.fruit.children = [...(data.fruit.children || []), newId]; |
| 166 | + tree |
| 167 | + .getItemInstance("fruit") |
| 168 | + .updateCachedChildrenIds(data.fruit.children); |
| 169 | + tree.rebuildTree(); |
| 170 | + }} |
| 171 | + > |
| 172 | + Insert new item into fruit |
| 173 | + </button> |
| 174 | + <div |
| 175 | + className="foreign-dragsource" |
| 176 | + draggable |
| 177 | + onDragStart={(e) => { |
| 178 | + e.dataTransfer.setData("text/plain", "hello world"); |
| 179 | + }} |
| 180 | + > |
| 181 | + Drag me into the tree! |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + </> |
| 185 | + ); |
| 186 | +}; |
0 commit comments