( state: TreeState<T>, id: string )
| 152 | }; |
| 153 | } |
| 154 | function toggleAllChildren<T extends { id: string; children?: T[] }>( |
| 155 | state: TreeState<T>, |
| 156 | id: string |
| 157 | ): TreeState<T> { |
| 158 | const item = state.items.find(({ id: nodeId }) => nodeId === id); |
| 159 | |
| 160 | if (!item) { |
| 161 | return state; |
| 162 | } |
| 163 | |
| 164 | if (!item.node.children || item.node.children.length === 0) { |
| 165 | return state; |
| 166 | } |
| 167 | |
| 168 | const allCollapsed = item.node.children.every( |
| 169 | (child) => state.collapsedState[child.id] |
| 170 | ); |
| 171 | |
| 172 | if (allCollapsed) { |
| 173 | const collapsedState = item.node.children.reduce( |
| 174 | (acc, child) => ({ |
| 175 | ...acc, |
| 176 | [child.id]: false, |
| 177 | }), |
| 178 | state.collapsedState |
| 179 | ); |
| 180 | |
| 181 | return { |
| 182 | ...state, |
| 183 | collapsedState, |
| 184 | items: createNodeItems(state.nodes, 0, collapsedState), |
| 185 | focusedNodeId: id, |
| 186 | }; |
| 187 | } |
| 188 | |
| 189 | const collapsedState = item.node.children.reduce( |
| 190 | (acc, child) => ({ |
| 191 | ...acc, |
| 192 | [child.id]: true, |
| 193 | }), |
| 194 | state.collapsedState |
| 195 | ); |
| 196 | |
| 197 | return { |
| 198 | ...state, |
| 199 | collapsedState, |
| 200 | items: createNodeItems(state.nodes, 0, collapsedState), |
| 201 | focusedNodeId: id, |
| 202 | }; |
| 203 | } |
| 204 | |
| 205 | export function useVirtualTree<T extends { id: string; children?: T[] }, R>( |
| 206 | options: UseVirtualTreeOptions<T, R> |
no test coverage detected