-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Feature: Add support for flex column width #2839
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
Merged
Merged
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
ec08ef0
Add support for "auto" width
ff6ae11
Calculate columns when grid width is set
9a8f6f3
Remove unnecessary code
04d61cb
Merge branch 'main' into am-auto-columns
f1d9109
Merge branch 'main' into am-auto-columns
amanmahajan7 a291318
Add support for css column units (fr, %, max-content)
e6e3230
Bring back virtualization
def96f2
Remove console
c5634e5
Fix minWidth and examples
391e780
Merge branch 'main' into am-auto-columns
amanmahajan7 efafea7
Add min width
358b7ad
Disable @typescript-eslint/naming-convention
4ab2529
Merge branch 'main' into am-auto-columns
amanmahajan7 085fd02
Add comments
41bea02
Revent some changes
a701693
Use getBoundingClientRect
dfb71aa
Rename variables
d7a6edf
Merge branch 'main' into am-auto-columns
fd6db55
Merge branch 'main' into am-auto-columns
amanmahajan7 7fa2267
Merge branch 'main' into am-auto-columns
9bc8500
Merge branch 'main' into am-auto-columns
amanmahajan7 45799e2
Merge branch 'main' into am-auto-columns
amanmahajan7 801b956
Merge branch 'main' into am-auto-columns
amanmahajan7 b34375d
Merge branch 'main' into am-auto-columns
amanmahajan7 e0a8624
support flex width on all the columns
6b1233b
Merge branch 'main' into am-auto-columns
amanmahajan7 ff24d72
Merge branch 'main' into am-auto-columns
amanmahajan7 dabb46e
Merge branch 'main' into am-auto-columns
amanmahajan7 07212fa
Merge branch 'main' into am-auto-columns
e76c3aa
Merge branch 'main' into am-auto-columns
amanmahajan7 8ef79be
Catchup main
amanmahajan7 0bd458b
Fix tests
amanmahajan7 b0abe58
Merge branch 'main' into am-auto-columns
amanmahajan7 f17731a
Update src/DataGrid.tsx
amanmahajan7 aa8485a
Address a few comments
amanmahajan7 eb899a9
Move width type close to minWidth
amanmahajan7 40a1c75
Merge branch 'main' into am-auto-columns
amanmahajan7 76aee36
Do not reset max-content columns
amanmahajan7 740ff89
Merge branch 'main' into am-auto-columns
amanmahajan7 05a4521
Remove flexColumnWidths state
amanmahajan7 2e38b07
Add max-content example
amanmahajan7 284dae9
Merge branch 'main' into am-auto-columns
amanmahajan7 4691248
Update src/DataGrid.tsx
amanmahajan7 a226262
Fix classname
amanmahajan7 0a6aaf4
Update src/hooks/useGridDimensions.ts
amanmahajan7 e34a87d
Remove useMemo
amanmahajan7 945f776
Add back useMemo
amanmahajan7 151d724
tweak autosizeColumnsClassname for better measuring
nstepien edc46e7
Revert "tweak autosizeColumnsClassname for better measuring"
nstepien File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -4,7 +4,7 @@ import type { CalculatedColumn, Column, Maybe } from '../types'; | |
import type { DataGridProps } from '../DataGrid'; | ||
import { valueFormatter, toggleGroupFormatter } from '../formatters'; | ||
import { SELECT_COLUMN_KEY } from '../Columns'; | ||
import { clampColumnWidth, floor, max, min, round } from '../utils'; | ||
import { clampColumnWidth, max, min } from '../utils'; | ||
|
||
type Mutable<T> = { | ||
-readonly [P in keyof T]: T[P]; | ||
|
@@ -15,6 +15,9 @@ interface ColumnMetric { | |
left: number; | ||
} | ||
|
||
const DEFAULT_COLUMN_WIDTH = 'auto'; | ||
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. |
||
const DEFAULT_COLUMN_MIN_WIDTH = 80; | ||
|
||
interface CalculatedColumnsArgs<R, SR> extends Pick<DataGridProps<R, SR>, 'defaultColumnOptions'> { | ||
rawColumns: readonly Column<R, SR>[]; | ||
rawGroupBy: Maybe<readonly string[]>; | ||
|
@@ -33,8 +36,8 @@ export function useCalculatedColumns<R, SR>({ | |
rawGroupBy, | ||
enableVirtualization | ||
}: CalculatedColumnsArgs<R, SR>) { | ||
const defaultWidth = defaultColumnOptions?.width; | ||
const defaultMinWidth = defaultColumnOptions?.minWidth ?? 80; | ||
const defaultWidth = defaultColumnOptions?.width ?? DEFAULT_COLUMN_WIDTH; | ||
const defaultMinWidth = defaultColumnOptions?.minWidth ?? DEFAULT_COLUMN_MIN_WIDTH; | ||
const defaultMaxWidth = defaultColumnOptions?.maxWidth; | ||
const defaultFormatter = defaultColumnOptions?.formatter ?? valueFormatter; | ||
const defaultSortable = defaultColumnOptions?.sortable ?? false; | ||
|
@@ -148,38 +151,19 @@ export function useCalculatedColumns<R, SR>({ | |
let left = 0; | ||
let totalFrozenColumnWidth = 0; | ||
let templateColumns = ''; | ||
let allocatedWidth = 0; | ||
let unassignedColumnsCount = 0; | ||
|
||
for (const column of columns) { | ||
let width = getSpecifiedWidth(column, columnWidths, viewportWidth); | ||
|
||
if (width === undefined) { | ||
unassignedColumnsCount++; | ||
} else { | ||
let width = columnWidths.get(column.key) ?? column.width; | ||
if (typeof width === 'number') { | ||
width = clampColumnWidth(width, column); | ||
amanmahajan7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
allocatedWidth += width; | ||
columnMetrics.set(column, { width, left: 0 }); | ||
} | ||
} | ||
|
||
for (const column of columns) { | ||
let width: number; | ||
if (columnMetrics.has(column)) { | ||
const columnMetric = columnMetrics.get(column)!; | ||
columnMetric.left = left; | ||
({ width } = columnMetric); | ||
} else { | ||
// avoid decimals as subpixel positioning can lead to cell borders not being displayed | ||
const unallocatedWidth = viewportWidth - allocatedWidth; | ||
const unallocatedColumnWidth = round(unallocatedWidth / unassignedColumnsCount); | ||
width = clampColumnWidth(unallocatedColumnWidth, column); | ||
allocatedWidth += width; | ||
unassignedColumnsCount--; | ||
columnMetrics.set(column, { width, left }); | ||
// This is a placeholder width so we can continue to use virtualization. | ||
// The actual value is set after the column is rendered | ||
width = column.minWidth; | ||
} | ||
left += width; | ||
templateColumns += `${width}px `; | ||
columnMetrics.set(column, { width, left }); | ||
left += width; | ||
} | ||
|
||
if (lastFrozenColumnIndex !== -1) { | ||
|
@@ -197,7 +181,7 @@ export function useCalculatedColumns<R, SR>({ | |
} | ||
|
||
return { layoutCssVars, totalFrozenColumnWidth, columnMetrics }; | ||
}, [columnWidths, columns, viewportWidth, lastFrozenColumnIndex]); | ||
}, [columnWidths, columns, lastFrozenColumnIndex]); | ||
|
||
const [colOverscanStartIdx, colOverscanEndIdx] = useMemo((): [number, number] => { | ||
if (!enableVirtualization) { | ||
|
@@ -265,22 +249,3 @@ export function useCalculatedColumns<R, SR>({ | |
groupBy | ||
}; | ||
} | ||
|
||
function getSpecifiedWidth<R, SR>( | ||
{ key, width }: Column<R, SR>, | ||
columnWidths: ReadonlyMap<string, number>, | ||
viewportWidth: number | ||
): number | undefined { | ||
if (columnWidths.has(key)) { | ||
// Use the resized width if available | ||
return columnWidths.get(key); | ||
} | ||
|
||
if (typeof width === 'number') { | ||
return width; | ||
} | ||
if (typeof width === 'string' && /^\d+%$/.test(width)) { | ||
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. No longer needed as we get the width after the columns are rendered |
||
return floor((viewportWidth * parseInt(width, 10)) / 100); | ||
} | ||
return undefined; | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This effect is run if the viewport has any flex columns.