generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Table): column pinning support enabled
- Loading branch information
Showing
11 changed files
with
231 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {block} from '../../utils'; | ||
|
||
export const columnPinningDemoBlock = block('column-pinning-demo'); |
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,56 @@ | ||
@use '../variables'; | ||
|
||
$block: '.#{variables.$ns}column-pinning-demo'; | ||
|
||
#{$block} { | ||
--default-border: 1px solid var(--g-color-line-generic); | ||
|
||
width: 600px; | ||
overflow: auto; | ||
|
||
border-inline: var(--default-border); | ||
border-block-end: var(--default-border); | ||
|
||
&__table { | ||
th, | ||
td { | ||
border: var(--default-border); | ||
|
||
padding: var(--g-spacing-1); | ||
} | ||
|
||
.gt-table__header-cell_lastPinnedLeft, | ||
.gt-table__cell_lastPinnedLeft { | ||
border-inline-end: 0; | ||
|
||
box-shadow: var(--g-color-line-generic-active) -2px 0px 2px -2px inset; | ||
} | ||
|
||
.gt-table__header-cell_firstPinnedRight, | ||
.gt-table__cell_firstPinnedRight { | ||
border-inline-start: 0; | ||
|
||
box-shadow: var(--g-color-line-generic-active) 2px 0 2px -2px inset; | ||
} | ||
|
||
.gt-table__header-row { | ||
.gt-table__header-cell:first-of-type { | ||
border-inline-start: 0; | ||
} | ||
|
||
.gt-table__header-cell:last-of-type { | ||
border-inline-end: 0; | ||
} | ||
} | ||
|
||
.gt-table__row { | ||
.gt-table__cell:first-of-type { | ||
border-inline-start: 0; | ||
} | ||
|
||
.gt-table__cell:last-of-type { | ||
border-inline-end: 0; | ||
} | ||
} | ||
} | ||
} |
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,35 @@ | ||
import React from 'react'; | ||
|
||
import type {ColumnPinningState} from '@tanstack/react-table'; | ||
|
||
import {useTable} from '../../hooks'; | ||
import {Table} from '../Table'; | ||
|
||
import {columnPinningDemoBlock} from './ColumnPinningDemo.classname'; | ||
import {data} from './constants/data'; | ||
import {columns} from './constants/pinning'; | ||
|
||
import './ColumnPinningDemo.scss'; | ||
|
||
export const ColumnPinningDemo = () => { | ||
const [columnPinning, setColumnPinning] = React.useState<ColumnPinningState>({ | ||
left: [], | ||
right: [], | ||
}); | ||
|
||
const table = useTable({ | ||
columns, | ||
data, | ||
enableColumnPinning: true, | ||
onColumnPinningChange: setColumnPinning, | ||
state: { | ||
columnPinning, | ||
}, | ||
}); | ||
|
||
return ( | ||
<div className={columnPinningDemoBlock()}> | ||
<Table className={columnPinningDemoBlock('table')} table={table} /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
|
||
import type {HeaderContext} from '@tanstack/react-table'; | ||
|
||
import type {Item} from '../types'; | ||
|
||
export interface ColumnPinningHeaderCellProps { | ||
value: string; | ||
info: HeaderContext<Item, unknown>; | ||
} | ||
|
||
export const ColumnPinningHeaderCell = ({value, info}: ColumnPinningHeaderCellProps) => { | ||
const handlePinLeft = () => { | ||
if (info.column.getCanPin()) { | ||
info.column.pin('left'); | ||
} | ||
}; | ||
|
||
const handlePinRight = () => { | ||
if (info.column.getCanPin()) { | ||
info.column.pin('right'); | ||
} | ||
}; | ||
|
||
const handleUnpin = () => { | ||
info.column.pin(false); | ||
}; | ||
|
||
return ( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
rowGap: '10px', | ||
}} | ||
> | ||
<div>{value}</div> | ||
<div style={{display: 'flex', columnGap: '10px'}}> | ||
<button onClick={() => handlePinLeft()}>{`<`}</button> | ||
<button onClick={() => handleUnpin()}>{`x`}</button> | ||
<button onClick={() => handlePinRight()}>{`>`}</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export interface ColumnPinningCellProps { | ||
value: string; | ||
} | ||
|
||
export const ColumnPinningCell = ({value}: ColumnPinningCellProps) => { | ||
return ( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
rowGap: '10px', | ||
}} | ||
> | ||
<div>{value}</div> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
|
||
import type {ColumnDef} from '@tanstack/react-table'; | ||
|
||
import {ColumnPinningCell, ColumnPinningHeaderCell} from '../cells/ColumnPinningCell'; | ||
import type {Item} from '../types'; | ||
|
||
export const columns: ColumnDef<Item>[] = [ | ||
{ | ||
accessorKey: 'name', | ||
header: (info) => <ColumnPinningHeaderCell value="Name" info={info} />, | ||
cell: (info) => <ColumnPinningCell value={info.getValue<string>()} />, | ||
minSize: 200, | ||
}, | ||
{ | ||
accessorKey: 'age', | ||
header: (info) => <ColumnPinningHeaderCell value="Age" info={info} />, | ||
cell: (info) => <ColumnPinningCell value={info.getValue<string>()} />, | ||
minSize: 200, | ||
}, | ||
{ | ||
accessorKey: 'status', | ||
header: (info) => <ColumnPinningHeaderCell value="Status" info={info} />, | ||
cell: (info) => <ColumnPinningCell value={info.getValue<string>()} />, | ||
minSize: 300, | ||
}, | ||
]; |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type React from 'react'; | ||
|
||
import type {NoStrictEntityMods} from '@bem-react/classname'; | ||
import type {Column} from '@tanstack/react-table'; | ||
|
||
export function getCommonPinningStyles<TData>(column: Column<TData>): React.CSSProperties { | ||
const isPinned = column.getIsPinned(); | ||
|
||
return { | ||
left: isPinned === 'left' ? `${column.getStart('left')}px` : undefined, | ||
right: isPinned === 'right' ? `${column.getAfter('right')}px` : undefined, | ||
position: isPinned ? 'sticky' : 'relative', | ||
width: column.getSize(), | ||
zIndex: isPinned ? 1 : 0, | ||
}; | ||
} | ||
|
||
export function getCommonPinningClassModes<TData>(column: Column<TData>): NoStrictEntityMods { | ||
const isPinned = column.getIsPinned(); | ||
const isLastLeftPinnedColumn = isPinned === 'left' && column.getIsLastColumn('left'); | ||
const isFirstRightPinnedColumn = isPinned === 'right' && column.getIsFirstColumn('right'); | ||
|
||
return { | ||
pinnedLeft: isPinned === 'left', | ||
pinnedRight: isPinned === 'right', | ||
lastPinnedLeft: isLastLeftPinnedColumn, | ||
firstPinnedRight: isFirstRightPinnedColumn, | ||
}; | ||
} |