-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data-grid): Add column pinning feature (#3176)
* feat(data-grid): Add column pinning feature * feat(data-grid): Add column pinning feature fix eslint error.. * feat(data-grid): Add column pinning feature fix horizontal scrollbar appearing when it shouldn't * feat(data-grid): Add column pinning feature Fix for styled-component warning * feat(data-grid): Add column pinning feature z-index to 'auto' by default. * feat(data-grid): Add column pinning feature Fix rowStyle method Closes #3042
- Loading branch information
Showing
7 changed files
with
166 additions
and
16 deletions.
There are no files selected for viewing
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
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
46 changes: 39 additions & 7 deletions
46
packages/eds-data-grid-react/src/components/TableBodyCell.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 |
---|---|---|
@@ -1,30 +1,62 @@ | ||
import { Cell, flexRender } from '@tanstack/react-table' | ||
import { Cell, ColumnPinningPosition, flexRender } from '@tanstack/react-table' | ||
import { Table, Typography } from '@equinor/eds-core-react' | ||
import { useTableContext } from '../EdsDataGridContext' | ||
import { useMemo } from 'react' | ||
import { tokens } from '@equinor/eds-tokens' | ||
import styled from 'styled-components' | ||
|
||
type Props<T> = { | ||
cell: Cell<T, unknown> | ||
} | ||
|
||
const StyledCell = styled(Table.Cell)<{ | ||
$pinned: ColumnPinningPosition | ||
$offset: number | ||
}>` | ||
position: ${(p) => (p.$pinned ? 'sticky' : 'relative')}; | ||
${(p) => { | ||
if (p.$pinned) { | ||
return `${p.$pinned}: ${p.$offset}px;` | ||
} | ||
return '' | ||
}} | ||
z-index: ${(p) => (p.$pinned ? 11 : 'auto')}; | ||
background-color: ${(p) => | ||
p.$pinned ? tokens.colors.ui.background__default.hex : 'inherit'}; | ||
overflow: hidden; | ||
white-space: nowrap; | ||
text-overflow: ellipsis; | ||
` | ||
|
||
export function TableBodyCell<T>({ cell }: Props<T>) { | ||
const { cellClass, cellStyle } = useTableContext() | ||
const { cellClass, cellStyle, table } = useTableContext() | ||
const pinned = cell.column.getIsPinned() | ||
const pinnedOffset = useMemo<number>(() => { | ||
if (!pinned) { | ||
return 0 | ||
} | ||
const header = table.getFlatHeaders().find((h) => h.id === cell.column.id) | ||
return pinned === 'left' | ||
? header.getStart() | ||
: table.getTotalSize() - header.getStart() - cell.column.getSize() | ||
}, [pinned, cell.column, table]) | ||
return ( | ||
<Table.Cell | ||
<StyledCell | ||
$pinned={pinned} | ||
$offset={pinnedOffset} | ||
className={cellClass ? cellClass(cell.row, cell.column.id) : ''} | ||
{...{ | ||
key: cell.id, | ||
style: { | ||
width: cell.column.getSize(), | ||
maxWidth: cell.column.getSize(), | ||
overflow: 'hidden', | ||
whiteSpace: 'nowrap', | ||
textOverflow: 'ellipsis', | ||
...(cellStyle?.(cell.row, cell.column.id) ?? {}), | ||
}, | ||
}} | ||
> | ||
<Typography as="span" group="table" variant="cell_text"> | ||
{flexRender(cell.column.columnDef.cell, cell.getContext())} | ||
</Typography> | ||
</Table.Cell> | ||
</StyledCell> | ||
) | ||
} |
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