|
| 1 | +--- |
| 2 | +"@zag-js/tree-view": minor |
| 3 | +--- |
| 4 | + |
| 5 | +Add support for renaming tree node labels with validation and control features. |
| 6 | + |
| 7 | +This feature enables users to edit tree node labels inline, unlocking use cases like file explorers, folder management |
| 8 | +systems, content hierarchies, and any tree-based interface where users need to rename items. |
| 9 | + |
| 10 | +## Key Features |
| 11 | + |
| 12 | +### Basic Renaming |
| 13 | + |
| 14 | +- Press `F2` on any node to enter rename mode |
| 15 | +- Input is automatically focused and synced with current label |
| 16 | +- Press `Enter` to submit or `Escape` to cancel |
| 17 | +- Blur event automatically submits changes |
| 18 | +- IME composition events are properly handled for international input |
| 19 | + |
| 20 | +### Validation & Control |
| 21 | + |
| 22 | +- **`canRename`** - Control which nodes are renameable based on node type or custom logic |
| 23 | +- **`onRenameStart`** - Called when rename mode starts (useful for analytics, showing hints) |
| 24 | +- **`onBeforeRename`** - Validate rename before accepting (e.g., prevent duplicates, empty names) |
| 25 | +- **Empty name prevention** - Automatically stays in rename mode if submitted name is empty/whitespace |
| 26 | +- **Label trimming** - Labels are automatically trimmed before being passed to callbacks |
| 27 | +- **`onRenameComplete`** - Handle the rename and update your collection |
| 28 | + |
| 29 | +### Styling & Visual State |
| 30 | + |
| 31 | +- **`data-renaming`** attribute - Added to both item and branch elements when in rename mode for easy styling |
| 32 | +- **`nodeState.renaming`** - Boolean property to check if a node is currently being renamed |
| 33 | + |
| 34 | +## API |
| 35 | + |
| 36 | +```tsx |
| 37 | +const [collection, setCollection] = useState(initialCollection) |
| 38 | + |
| 39 | +useMachine(tree.machine, { |
| 40 | + collection, |
| 41 | + |
| 42 | + // Control which nodes can be renamed |
| 43 | + canRename: (node, indexPath) => { |
| 44 | + // Only allow renaming leaf nodes (files), not branches (folders) |
| 45 | + return !node.children |
| 46 | + }, |
| 47 | + |
| 48 | + // Called when rename mode starts |
| 49 | + onRenameStart: (details) => { |
| 50 | + // details contains: { value, node, indexPath } |
| 51 | + console.log("Started renaming:", details.node.name) |
| 52 | + // Track analytics, show hints, etc. |
| 53 | + }, |
| 54 | + |
| 55 | + // Validate before accepting rename |
| 56 | + onBeforeRename: (details) => { |
| 57 | + // Note: details.label is already trimmed by the machine |
| 58 | + |
| 59 | + // Prevent empty names |
| 60 | + if (!details.label) return false |
| 61 | + |
| 62 | + // Prevent duplicate names at the same level |
| 63 | + const parentPath = details.indexPath.slice(0, -1) |
| 64 | + const parent = parentPath.length > 0 ? collection.at(parentPath) : collection.rootNode |
| 65 | + const siblings = parent?.children || [] |
| 66 | + |
| 67 | + const hasDuplicate = siblings.some((sibling) => sibling.name === details.label && sibling.id !== details.value) |
| 68 | + |
| 69 | + return !hasDuplicate |
| 70 | + }, |
| 71 | + |
| 72 | + // Handle successful rename |
| 73 | + onRenameComplete: (details) => { |
| 74 | + // details contains: { value, label (trimmed), indexPath } |
| 75 | + const node = collection.at(details.indexPath) |
| 76 | + const updatedCollection = collection.replace(details.indexPath, { |
| 77 | + ...node, |
| 78 | + name: details.label, |
| 79 | + }) |
| 80 | + setCollection(updatedCollection) |
| 81 | + }, |
| 82 | +}) |
| 83 | +``` |
| 84 | + |
| 85 | +## Component Integration |
| 86 | + |
| 87 | +```tsx |
| 88 | +const TreeNode = ({ node, indexPath, api }) => { |
| 89 | + const nodeState = api.getNodeState({ node, indexPath }) |
| 90 | + |
| 91 | + return ( |
| 92 | + <div {...api.getItemProps({ node, indexPath })}> |
| 93 | + <FileIcon /> |
| 94 | + |
| 95 | + {/* Show text when not renaming */} |
| 96 | + <span {...api.getItemTextProps({ node, indexPath })} style={{ display: nodeState.renaming ? "none" : "inline" }}> |
| 97 | + {node.name} |
| 98 | + </span> |
| 99 | + |
| 100 | + {/* Show input when renaming */} |
| 101 | + <input {...api.getNodeRenameInputProps({ node, indexPath })} /> |
| 102 | + </div> |
| 103 | + ) |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Programmatic API |
| 108 | + |
| 109 | +```tsx |
| 110 | +// Start renaming a node |
| 111 | +api.startRenaming(nodeValue) |
| 112 | + |
| 113 | +// Submit rename with new label |
| 114 | +api.submitRenaming(nodeValue, newLabel) |
| 115 | + |
| 116 | +// Cancel renaming |
| 117 | +api.cancelRenaming() |
| 118 | +``` |
| 119 | + |
| 120 | +## Node State & Styling |
| 121 | + |
| 122 | +The `nodeState` now includes a `renaming` property to track rename mode: |
| 123 | + |
| 124 | +```tsx |
| 125 | +const nodeState = api.getNodeState({ node, indexPath }) |
| 126 | +// nodeState.renaming -> boolean |
| 127 | +``` |
| 128 | + |
| 129 | +Both `getItemProps` and `getBranchControlProps` now include a `data-renaming` attribute for styling: |
| 130 | + |
| 131 | +```css |
| 132 | +/* Style items being renamed */ |
| 133 | +[data-part="item"][data-renaming] { |
| 134 | + outline: 2px solid blue; |
| 135 | +} |
| 136 | + |
| 137 | +/* Style branch controls being renamed */ |
| 138 | +[data-part="branch-control"][data-renaming] { |
| 139 | + background: rgba(0, 0, 255, 0.1); |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +## Use Cases Unlocked |
| 144 | + |
| 145 | +1. **File Explorers** - Allow users to rename files and folders with validation |
| 146 | +2. **Content Management** - Edit page titles, categories, or navigation items in-place |
| 147 | +3. **Folder Organization** - Rename folders with duplicate prevention |
| 148 | +4. **Project Management** - Edit task names, project hierarchies |
| 149 | +5. **Knowledge Bases** - Rename articles, sections, or categories inline |
| 150 | + |
| 151 | +## Example: File Explorer with Smart Validation |
| 152 | + |
| 153 | +```tsx |
| 154 | +useMachine(tree.machine, { |
| 155 | + collection, |
| 156 | + |
| 157 | + canRename: (node, indexPath) => { |
| 158 | + // Prevent renaming system files |
| 159 | + if (node.system) return false |
| 160 | + // Prevent renaming locked files |
| 161 | + if (node.locked) return false |
| 162 | + // Only allow renaming files, not folders |
| 163 | + return !node.children |
| 164 | + }, |
| 165 | + |
| 166 | + onBeforeRename: (details) => { |
| 167 | + // Note: details.label is already trimmed |
| 168 | + |
| 169 | + // Check file extension rules |
| 170 | + if (!details.label.includes(".")) { |
| 171 | + console.error("File must have an extension") |
| 172 | + return false |
| 173 | + } |
| 174 | + |
| 175 | + // Validate file name characters |
| 176 | + if (/[<>:"/\\|?*]/.test(details.label)) { |
| 177 | + console.error("Invalid characters in filename") |
| 178 | + return false |
| 179 | + } |
| 180 | + |
| 181 | + return true |
| 182 | + }, |
| 183 | + |
| 184 | + onRenameComplete: (details) => { |
| 185 | + // Update collection and sync to backend |
| 186 | + const node = collection.at(details.indexPath) |
| 187 | + const updatedCollection = collection.replace(details.indexPath, { |
| 188 | + ...node, |
| 189 | + name: details.label, |
| 190 | + lastModified: new Date(), |
| 191 | + }) |
| 192 | + setCollection(updatedCollection) |
| 193 | + |
| 194 | + // Sync to server |
| 195 | + api.renameFile(details.value, details.label) |
| 196 | + }, |
| 197 | +}) |
| 198 | +``` |
0 commit comments