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] Move focus cell to (0,0) on FULL_TABLE selection #1172

Merged
merged 2 commits into from May 31, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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