-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -489,15 +489,34 @@ export class Table extends AbstractComponent<ITableProps, ITableState> { | |
); | ||
} | ||
|
||
public resizeRowsByTallestCell(columnIndex: number) { | ||
/** | ||
* 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. | ||
*/ | ||
public resizeRowsByTallestCells(columnIndices?: number[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about one method to rule them all: public resizeRowsByTallestCells(...columnIndices: number[]) { }
resizeRowsByTallestCells(1);
resizeRowsByTallestCells(1, 2, 3); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's a limit to the number of args you can provide to a function. better to leave this as an array. some people have tables that are 10,000s cols wide. if you really want a single function then you can overload it and do some runtime type checking on the argument but I think this is fine as-is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok that's fair i suppose There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, never thought about that before. TIL http://stackoverflow.com/questions/22747068/is-there-a-max-number-of-arguments-javascript-functions-can-accept There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another thought: I don't think this function is named quite correctly as is; no matter how many column indices you provide, you still only care about one "tallest cell" amongst all of them, right? Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i am inclined to agree with @cmslewis above. the plural felt weird to me to begin with, and now it's clear that it's not even semantically correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that suggestion @cmslewis with respect to the API |
||
const { locator } = this.state; | ||
|
||
const tallest = locator.getTallestVisibleCellInColumn(columnIndex); | ||
let tallest = 0; | ||
if (columnIndices != null) { | ||
const tallestByColumns = columnIndices.map((col) => locator.getTallestVisibleCellInColumn(col)); | ||
tallest = Math.max(...tallestByColumns); | ||
} else { | ||
// Use viewport columns | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slightly worried that people will think it applies to everything if you leave this blank... but probably fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could create a separate, explicit method if that's preferable, since the usage might not be obvious; don't have strong opinions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use a slightly more detailed comment here. |
||
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)); | ||
} | ||
} | ||
const rowHeights = Array(this.state.rowHeights.length).fill(tallest); | ||
this.invalidateGrid(); | ||
this.setState({ rowHeights }); | ||
} | ||
|
||
public resizeRowsByTallestCell(columnIndex: number) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Under my proposal above, we wouldn't need this additional function. |
||
this.resizeRowsByTallestCells([columnIndex]); | ||
} | ||
|
||
/** | ||
* When the component mounts, the HTML Element refs will be available, so | ||
* we constructor the Locator, which queries the elements' bounding | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,22 +103,42 @@ describe("<Table>", () => { | |
rowHeaders.forEach((rowHeader) => expectCellLoading(rowHeader, CellType.ROW_HEADER)); | ||
}); | ||
|
||
it("Gets and sets the tallest cell height in the table", () => { | ||
const renderCell = () => <Cell wrapText={true}>my cell value with lots and lots of words</Cell>; | ||
it("Gets and sets the tallest cell by columns correctly", () => { | ||
const DEFAULT_RESIZE_HEIGHT = 30; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
const MAX_HEIGHT = 40; | ||
const renderCellLong = () => <Cell wrapText={true}>my cell value with lots and lots of words</Cell>; | ||
const renderCellShort = () => <Cell wrapText={false}>short value</Cell>; | ||
|
||
let table: Table; | ||
|
||
const saveTable = (t: Table) => table = t; | ||
|
||
harness.mount( | ||
<Table ref={saveTable} numRows={4}> | ||
<Column name="Column0" renderCell={renderCell} /> | ||
<Column name="Column1" renderCell={renderCell} /> | ||
<Column name="Column0" renderCell={renderCellLong} /> | ||
<Column name="Column1" renderCell={renderCellShort} /> | ||
</Table>, | ||
); | ||
|
||
// Resize by first column | ||
table.resizeRowsByTallestCell(0); | ||
expect(table.state.rowHeights[0]).to.equal(40); | ||
expect(table.state.rowHeights[0]).to.equal(MAX_HEIGHT); | ||
|
||
// Resize by second column | ||
table.resizeRowsByTallestCell(1); | ||
expect(table.state.rowHeights[0]).to.equal(DEFAULT_RESIZE_HEIGHT); | ||
|
||
// Resize by both columns | ||
table.resizeRowsByTallestCells([0, 1]); | ||
expect(table.state.rowHeights[0]).to.equal(MAX_HEIGHT); | ||
|
||
// Resize by second column via array | ||
table.resizeRowsByTallestCells([1]); | ||
expect(table.state.rowHeights[0]).to.equal(DEFAULT_RESIZE_HEIGHT); | ||
|
||
// Resize by visible columns | ||
table.resizeRowsByTallestCells(); | ||
expect(table.state.rowHeights[0]).to.equal(MAX_HEIGHT); | ||
}); | ||
|
||
it("Selects all on click of upper-left corner", () => { | ||
|
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.
I vote for a slightly more precise description here. Something like: