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] scrollToRegion instance method #1496

Merged
merged 30 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f661262
Delete commented code
cmslewis Aug 22, 2017
bd91c72
Add method stub
cmslewis Aug 22, 2017
c4d9f6b
Initial implementation
cmslewis Aug 22, 2017
9e694ab
[TEMPORARY?] Add instance to the window object
cmslewis Aug 22, 2017
70a8bb5
Make auto-scrolling work; fix syncViewportPosition bug
cmslewis Aug 23, 2017
bd1c170
Handling for frozen rows/columns
cmslewis Aug 23, 2017
f65c8f4
Fix scroll-correction logic
cmslewis Aug 23, 2017
0ad4d8a
Add instance method to TableQuadrantStack
cmslewis Aug 24, 2017
33bb63b
Refactor logic into new scrollUtils.ts
cmslewis Aug 24, 2017
b4cf85e
Create new common/internal folder
cmslewis Aug 24, 2017
cc7b908
Fix scroll misalignment bug
cmslewis Aug 24, 2017
fcb8aed
Don't correct if scroll is disabled
cmslewis Aug 24, 2017
2c29b21
Prevent programmatic scrolling when scrolling disabled
cmslewis Aug 24, 2017
a29f832
Remove need for Grid in scrollUtils file
cmslewis Aug 24, 2017
a87242d
Fix off-by-one bug
cmslewis Aug 24, 2017
5cbf3b6
Add scrollTo controls to the table example
cmslewis Aug 24, 2017
ca9be6b
Fix lint
cmslewis Aug 24, 2017
f5080e4
Delete animated param for now
cmslewis Aug 24, 2017
28fcb52
Delete window instance
cmslewis Aug 24, 2017
9d1d9f1
Delete console.logs
cmslewis Aug 24, 2017
5281104
Revert changes RE: prereqStateKeyValue
cmslewis Aug 24, 2017
839c027
Delete unintentional cnewline
cmslewis Aug 24, 2017
89b7b05
Write tests for scrollUtils
cmslewis Aug 24, 2017
da186b5
Fix lint again
cmslewis Aug 25, 2017
dfb5a9c
Write tests in table too
cmslewis Aug 25, 2017
26b4fc8
Added docs, new 'Instance methods' section
cmslewis Aug 25, 2017
70ca679
Update stuff per CR feedback
cmslewis Aug 25, 2017
f21b683
Oops, revert changes to table example
cmslewis Aug 25, 2017
ec3e915
Fix stuff per bdwyer CR
cmslewis Aug 26, 2017
80dfb99
Merge branch 'master' into cl/table-scroll-to-position
cmslewis Aug 28, 2017
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
Prev Previous commit
Next Next commit
Write tests for scrollUtils
  • Loading branch information
cmslewis committed Aug 24, 2017
commit 89b7b0527ffa15653413a00bf52e32895f30022c
271 changes: 271 additions & 0 deletions packages/table/test/common/internal/scrollUtilsTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
/**
* 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 * as ScrollUtils from "../../../src/common/internal/scrollUtils";
import { IRegion, Regions } from "../../../src/regions";

describe("scrollUtils", () => {
describe("getScrollPositionForRegion", () => {
const COLUMN_WIDTH = 150;
const ROW_HEIGHT = 20;

const INITIAL_SCROLL_LEFT = 17;
const INITIAL_SCROLL_TOP = 33;

const NUM_FROZEN_ROWS = 2;
const NUM_FROZEN_COLUMNS = 2;

describe("no frozen rows or columns", () => {
const TARGET_ROW = 2;
const TARGET_COLUMN = 3;

function fn(region: IRegion) {
return ScrollUtils.getScrollPositionForRegion(
region,
INITIAL_SCROLL_LEFT,
INITIAL_SCROLL_TOP,
getLeftOffset,
getTopOffset,
);
}

it("scrolling to cell", () => {
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT);
});

it("scrolling to row", () => {
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT);
});

it("scrolling to column", () => {
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH);
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});

it("scrolling to full table", () => {
const region = Regions.table();
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(0);
});
});

describe("with frozen rows", () => {
function fn(region: IRegion) {
return ScrollUtils.getScrollPositionForRegion(
region,
INITIAL_SCROLL_LEFT,
INITIAL_SCROLL_TOP,
getLeftOffset,
getTopOffset,
NUM_FROZEN_ROWS,
);
}

it("scrolling to a frozen cell", () => {
const TARGET_ROW = NUM_FROZEN_ROWS - 1;
const TARGET_COLUMN = 3;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH);
expect(scrollTop).to.equal(0);
});

it("scrolling to a non-frozen cell", () => {
const TARGET_ROW = NUM_FROZEN_ROWS; // 1 row beyond the last frozen row, b/c num is 1-indexed
const TARGET_COLUMN = 3;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH);
expect(scrollTop).to.equal((TARGET_ROW * ROW_HEIGHT) - (NUM_FROZEN_ROWS * ROW_HEIGHT));
});

it("scrolling to a column", () => {
const TARGET_COLUMN = 3;
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(TARGET_COLUMN * COLUMN_WIDTH);
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});

it("scrolling to a frozen row", () => {
const TARGET_ROW = NUM_FROZEN_ROWS - 1;
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal(0);
});

it("scrolling to a non-frozen row", () => {
const TARGET_ROW = NUM_FROZEN_ROWS; // 1 row beyond the frozen region
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal((TARGET_ROW * ROW_HEIGHT) - (NUM_FROZEN_ROWS * ROW_HEIGHT));
});

it("scrolling to full table", () => {
const region = Regions.table();
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(0);
});
});

describe("with frozen columns", () => {
function fn(region: IRegion) {
return ScrollUtils.getScrollPositionForRegion(
region,
INITIAL_SCROLL_LEFT,
INITIAL_SCROLL_TOP,
getLeftOffset,
getTopOffset,
0,
NUM_FROZEN_COLUMNS,
);
}

it("scrolling to a frozen cell", () => {
const TARGET_ROW = 3
const TARGET_COLUMN = NUM_FROZEN_COLUMNS - 1;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT);
});

it("scrolling to a non-frozen cell", () => {
const TARGET_ROW = 3;
const TARGET_COLUMN = NUM_FROZEN_COLUMNS;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT);
expect(scrollLeft).to.equal((TARGET_COLUMN * COLUMN_WIDTH) - (NUM_FROZEN_COLUMNS * COLUMN_WIDTH));
});

it("scrolling to a frozen column", () => {
const TARGET_COLUMN = NUM_FROZEN_COLUMNS - 1;
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});

it("scrolling to a non-frozen column", () => {
const TARGET_COLUMN = NUM_FROZEN_COLUMNS; // 1 row beyond the frozen region
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal((TARGET_COLUMN * COLUMN_WIDTH) - (NUM_FROZEN_COLUMNS * COLUMN_WIDTH));
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});

it("scrolling to a row", () => {
const TARGET_ROW = 3;
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal(TARGET_ROW * ROW_HEIGHT);
});

it("scrolling to full table", () => {
const region = Regions.table();
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(0);
});
});

describe("with frozen rows and columns", () => {
function fn(region: IRegion) {
return ScrollUtils.getScrollPositionForRegion(
region,
INITIAL_SCROLL_LEFT,
INITIAL_SCROLL_TOP,
getLeftOffset,
getTopOffset,
NUM_FROZEN_ROWS,
NUM_FROZEN_COLUMNS,
);
}

it("scrolling to a frozen cell", () => {
const TARGET_ROW = NUM_FROZEN_ROWS - 1;
const TARGET_COLUMN = NUM_FROZEN_COLUMNS - 1;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(0);
});

it("scrolling to a non-frozen cell", () => {
const TARGET_ROW = NUM_FROZEN_ROWS;
const TARGET_COLUMN = NUM_FROZEN_COLUMNS;
const region = Regions.cell(TARGET_ROW, TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollTop).to.equal((TARGET_ROW * ROW_HEIGHT) - (NUM_FROZEN_ROWS * ROW_HEIGHT));
expect(scrollLeft).to.equal((TARGET_COLUMN * COLUMN_WIDTH) - (NUM_FROZEN_COLUMNS * COLUMN_WIDTH));
});

it("scrolling to a frozen column", () => {
const TARGET_COLUMN = NUM_FROZEN_COLUMNS - 1;
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});

it("scrolling to a non-frozen column", () => {
const TARGET_COLUMN = NUM_FROZEN_COLUMNS; // 1 row beyond the frozen region
const region = Regions.column(TARGET_COLUMN);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal((TARGET_COLUMN * COLUMN_WIDTH) - (NUM_FROZEN_COLUMNS * COLUMN_WIDTH));
expect(scrollTop).to.equal(INITIAL_SCROLL_TOP);
});

it("scrolling to a frozen row", () => {
const TARGET_ROW = NUM_FROZEN_ROWS - 1;
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal(0);
});

it("scrolling to a non-frozen row", () => {
const TARGET_ROW = NUM_FROZEN_ROWS; // 1 row beyond the frozen region
const region = Regions.row(TARGET_ROW);
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(INITIAL_SCROLL_LEFT);
expect(scrollTop).to.equal((TARGET_ROW * ROW_HEIGHT) - (NUM_FROZEN_ROWS * ROW_HEIGHT));
});

it("scrolling to full table", () => {
const region = Regions.table();
const { scrollLeft, scrollTop } = fn(region);
expect(scrollLeft).to.equal(0);
expect(scrollTop).to.equal(0);
});
});

function getTopOffset(rowIndex: number) {
return ROW_HEIGHT * rowIndex;
}

function getLeftOffset(columnIndex: number) {
return COLUMN_WIDTH * columnIndex;
}
});
});
1 change: 1 addition & 0 deletions packages/table/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "./cellTests.tsx";
import "./clipboardTests.ts";
import "./columnHeaderCellTests.tsx";
import "./columnTests.tsx";
import "./common/internal/scrollUtilsTests.ts";
import "./editableCellTests.tsx";
import "./editableNameTests.tsx";
import "./formatsTests.tsx";
Expand Down