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 all 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
6 changes: 5 additions & 1 deletion packages/table/src/interactions/selectable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { IRegion, Regions } from "../regions";
import { DragEvents } from "./dragEvents";
import { Draggable, ICoordinateData, IDraggableProps } from "./draggable";

export type ISelectedRegionTransform = (region: IRegion, event: MouseEvent, coords?: ICoordinateData) => IRegion;
export type ISelectedRegionTransform = (
region: IRegion,
event: MouseEvent | KeyboardEvent,
coords?: ICoordinateData,
) => IRegion;

export interface ISelectableProps {
/**
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, 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

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

// keep the focused cell in view
Expand Down