-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tree-control expand/collapse on click the expander button or by a custom logic #36434
Conversation
Test Results SummaryCommit SHA: 34e6d86
To view the full API test report, click here. To view the full E2E test report, click here. To view all test reports, visit the WooCommerce Test Reports Dashboard. |
ff04deb
to
49c15b8
Compare
49c15b8
to
baa3cb5
Compare
baa3cb5
to
e4572ac
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are awesome hooks and very nicely isolated! Left a few very nitpicky naming suggestions and a suggestion on premature optimization.
<TreeControl | ||
id="expand-on-search" | ||
items={ listItems } | ||
isItemExpanded={ useCallback( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this useCallback
offers any optimization and in fact may worsen performance.
The only variable coming from outside this function is text
which also is a dependency of the callback, so memoization of this function increases space complexity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that useCallback
is useful here. Otherwise, every time the parent of TreeControl
was re-rendered, TreeControl
would be re-rendered because the function passed into isItemExpanded
would be re-created.
The function passed in is doing recursive checking of children items to see if an item should be expanded, so it seems potentially expensive if there is a large tree.
In this particular story, I don't think it really makes any noticeable difference though.
I could do either way, either keeping useCallback
or getting rid of it for simplicity in this particular example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently in this example, the only time this should be re-rendered is when text
is changed, right? We would also want to re-render in that case and it does invalidate the memoization on the callback.
I think for this story (and most) the performance changes are going to be negligible either way, but since this decreases code readability and these stories act as some of the primary example for 3PDs, erring on the side of simplicity is best IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Simplicity is the way to go. I'll update this.
|
||
useEffect( () => { | ||
if ( item.children?.length && typeof isExpanded === 'function' ) { | ||
setExpanded( isExpanded( item ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make this a bit more clear and avoid ambiguity, maybe we could update the names slightly.
expanded -> isExpanded (boolean)
isExpanded -> shouldExpand (function)
@@ -36,6 +36,36 @@ export const SimpleTree: React.FC = () => { | |||
); | |||
}; | |||
|
|||
function isItemExpanded( item: LinkedTree, text: string ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment here re: is/should
. I also think text
might be better as query
if I'm understanding what this is correctly (user search input).
function isItemExpanded( item: LinkedTree, text: string ) { | |
function shouldItemExpand( item: LinkedTree, query: string ) { |
@joshuatf I've address your suggestions and renamed the expanded-related props. I'm on the fence regarding the use of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the discussion on this issue! Changes look good and still testing well 🎉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good, thanks for the update!
All Submissions:
Changes proposed in this Pull Request:
Depends on #36432
Partially addresses #35851
This PR adds expand/collapse functionality to the
TreeControl
component.How to test the changes in this Pull Request:
pnpm --filter=@woocommerce/storybook build-storybook
)pnpm --filter=@woocommerce/storybook storybook
)http://localhost:6007/?path=/story/woocommerce-admin-experimental-treecontrol--expand-on-search
Other information:
pnpm --filter=<project> changelog add
?FOR PR REVIEWER ONLY: