-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[DataGrid] Add fluid columns width support #480
Merged
DanailH
merged 25 commits into
mui:master
from
DanailH:feature/DataGrid-347-columns-fluid-width
Nov 4, 2020
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
b2392dd
[DataGrid] Add fluid columns width support
DanailH e54cb9a
Fix doc formatting
DanailH c77a475
Fix documentation so that it is complient to the MUI documentation st…
DanailH 756932d
Add unit tests
DanailH 2d23e7d
Update packages/grid/_modules_/grid/hooks/root/useColumns.ts
DanailH 6cdced3
Update packages/grid/_modules_/grid/models/colDef/colDef.ts
DanailH b6cf786
Merge branch 'feature/DataGrid-347-columns-fluid-width' of github.com…
DanailH 12fa1fa
Fix PR comments
DanailH 3b1d8c3
Rebase from upstream master
DanailH 9b042c6
Fix unit tests
DanailH e92df89
Resolve TS issues
DanailH ff5e963
Add // @ts-expect-error need to migrate helpers to TypeScript before …
DanailH 5805baa
Add storybook examples
DanailH 6e5401c
Update docs/src/pages/components/data-grid/columns/columns.md
DanailH bf768d3
Update docs/src/pages/components/data-grid/columns/columns.md
DanailH 94404aa
Update docs/src/pages/components/data-grid/columns/columns.md
DanailH 4c6a988
Update docs/src/pages/components/data-grid/columns/columns.md
DanailH 04f336a
Update docs/src/pages/components/data-grid/columns/columns.md
DanailH b358751
Update packages/grid/data-grid/src/DataGrid.test.tsx
DanailH 2219564
Fix storybook flex col width examples
DanailH 401b7ed
rerun ci
oliviertassinari 2bd46a0
Fix formatting
DanailH 28baf68
Merge branch 'feature/DataGrid-347-columns-fluid-width' of github.com…
DanailH 9672852
Update docs/src/pages/components/data-grid/columns/columns.md
mbrookes d300dfe
Trigger CI
DanailH 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
34 changes: 34 additions & 0 deletions
34
docs/src/pages/components/data-grid/columns/ColumnFluidWidthGrid.js
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,34 @@ | ||
import * as React from 'react'; | ||
import { DataGrid } from '@material-ui/data-grid'; | ||
|
||
const rows = [ | ||
{ | ||
id: 1, | ||
username: 'defunkt', | ||
age: 38, | ||
}, | ||
]; | ||
|
||
export default function ColumnFluidWidthGrid() { | ||
return ( | ||
<div style={{ height: 250, width: '100%' }}> | ||
<DataGrid | ||
columns={[ | ||
{ | ||
field: 'id', | ||
flex: 1, | ||
}, | ||
{ | ||
field: 'username', | ||
width: 200, | ||
}, | ||
{ | ||
field: 'age', | ||
flex: 0.3, | ||
}, | ||
]} | ||
rows={rows} | ||
/> | ||
</div> | ||
); | ||
} |
34 changes: 34 additions & 0 deletions
34
docs/src/pages/components/data-grid/columns/ColumnFluidWidthGrid.tsx
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,34 @@ | ||
import * as React from 'react'; | ||
import { DataGrid } from '@material-ui/data-grid'; | ||
|
||
const rows = [ | ||
{ | ||
id: 1, | ||
username: 'defunkt', | ||
age: 38, | ||
}, | ||
]; | ||
|
||
export default function ColumnFluidWidthGrid() { | ||
return ( | ||
<div style={{ height: 250, width: '100%' }}> | ||
<DataGrid | ||
columns={[ | ||
{ | ||
field: 'id', | ||
flex: 1, | ||
}, | ||
{ | ||
field: 'username', | ||
width: 200, | ||
}, | ||
{ | ||
field: 'age', | ||
flex: 0.3, | ||
}, | ||
]} | ||
rows={rows} | ||
/> | ||
</div> | ||
); | ||
} |
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
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
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 |
---|---|---|
|
@@ -16,14 +16,47 @@ import { useApiMethod } from '../../root/useApiMethod'; | |
import { Logger, useLogger } from '../../utils/useLogger'; | ||
import { useGridState } from '../core/useGridState'; | ||
|
||
function mapColumns( | ||
columns: Columns, | ||
columnTypes: ColumnTypesRecord, | ||
containerWidth: number, | ||
): Columns { | ||
let extendedColumns = columns.map((c) => ({ ...getColDef(columnTypes, c.type), ...c })); | ||
const numberOfFluidColumns = columns.filter((column) => !!column.flex).length; | ||
let flexDivider = 0; | ||
|
||
if (numberOfFluidColumns && containerWidth) { | ||
extendedColumns.forEach((column) => { | ||
if (!column.flex) { | ||
containerWidth -= column.width!; | ||
} else { | ||
flexDivider += column.flex; | ||
} | ||
}); | ||
} | ||
|
||
if (containerWidth > 0 && numberOfFluidColumns) { | ||
const flexMultiplier = containerWidth / flexDivider; | ||
extendedColumns = extendedColumns.map((column) => { | ||
return { | ||
...column, | ||
width: column.flex! ? Math.floor(flexMultiplier * column.flex!) : column.width, | ||
}; | ||
}); | ||
} | ||
|
||
return extendedColumns; | ||
} | ||
|
||
function hydrateColumns( | ||
columns: Columns, | ||
columnTypes: ColumnTypesRecord, | ||
containerWidth: number, | ||
withCheckboxSelection: boolean, | ||
logger: Logger, | ||
): Columns { | ||
logger.debug('Hydrating Columns with default definitions'); | ||
let mappedCols = columns.map((c) => ({ ...getColDef(columnTypes, c.type), ...c })); | ||
let mappedCols = mapColumns(columns, columnTypes, containerWidth); | ||
if (withCheckboxSelection) { | ||
mappedCols = [checkboxSelectionColDef, ...mappedCols]; | ||
} | ||
|
@@ -58,14 +91,15 @@ function toMeta(logger: Logger, visibleColumns: Columns): ColumnsMeta { | |
const resetState = ( | ||
columns: Columns, | ||
columnTypes: ColumnTypesRecord, | ||
containerWidth: number, | ||
withCheckboxSelection: boolean, | ||
logger: Logger, | ||
): InternalColumns => { | ||
if (columns.length === 0) { | ||
return getInitialColumnsState(); | ||
} | ||
|
||
const all = hydrateColumns(columns, columnTypes, withCheckboxSelection, logger); | ||
const all = hydrateColumns(columns, columnTypes, containerWidth, withCheckboxSelection, logger); | ||
const visible = filterVisible(logger, all); | ||
const meta = toMeta(logger, visible); | ||
const lookup = toLookup(logger, all); | ||
|
@@ -115,7 +149,7 @@ const getUpdatedColumnState = ( | |
export function useColumns(columns: Columns, apiRef: ApiRef): InternalColumns { | ||
const logger = useLogger('useColumns'); | ||
const [gridState, setGridState, forceUpdate] = useGridState(apiRef); | ||
|
||
const viewportWidth = gridState.containerSizes ? gridState.containerSizes.viewportSize.width : 0; | ||
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 that it would be better to keep the same name for variables, |
||
const updateState = React.useCallback( | ||
(newState: InternalColumns, emit = true) => { | ||
logger.debug('Updating columns state.'); | ||
|
@@ -134,6 +168,7 @@ export function useColumns(columns: Columns, apiRef: ApiRef): InternalColumns { | |
const newState = resetState( | ||
columns, | ||
gridState.options.columnTypes, | ||
viewportWidth, | ||
!!gridState.options.checkboxSelection, | ||
logger, | ||
); | ||
|
@@ -142,6 +177,7 @@ export function useColumns(columns: Columns, apiRef: ApiRef): InternalColumns { | |
columns, | ||
gridState.options.columnTypes, | ||
gridState.options.checkboxSelection, | ||
viewportWidth, | ||
logger, | ||
updateState, | ||
]); | ||
|
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
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
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
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.
Should this be selector?
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 would keep it as it is. This function is only used in the
hydrateColumns
one and initially, that functionality was part of thehydrateColumns
but I separated it from it because thehydrateColumns
was becoming a bit too bloated.