Skip to content

Commit

Permalink
[Table] Move focus cell to (0,0) on FULL_TABLE selection (#1172)
Browse files Browse the repository at this point in the history
* Move focus cell to (0,0) on FULL_TABLE selection

* Add test
  • Loading branch information
cmslewis committed May 31, 2017
1 parent afe2f62 commit 2fe81ad
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
19 changes: 1 addition & 18 deletions packages/table/src/interactions/selectable.tsx
Expand Up @@ -84,23 +84,6 @@ export interface IDragSelectableProps extends ISelectableProps {

@PureRender
export class DragSelectable extends React.Component<IDragSelectableProps, {}> {
private static getFocusCellCoordinatesFromRegion(region: IRegion) {
const regionCardinality = Regions.getRegionCardinality(region);

switch (regionCardinality) {
case RegionCardinality.FULL_TABLE:
return { col: 0, row: 0 };
case RegionCardinality.FULL_COLUMNS:
return { col: region.cols[0], row: 0 };
case RegionCardinality.FULL_ROWS:
return { col: 0, row: region.rows[0] };
case RegionCardinality.CELLS:
return { col: region.cols[0], row: region.rows[0] };
default:
return null;
}
}

private didExpandSelectionOnActivate = false;

public render() {
Expand Down Expand Up @@ -138,7 +121,7 @@ export class DragSelectable extends React.Component<IDragSelectableProps, {}> {
selectedRegionTransform,
} = this.props;

const focusCellCoordinates = DragSelectable.getFocusCellCoordinatesFromRegion(region);
const focusCellCoordinates = Regions.getFocusCellCoordinatesFromRegion(region);
this.props.onFocus(focusCellCoordinates);

if (selectedRegionTransform != null) {
Expand Down
17 changes: 17 additions & 0 deletions packages/table/src/regions.ts
Expand Up @@ -152,6 +152,23 @@ export class Regions {
}
}

public static getFocusCellCoordinatesFromRegion(region: IRegion) {
const regionCardinality = Regions.getRegionCardinality(region);

switch (regionCardinality) {
case RegionCardinality.FULL_TABLE:
return { col: 0, row: 0 };
case RegionCardinality.FULL_COLUMNS:
return { col: region.cols[0], row: 0 };
case RegionCardinality.FULL_ROWS:
return { col: 0, row: region.rows[0] };
case RegionCardinality.CELLS:
return { col: region.cols[0], row: region.rows[0] };
default:
return null;
}
}

/**
* Returns a region containing one or more cells.
*/
Expand Down
4 changes: 4 additions & 0 deletions packages/table/src/table.tsx
Expand Up @@ -620,6 +620,10 @@ export class Table extends AbstractComponent<ITableProps, ITableState> {
// clicking on upper left hand corner sets selection to "all"
// regardless of current selection state (clicking twice does not deselect table)
selectionHandler([Regions.table()]);

// move the focus cell to the top left
const newFocusedCellCoordinates = Regions.getFocusCellCoordinatesFromRegion(RegionCardinality.FULL_TABLE);
this.handleFocus(newFocusedCellCoordinates);
}

private handleSelectAllHotkey = (e: KeyboardEvent) => {
Expand Down
8 changes: 7 additions & 1 deletion packages/table/test/tableTests.tsx
Expand Up @@ -143,11 +143,14 @@ describe("<Table>", () => {
expect(table.state.rowHeights[0]).to.equal(MAX_HEIGHT);
});

it("Selects all on click of upper-left corner", () => {
it("Selects all and moves focus cell to (0, 0) on click of upper-left corner", () => {
const onFocus = sinon.spy();
const onSelection = sinon.spy();

const table = harness.mount(
<Table
enableFocus={true}
onFocus={onFocus}
onSelection={onSelection}
numRows={10}
>
Expand All @@ -156,9 +159,12 @@ describe("<Table>", () => {
<Column renderCell={renderCell}/>
</Table>,
);

const menu = table.find(`.${Classes.TABLE_MENU}`);
menu.mouse("click");

expect(onSelection.args[0][0]).to.deep.equal([Regions.table()]);
expect(onFocus.args[0][0]).to.deep.equal({ col: 0, row: 0 });
});

describe("Resizing", () => {
Expand Down

0 comments on commit 2fe81ad

Please sign in to comment.