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

[Table] Newly focused cell after keyboard navigation is now transformed #2988

Conversation

ntamas
Copy link
Contributor

@ntamas ntamas commented Oct 1, 2018

Fixes #2871

Changes proposed in this pull request:

This pull request implements a change to the Table component that passes the newly focused cell through the selectedRegionTransform function of the table if it is present. This is to ensure that invariants enforced in the selectedRegionTransform function (e.g., "only whole rows should be selected") are not violated after the user navigates to another cell with the keyboard.

Reviewers should focus on:

Please check my changes made to the handleFocusMove() method of the Table component. I have updated the table dev app as well to make this change easier to test.

@palantirtech
Copy link
Member

Thanks for your interest in palantir/blueprint, @ntamas! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@ntamas ntamas changed the title Newly focused cell after keyboard navigation is now transformed [Table] Newly focused cell after keyboard navigation is now transformed Oct 1, 2018
Copy link
Contributor

@giladgray giladgray left a comment

Choose a reason for hiding this comment

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

@@ -1826,6 +1826,11 @@ export class Table extends AbstractComponent<ITableProps, ITableState> {

// change selection to match new focus cell location
const newSelectionRegions = [Regions.cell(newFocusedCell.row, newFocusedCell.col)];
const { selectedRegionTransform } = this.props;
if (selectedRegionTransform != null) {
// tslint:disable-next-line no-unnecessary-callback-wrapper
Copy link
Contributor

Choose a reason for hiding this comment

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

do we have to disable this? if so, leave another //comment explaining why.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are correct -- there is no need to disable this. Shall I add another commit to this PR to remove this line and will you squash it if it gets merged eventually?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes add commits. do not force push on PRs.

const { selectedRegionTransform } = this.props;
if (selectedRegionTransform != null) {
// tslint:disable-next-line no-unnecessary-callback-wrapper
newSelectionRegions.forEach(region => selectedRegionTransform(region, undefined));
Copy link
Contributor

Choose a reason for hiding this comment

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

why the explicit undefined? i'm not familiar with this code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

selectedRegionTransform() is a function that takes a region and a mouse event that triggered the selection. In this case, there is no mouse event so the best I could do is to pass undefined as the second argument. (I did not want to mess around with the typing of selectedRegionTransform as that would be an API breakage). Actually, I was surprised to see that TypeScript accepts undefined for an argument that is typed as a MouseEvent.

Also, strangely enough, TypeScript complains if I omit the second argument ("Expected 2-3 arguments, got 1"), so that's why we need an explicit undefined here.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah this is not valid. it only accepts undefined because we have not enabled
strictNullChecks compiler options.

@ntamas
Copy link
Contributor Author

ntamas commented Oct 2, 2018

I have added the auth token after I have realized that CircleCI failed the checks and I have re-ran the pipeline on CircleCI. Is there anything else I need to do?

this.handleSelection(newSelectionRegions);
const transformedSelectionRegions =
selectedRegionTransform != null
? newSelectionRegions.map(region => selectedRegionTransform(region, undefined))
Copy link
Contributor

Choose a reason for hiding this comment

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

this undefined breaks the type contract. blocker for this feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Might be overkill but would it be acceptable to create a fake MouseEvent that simulates a click on the middle of the cell that the focus is being moved to?

Copy link
Contributor

Choose a reason for hiding this comment

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

this code is inside an event handler so we do have an event, it's just not a MouseEvent. best solution may be to change type to MouseEvent | FocusEvent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have a KeyboardEvent there, not a FocusEvent, but I see your point. That would be a major version bump, though - so this has to wait until Blueprint 4, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

oh right my bad it is a keyboard event.

honestly this table library will never have a v4. we're working on a complete rewrite internally that will replace this implementation completely. it's going to be so much better but also quite different.

Copy link
Contributor

Choose a reason for hiding this comment

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

i'm willing to merge this change and declare it a bug fix, not an API break. makes sense for keyboard events to go through this flow just like mouse events. typescript users will see compile errors if they're relying on MouseEvent properties so at least it won't be silent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the typing of ISelectedRegionTransform in 6a35c0a - this version now also forwards the KeyboardEvent to the region transform function.

@@ -1829,7 +1829,7 @@ export class Table extends AbstractComponent<ITableProps, ITableState> {
const { selectedRegionTransform } = this.props;
const transformedSelectionRegions =
selectedRegionTransform != null
? newSelectionRegions.map(region => selectedRegionTransform(region, undefined))
? newSelectionRegions.map(region => selectedRegionTransform(region, e))
Copy link
Contributor

Choose a reason for hiding this comment

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

do we have ICoordinateData for the third argument? perhaps newFocusedCell? (i'm not looking at the code actively)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ICoordinateData is defined in src/interactions/draggable.tsx and it seems to hold data that is relevant only for dragging gestures. The only time when this argument is used at the moment is in handleDragMove() in src/interactions/selectable.tsx. I would say that it is not relevant here.

Copy link
Contributor

Choose a reason for hiding this comment

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

nice thank you

Copy link
Contributor

@giladgray giladgray left a comment

Choose a reason for hiding this comment

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

looks good! thanks @ntamas

@giladgray giladgray merged commit 9daee54 into palantir:develop Oct 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants