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

Expose scrollTo and scrollToItem on imperative ref #6042

Merged
merged 9 commits into from
Jul 12, 2022

Conversation

weltenwort
Copy link
Member

@weltenwort weltenwort commented Jul 11, 2022

📝 Summary

This exposes the scrollTo() and scrollToItem() methods on the imperative API of the grid component. It's a break-out PR from #6028 (#6024).

Checklist

  • Checked in both light and dark modes
  • Checked in mobile
  • Checked in Chrome, Safari, Edge, and Firefox
  • Props have proper autodocs and playground toggles
  • Added documentation
  • Checked Code Sandbox works for any docs examples
  • Added or updated jest and cypress tests
  • Checked for breaking changes and labeled appropriately
  • Checked for accessibility including keyboard-only and screenreader modes
  • A changelog entry exists and is marked appropriately

@weltenwort weltenwort marked this pull request as draft July 11, 2022 15:59
@cee-chen
Copy link
Contributor

@weltenwort this looks great to me! Any objections if I push up Cypress tests to your branch for these 2 new APIs?

@weltenwort
Copy link
Member Author

weltenwort commented Jul 11, 2022

I accidentally submitted this too early and have since reverted it to a draft because I wanted to add tests. 🙈 Any help with those would be appreciated.

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6042/

@cee-chen
Copy link
Contributor

cee-chen commented Jul 11, 2022

Yes, for sure! You'll want ref.spec.tsx and to add a new describe block for each API. The grid in this example is already virtualized and has lots of room for scrolling, which is nice, because we won't have to set that up manually. You should be able to do something like:

  describe('scrollToItem', () => {
    it('scrolls to a specific cell position, rendering the cell', () => {
      cy.get('[data-gridcell-row-index="90"]']).should('not.exist');
      ref.current.scrollToItem({ rowIndex: 90, columnIndex: 5 });
      cy.get('[data-gridcell-row-index="90"]']).should('exist');
    });
  });

  describe('scrollTo', () => {
    it('scrolls the grid to a specified position', () => {
      cy.get('.euiDataGrid__virtualized']).its('scrollY').should('equal', 0);
      ref.current.scrollTo({ scrollTop: 500, scrollLeft: 0 });
      cy.get('.euiDataGrid__virtualized']).its('scrollY').should('equal', 500);
    });
  });

I didn't actually run the above so it's possible I have some syntax incorrect - feel free to bug me if so!

Also, I can't remember if the Cypress docs mention it, but the CLI command you'll want to run/test this locally is yarn test-cypress-dev --skip-css

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6042/

Comment on lines 82 to 102
<li>
<p>
<EuiCode>
scrollTo({'{ scrollLeft: number, scrollTop: number }'})
</EuiCode>{' '}
- scrolls the grid to the specified horizontal and vertical
pixel offsets.
</p>
</li>
<li>
<p>
<EuiCode>
scrollToItem(
{
'{align: string = "auto", columnIndex?: number, rowIndex?: number }'
}
)
</EuiCode>{' '}
- scrolls the grid to the specified row and columns indices
</p>
</li>
Copy link
Contributor

@cee-chen cee-chen Jul 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple quick documentation notes:

  1. I think we should link to react-window's docs (https://react-window.vercel.app/#/api/FixedSizeGrid#methods) rather than write our own - in general since we're just passing their methods as-is, their docs will always be more up to date than ours.

  2. I'm also tempted to nest another <ul> titled react-window methods (or similar) for this section - this is important because of how EUI's ref APIs handle rowIndex vs how react-window's APIs handle rowIndex. See the below callout:

EUI expects the rowIndex received from the original unsorted/unpaginated consumer data. react-window does not care at all about that and wants what EUI refers to as the visibleRowIndex (i.e., the row index after sorting and pagination). This is a confusing but important difference that could potentially bite consumers if not clarified, and I think it's worth separating out react-window's ref APIs vs EUI's ref APIs for that reason.

@chandlerprall any thoughts here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a bit of poking, I think this is how I'd prefer to separate the API documentation for clarity:

LMK what y'all think 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also love to update src-docs/src/views/datagrid/advanced/ref.tsx with a Scroll to cell button that uses scrollToItem, which I think will help make it a bit clearer to users the difference between the two APIs on a sorted grid!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separating the new methods into their own list makes sense to me. I think it draws a good distinction between the logic EUI provides vs. these pass-through functions. That would also provide a natural place to link to react-window's docs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll split the react-window API into its own section. Any opinion which section should own the example? Should it belong to the "Ref methods" or "react-window methods" section?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it after the second section for now since it's pretty short. Let me know what you think.

@weltenwort weltenwort self-assigned this Jul 11, 2022
@weltenwort weltenwort marked this pull request as ready for review July 11, 2022 18:13
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6042/

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6042/

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6042/

@cee-chen
Copy link
Contributor

Looks fantastic, thanks so much @weltenwort! I have a couple super minor docs comments/tweaks that I'll go ahead and push up to your branch since I'm a half-day timezone away from you (and very slow in the mornings 😅) and don't want to delay your PR another full day. Excited to see the next PR with RowHeightUtils test improvements, that looked really cool from what I saw in the original PR!

cee-chen added 2 commits July 12, 2022 12:52
- combine sections and use EuiTitle tag for react-window methods

- Fix link to react-window docs

- minor grammar tweaks
@cee-chen cee-chen enabled auto-merge (squash) July 12, 2022 20:14
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6042/

@cee-chen cee-chen merged commit cb937f4 into elastic:main Jul 12, 2022
@weltenwort
Copy link
Member Author

weltenwort commented Jul 13, 2022

@constancecchen thanks so much for helping me with the docs ❤️ and your cypress snippets got me started very quickly

Excited to see the next PR with RowHeightUtils test improvements

sure thing, I'll try to submit it today I submitted those in #6050

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_6042/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants