fix(tree-node-web): keep sort order and expansion state on refresh - #2399
Open
davidofsky wants to merge 2 commits into
Open
fix(tree-node-web): keep sort order and expansion state on refresh#2399davidofsky wants to merge 2 commits into
davidofsky wants to merge 2 commits into
Conversation
The infinite-depth tree (`Parent association`) builds its node map incrementally. A data-source refresh never re-ordered siblings: reordering items through a sequence attribute stayed invisible until the page was reopened. The same hook also collapsed the whole tree on any refresh. Expansion state is now remembered per item id and restored when a node is recreated, and a loading data source no longer clears the tree.
Collaborator
|
linked to WC-3557 |
In useInfiniteTreeNode.ts: expanding a node only pre-loaded the children that happened to be in the datasource at that moment, so a child added later (or arriving in a later batch) was never pre-loaded and its parent lost the expand chevron below the second level. Now the hook keeps a set of expanded node ids (expandedIdsRef) and, on every datasource update, pre-loads any new item whose parent is in that set, so every visible node knows whether it has children
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull request type
Bug fix (non-breaking change which fixes an issue)
Description
Two bugs in the infinite-depth Tree node (the
Parent associationconfiguration added in3.11.0, i.e. the
TreeNodeV2code path). Both are caused byuseIncrementalTreeData,which builds its node map incrementally and reuses nodes across data source updates.
1. A new sort order is never applied
A node is appended to
rootsRef/parent.childrenonly the first time its id is seen.On later updates an already-known node takes the "already exists" branch, which refreshes
item,titleandparentIdbut never re-orders the sibling arrays. So when the datasource returns the same items in a different order. For example after a microflow changes
a sequence attribute that the data source sorts on, the tree keeps the order it captured
on first load. The new order only becomes visible when the page is reopened and the widget
remounts.
Fixed by re-applying the data source order on every update, for the root nodes and for each node's children
2. Every node collapses when the data source refreshes
On a refresh the widget throws its tree away and rebuilds it, and rebuilt nodes always
start out collapsed. Three things trigger that rebuild: the data source being mid-load
(
datasource.itemsisundefined, which the hook read as "all items removed"), the clienthanding over new prop instances (
isConfigChangedcompares them by reference, so thishappens on every refresh), and any single item being deleted. While loading, the tree also
briefly showed "No data available".
Fixed by keeping the current tree while the data source is loading, and by remembering
each node's expanded/collapsed state so a rebuilt node gets its state back.
The v1 code path (nested Tree node widgets, no parent association) rebuilds from
datasource.itemson every update and was never affected.7 unit tests added to
useIncrementalTreeData.spec.ts. 3 for sibling/root re-orderingand 4 for expansion state surviving a reload, config churn, an item removal, and a
user-collapsed node with
startExpandedon. All 7 fail onmainand pass with thischange. Full package suite: 69 tests passing,
pnpm lintclean.What should be covered while testing?
Requires a Tree node configured with Parent association (self-referencing
association), so the infinite-depth code path is used.
Sort order
expanded nodes stay expanded.
Expansion state on refresh
Refresh entity / a microflow that only reloads the list.
"No data available" while reloading, and no loading spinners remain on the chevrons.
Regression checks
Start expandedon and off, on first load, still behaves as before.