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

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions packages/table-dev-app/src/mutableTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export enum CellContent {
LARGE_JSON = "large-json",
}

export enum SelectedRegionTransformPreset {
CELL = "cell",
ROW = "row",
COLUMN = "column",
}

type IMutableStateUpdateCallback = (
stateKey: keyof IMutableTableState,
) => ((event: React.FormEvent<HTMLElement>) => void);
Expand All @@ -77,6 +83,12 @@ const REGION_CARDINALITIES: RegionCardinality[] = [

const RENDER_MODES: RenderMode[] = [RenderMode.BATCH_ON_UPDATE, RenderMode.BATCH, RenderMode.NONE];

const SELECTION_MODES: SelectedRegionTransformPreset[] = [
SelectedRegionTransformPreset.CELL,
SelectedRegionTransformPreset.ROW,
SelectedRegionTransformPreset.COLUMN,
];

const CELL_CONTENTS: CellContent[] = [
CellContent.EMPTY,
CellContent.CELL_NAMES,
Expand Down Expand Up @@ -183,6 +195,16 @@ function contains(arr: any[], value: any) {
return arr.indexOf(value) >= 0;
}

function enforceWholeColumnSelection(region: IRegion) {
delete region.rows;
return region;
}

function enforceWholeRowSelection(region: IRegion) {
delete region.cols;
return region;
}

export interface IMutableTableState {
cellContent?: CellContent;
cellTruncatedPopoverMode?: TruncatedPopoverMode;
Expand Down Expand Up @@ -214,6 +236,7 @@ export interface IMutableTableState {
scrollToRegionType?: RegionCardinality;
scrollToRowIndex?: number;
selectedFocusStyle?: FocusStyle;
selectedRegionTransformPreset?: SelectedRegionTransformPreset;
showCallbackLogs?: boolean;
showCellsLoading?: boolean;
showColumnHeadersLoading?: boolean;
Expand Down Expand Up @@ -259,6 +282,7 @@ const DEFAULT_STATE: IMutableTableState = {
scrollToRegionType: RegionCardinality.CELLS,
scrollToRowIndex: 0,
selectedFocusStyle: FocusStyle.TAB,
selectedRegionTransformPreset: SelectedRegionTransformPreset.CELL,
showCallbackLogs: true,
showCellsLoading: false,
showColumnHeadersLoading: false,
Expand Down Expand Up @@ -377,6 +401,7 @@ export class MutableTable extends React.Component<{}, IMutableTableState> {
ref={this.refHandlers.table}
renderMode={this.state.renderMode}
rowHeaderCellRenderer={this.renderRowHeader}
selectedRegionTransform={this.getSelectedRegionTransform()}
selectionModes={this.getEnabledSelectionModes()}
styledRegionGroups={this.getStyledRegionGroups()}
>
Expand Down Expand Up @@ -577,6 +602,13 @@ export class MutableTable extends React.Component<{}, IMutableTableState> {
this.toRenderModeLabel,
this.handleNumberStateChange,
);
const selectedRegionTransformPresetMenu = this.renderSelectMenu(
"Selection",
"selectedRegionTransformPreset",
SELECTION_MODES,
this.toSelectedRegionTransformPresetLabel,
this.handleSelectedRegionTransformPresetChange,
);
const cellContentMenu = this.renderSelectMenu(
"Cell content",
"cellContent",
Expand Down Expand Up @@ -617,6 +649,7 @@ export class MutableTable extends React.Component<{}, IMutableTableState> {
{this.renderSwitch("Callback logs", "showCallbackLogs")}
{this.renderSwitch("Full-table selection", "enableFullTableSelection")}
{this.renderSwitch("Multi-selection", "enableMultiSelection")}
{selectedRegionTransformPresetMenu}
<H6>Scroll to</H6>
{this.renderScrollToSection()}

Expand Down Expand Up @@ -855,6 +888,19 @@ export class MutableTable extends React.Component<{}, IMutableTableState> {
}
}

private toSelectedRegionTransformPresetLabel(selectedRegionTransformPreset: SelectedRegionTransformPreset) {
switch (selectedRegionTransformPreset) {
case SelectedRegionTransformPreset.CELL:
return "Unconstrained";
case SelectedRegionTransformPreset.ROW:
return "Whole rows only";
case SelectedRegionTransformPreset.COLUMN:
return "Whole columns only";
default:
return "None";
}
}

private toCellContentLabel(cellContent: CellContent) {
switch (cellContent) {
case CellContent.CELL_NAMES:
Expand Down Expand Up @@ -1043,6 +1089,10 @@ export class MutableTable extends React.Component<{}, IMutableTableState> {
return handleNumberChange(value => this.setState({ [stateKey]: value }));
};

private handleSelectedRegionTransformPresetChange = (stateKey: keyof IMutableTableState) => {
return handleStringChange(value => this.setState({ [stateKey]: value }));
};

private updateFocusStyleState = () => {
return handleStringChange((value: string) => {
const selectedFocusStyle = value === "tab" ? FocusStyle.TAB : FocusStyle.TAB_OR_CLICK;
Expand Down Expand Up @@ -1095,6 +1145,22 @@ export class MutableTable extends React.Component<{}, IMutableTableState> {
return loadingOptions;
}

private getSelectedRegionTransform() {
switch (this.state.selectedRegionTransformPreset) {
case SelectedRegionTransformPreset.CELL:
return undefined;

case SelectedRegionTransformPreset.ROW:
return enforceWholeRowSelection;

case SelectedRegionTransformPreset.COLUMN:
return enforceWholeColumnSelection;

default:
return undefined;
}
}

private getStyledRegionGroups() {
// show 3 styled regions as samples
return !this.state.showCustomRegions
Expand Down
7 changes: 6 additions & 1 deletion packages/table/src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,12 @@ export class Table extends AbstractComponent<ITableProps, ITableState> {

// change selection to match new focus cell location
const newSelectionRegions = [Regions.cell(newFocusedCell.row, newFocusedCell.col)];
this.handleSelection(newSelectionRegions);
const { selectedRegionTransform } = this.props;
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.

: newSelectionRegions;
this.handleSelection(transformedSelectionRegions);
this.handleFocus(newFocusedCell);

// keep the focused cell in view
Expand Down