Skip to content
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

Merged
merged 9 commits into from
Feb 1, 2023

Conversation

mdperez86
Copy link
Contributor

@mdperez86 mdperez86 commented Jan 14, 2023

All Submissions:

Changes proposed in this Pull Request:

Depends on #36432
Partially addresses #35851

This PR adds expand/collapse functionality to the TreeControl component.

Screenshot 2023-01-27 at 11 55 53

  • This PR is a very minor change/addition and does not require testing instructions (if checked you can ignore/remove the next section).

How to test the changes in this Pull Request:

  1. Build storybook (pnpm --filter=@woocommerce/storybook build-storybook)
  2. Run storybook (pnpm --filter=@woocommerce/storybook storybook)
  3. Visit http://localhost:6007/?path=/story/woocommerce-admin-experimental-treecontrol--expand-on-search
  4. A basic tree control should be shown.
  5. If you click the expander button at the end of the item the subtree should expand/collapse.
  6. If you type in the text control above the tree should expand to show the matching children.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you created a changelog file for each project being changed, ie pnpm --filter=<project> changelog add?

FOR PR REVIEWER ONLY:

  • I have reviewed that everything is sanitized/escaped appropriately for any SQL or XSS injection possibilities. I made sure Linting is not ignored or disabled.

@mdperez86 mdperez86 added focus: product management Related to product creation and editing. focus: new product ux revamped product management experience labels Jan 14, 2023
@mdperez86 mdperez86 requested a review from a team January 14, 2023 01:46
@mdperez86 mdperez86 self-assigned this Jan 14, 2023
@github-actions github-actions bot added the package: @woocommerce/components issues related to @woocommerce/components label Jan 14, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jan 14, 2023

Test Results Summary

Commit SHA: 34e6d86

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202611m 15s
E2E Tests189006019517m 29s

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.

@mdperez86 mdperez86 marked this pull request as ready for review January 17, 2023 13:27
@mdperez86 mdperez86 force-pushed the add/35851-tree-control-expander branch from ff04deb to 49c15b8 Compare January 17, 2023 14:08
@mattsherman mattsherman assigned mattsherman and unassigned mdperez86 Jan 19, 2023
@mattsherman mattsherman force-pushed the add/35851-tree-control-expander branch from 49c15b8 to baa3cb5 Compare January 19, 2023 20:26
@mattsherman mattsherman mentioned this pull request Jan 24, 2023
7 tasks
@mattsherman mattsherman added the status: blocked The issue is blocked from progressing, waiting for another piece of work to be done. label Jan 24, 2023
@mattsherman mattsherman removed the status: blocked The issue is blocked from progressing, waiting for another piece of work to be done. label Jan 27, 2023
@mattsherman mattsherman force-pushed the add/35851-tree-control-expander branch from baa3cb5 to e4572ac Compare January 27, 2023 16:58
Copy link
Contributor

@joshuatf joshuatf left a 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(
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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 ) );
Copy link
Contributor

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 ) {
Copy link
Contributor

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).

Suggested change
function isItemExpanded( item: LinkedTree, text: string ) {
function shouldItemExpand( item: LinkedTree, query: string ) {

@mattsherman
Copy link
Contributor

@joshuatf I've address your suggestions and renamed the expanded-related props.

I'm on the fence regarding the use of useCallback in the story example.

joshuatf
joshuatf previously approved these changes Feb 1, 2023
Copy link
Contributor

@joshuatf joshuatf left a 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 🎉

Copy link
Contributor

@joshuatf joshuatf left a 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!

@mattsherman mattsherman merged commit 5570f6a into trunk Feb 1, 2023
@mattsherman mattsherman deleted the add/35851-tree-control-expander branch February 1, 2023 17:04
@github-actions github-actions bot added this to the 7.5.0 milestone Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus: new product ux revamped product management experience focus: product management Related to product creation and editing. package: @woocommerce/components issues related to @woocommerce/components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants