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] Add resizeRowsByTallestCells to handle multi-column resize #1075

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions packages/table/preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ class FormatsTable extends React.Component<{}, {}> {
this.formatsTable.resizeRowsByTallestCell(3);
});
document.querySelector(".resize-wrapped-and-json").addEventListener("click", () => {
this.formatsTable.resizeRowsByTallestCells([1, 3]);
this.formatsTable.resizeRowsByTallestCell([1, 3]);
});
document.querySelector(".resize-viewport").addEventListener("click", () => {
this.formatsTable.resizeRowsByTallestCells();
this.formatsTable.resizeRowsByTallestCell();
});
}

Expand Down
21 changes: 9 additions & 12 deletions packages/table/src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -490,33 +490,30 @@ export class Table extends AbstractComponent<ITableProps, ITableState> {
}

/**
* Resize rows based on a given set of column indices.
* If no indices are provided, default to using the columns that are currently in the viewport.
* Resize all rows in the table to the height of the tallest visible cell in the specified columns.
* If no indices are provided, default to using the tallest visible cell from all columns in view.
*/
public resizeRowsByTallestCells(columnIndices?: number[]) {
public resizeRowsByTallestCell(columnIndices?: number | number[]) {
const { locator } = this.state;
let tallest = 0;
if (columnIndices != null) {
const tallestByColumns = columnIndices.map((col) => locator.getTallestVisibleCellInColumn(col));
tallest = Math.max(...tallestByColumns);
} else {
// Use viewport columns
if (columnIndices == null) {
// Consider all columns currently in viewport
const { grid } = this;
const { viewportRect } = this.state;
const viewportColumnIndices = grid.getColumnIndicesInRect(viewportRect);
for (let col = viewportColumnIndices.columnIndexStart; col <= viewportColumnIndices.columnIndexEnd; col++) {
tallest = Math.max(tallest, locator.getTallestVisibleCellInColumn(col));
}
} else {
const columnIndicesArray = Array.isArray(columnIndices) ? columnIndices : [columnIndices];
const tallestByColumns = columnIndicesArray.map((col) => locator.getTallestVisibleCellInColumn(col));
tallest = Math.max(...tallestByColumns);
}
const rowHeights = Array(this.state.rowHeights.length).fill(tallest);
this.invalidateGrid();
this.setState({ rowHeights });
}

public resizeRowsByTallestCell(columnIndex: number) {
this.resizeRowsByTallestCells([columnIndex]);
}

/**
* When the component mounts, the HTML Element refs will be available, so
* we constructor the Locator, which queries the elements' bounding
Expand Down
6 changes: 3 additions & 3 deletions packages/table/test/tableTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ describe("<Table>", () => {
expect(table.state.rowHeights[0]).to.equal(DEFAULT_RESIZE_HEIGHT);

// Resize by both columns
table.resizeRowsByTallestCells([0, 1]);
table.resizeRowsByTallestCell([0, 1]);
expect(table.state.rowHeights[0]).to.equal(MAX_HEIGHT);

// Resize by second column via array
table.resizeRowsByTallestCells([1]);
table.resizeRowsByTallestCell([1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove brackets here to test non-array syntax? (1)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nvm i see the lines above now 👍

expect(table.state.rowHeights[0]).to.equal(DEFAULT_RESIZE_HEIGHT);

// Resize by visible columns
table.resizeRowsByTallestCells();
table.resizeRowsByTallestCell();
expect(table.state.rowHeights[0]).to.equal(MAX_HEIGHT);
});

Expand Down