-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Table] Show focus cell appropriately when enableFocus becomes true (#…
…1447) * Show focus cell appropriately when enableFocus becomes true * Add focusUtils.ts * Rename file, incorporate, add first test * Write more tests * Oops, delete .only * Add missing file header
- Loading branch information
Showing
5 changed files
with
150 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright 2017 Palantir Technologies, Inc. All rights reserved. | ||
* Licensed under the BSD-3 License as modified (the “License”); you may obtain a copy | ||
* of the license at https://github.com/palantir/blueprint/blob/master/LICENSE | ||
* and https://github.com/palantir/blueprint/blob/master/PATENTS | ||
*/ | ||
|
||
import { IRegion, Regions } from "../../regions"; | ||
import { IFocusedCellCoordinates } from "../cell"; | ||
|
||
/** | ||
* Returns the proper focused cell for the given set of initial conditions. | ||
*/ | ||
export function getInitialFocusedCell( | ||
enableFocus: boolean, | ||
focusedCellFromProps: IFocusedCellCoordinates, | ||
focusedCellFromState: IFocusedCellCoordinates, | ||
selectedRegions: IRegion[], | ||
): IFocusedCellCoordinates { | ||
if (!enableFocus) { | ||
return undefined; | ||
} else if (focusedCellFromProps != null) { | ||
// controlled mode | ||
return focusedCellFromProps; | ||
} else if (focusedCellFromState != null) { | ||
// use the current focused cell from state | ||
return focusedCellFromState; | ||
} else if (selectedRegions.length > 0) { | ||
// focus the top-left cell of the first selection | ||
return { | ||
...Regions.getFocusCellCoordinatesFromRegion(selectedRegions[0]), | ||
focusSelectionIndex: 0, | ||
}; | ||
} else { | ||
// focus the top-left cell of the table | ||
return { col: 0, row: 0, focusSelectionIndex: 0 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/table/test/common/internal/focusCellUtilsTests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* Copyright 2017 Palantir Technologies, Inc. All rights reserved. | ||
* Licensed under the BSD-3 License as modified (the “License”); you may obtain a copy | ||
* of the license at https://github.com/palantir/blueprint/blob/master/LICENSE | ||
* and https://github.com/palantir/blueprint/blob/master/PATENTS | ||
*/ | ||
|
||
import { expect } from "chai"; | ||
|
||
import { IFocusedCellCoordinates } from "../../../src/common/cell"; | ||
import * as FocusedCellUtils from "../../../src/common/internal/focusedCellUtils"; | ||
import { Regions } from "../../../src/regions"; | ||
|
||
describe("focusCellUtils", () => { | ||
describe("getInitialFocusedCell", () => { | ||
const FOCUSED_CELL_FROM_PROPS = getFocusedCell(1, 2); | ||
const FOCUSED_CELL_FROM_STATE = getFocusedCell(3, 4); | ||
const SELECTED_REGIONS = [ | ||
Regions.cell(1, 1, 4, 5), | ||
Regions.cell(5, 1, 6, 2), | ||
]; | ||
|
||
it("returns undefined if enableFocus=false", () => { | ||
const focusedCell = FocusedCellUtils.getInitialFocusedCell( | ||
false, | ||
FOCUSED_CELL_FROM_PROPS, | ||
FOCUSED_CELL_FROM_STATE, | ||
SELECTED_REGIONS, | ||
); | ||
expect(focusedCell).to.be.undefined; | ||
}); | ||
|
||
it("returns the focusedCellFromProps if defined", () => { | ||
const focusedCell = FocusedCellUtils.getInitialFocusedCell( | ||
true, | ||
FOCUSED_CELL_FROM_PROPS, | ||
FOCUSED_CELL_FROM_STATE, | ||
SELECTED_REGIONS, | ||
); | ||
expect(focusedCell).to.deep.equal(FOCUSED_CELL_FROM_PROPS); | ||
}); | ||
|
||
it("returns the focusedCellFromState if focusedCellFromProps not defined", () => { | ||
const focusedCell = FocusedCellUtils.getInitialFocusedCell( | ||
true, | ||
null, | ||
FOCUSED_CELL_FROM_STATE, | ||
SELECTED_REGIONS, | ||
); | ||
expect(focusedCell).to.deep.equal(FOCUSED_CELL_FROM_STATE); | ||
}); | ||
|
||
it("returns the focused cell for the first selected region if " + | ||
"focusedCellFromState and focusedCellFromProps not defined", () => { | ||
const focusedCell = FocusedCellUtils.getInitialFocusedCell( | ||
true, | ||
null, | ||
null, | ||
SELECTED_REGIONS, | ||
); | ||
const expectedFocusedCell = { | ||
...Regions.getFocusCellCoordinatesFromRegion(SELECTED_REGIONS[0]), | ||
focusSelectionIndex: 0, | ||
}; | ||
expect(focusedCell).to.deep.equal(expectedFocusedCell); | ||
}); | ||
|
||
it("returns cell (0, 0) if nothing else is defined", () => { | ||
const focusedCell = FocusedCellUtils.getInitialFocusedCell( | ||
true, | ||
null, | ||
null, | ||
[], | ||
); | ||
const expectedFocusedCell = { | ||
col: 0, | ||
focusSelectionIndex: 0, | ||
row: 0, | ||
}; | ||
expect(focusedCell).to.deep.equal(expectedFocusedCell); | ||
}); | ||
|
||
function getFocusedCell(row: number, col: number, focusSelectionIndex: number = 0): IFocusedCellCoordinates { | ||
return { row, col, focusSelectionIndex }; | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Copyright 2017 Palantir Technologies, Inc. All rights reserved. | ||
* Licensed under the BSD-3 License as modified (the “License”); you may obtain a copy | ||
* of the license at https://github.com/palantir/blueprint/blob/master/LICENSE | ||
* and https://github.com/palantir/blueprint/blob/master/PATENTS | ||
*/ | ||
|
||
import "./focusCellUtilsTests"; | ||
import "./scrollUtilsTests"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
537acca
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Table] Show focus cell appropriately when enableFocus becomes true (#1447)
Preview: documentation
Coverage: core | datetime