Skip to content

Commit

Permalink
feat(Table): sticky header support added
Browse files Browse the repository at this point in the history
  • Loading branch information
chelentos committed Jul 18, 2024
1 parent 3ef0a16 commit 52586de
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/components/Table/Table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ $block: '.#{variables.$ns}table';

&__header {
background: var(--g-color-base-background);

&_sticky {
position: sticky;
inset-block-start: 0;
z-index: 1;
}
}

&__header-row {
Expand Down Expand Up @@ -167,7 +173,6 @@ $block: '.#{variables.$ns}table';
&__header {
display: grid;

position: sticky;
inset-block-start: 0;

z-index: 1;
Expand Down
4 changes: 3 additions & 1 deletion src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface TableProps<TData, TScrollElement extends Element | Window = HTM
rowClassName?: string;
rowVirtualizer?: Virtualizer<TScrollElement, HTMLTableRowElement>;
sortIndicatorClassName?: HeaderRowProps<TData, unknown>['sortIndicatorClassName'];
stickyHeader?: boolean;
table: TableType<TData>;
withHeader?: boolean;
}
Expand All @@ -65,6 +66,7 @@ export const Table = React.forwardRef(
rowClassName,
rowVirtualizer,
sortIndicatorClassName,
stickyHeader = false,
table,
withHeader = true,
}: TableProps<TData, TScrollElement>,
Expand Down Expand Up @@ -105,7 +107,7 @@ export const Table = React.forwardRef(
data-dragging-row-index={draggingRowIndex > -1 ? draggingRowIndex : undefined}
>
{withHeader && (
<thead className={b('header', headerClassName)}>
<thead className={b('header', {sticky: stickyHeader}, headerClassName)}>
{headerGroups.map((headerGroup, index) => (
<HeaderRow
key={headerGroup.id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const ReorderingWithVirtualizationDemo = () => {
table={table}
rowVirtualizer={rowVirtualizer}
onReorder={handleReorder}
stickyHeader={true}
/>
);
};
3 changes: 3 additions & 0 deletions src/components/__stories__/StickyHeaderDemo.classname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {block} from '../../utils';

export const cnStickyHeaderDemo = block('sticky-header-demo');
34 changes: 34 additions & 0 deletions src/components/__stories__/StickyHeaderDemo.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@use '../variables';

$block: '.#{variables.$ns}sticky-header-demo';

#{$block} {
width: fit-content;
max-height: 400px;

overflow: auto;

&__table {
th,
td {
box-sizing: border-box;

padding: var(--g-spacing-1);

border-collapse: collapse;
border: 1px solid var(--g-color-line-generic);
}

th {
background: var(--g-color-base-generic-medium);

border-block-end: 0;

box-shadow: var(--g-color-line-generic) 0 -1px 0 inset;
}

.gt-table__row:first-child td {
border-block-start: 0;
}
}
}
26 changes: 26 additions & 0 deletions src/components/__stories__/StickyHeaderDemo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';

import {useTable} from '../../hooks';
import {Table} from '../Table';

import {cnStickyHeaderDemo} from './StickyHeaderDemo.classname';
import {columns} from './constants/columns';
import {data} from './constants/data';

import './StickyHeaderDemo.scss';

export const StickyHeaderDemo = () => {
const table = useTable({
columns,
data: Array(5)
.fill(data)
.flat()
.map((val, index) => ({...val, id: `${val.id}-${index}`})),
});

return (
<div className={cnStickyHeaderDemo()}>
<Table className={cnStickyHeaderDemo('table')} table={table} stickyHeader={true} />
</div>
);
};
4 changes: 4 additions & 0 deletions src/components/__stories__/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {ReorderingTreeDemo} from './ReorderingTreeDemo';
import {ReorderingWithVirtualizationDemo} from './ReorderingWithVirtualizationDemo';
import {ResizingDemo} from './ResizingDemo';
import {SortingDemo} from './SortingDemo';
import {StickyHeaderDemo} from './StickyHeaderDemo';
import {TreeDemo} from './TreeDemo';
import {TreeWithGroupsDemo} from './TreeWithGroupsDemo';
import {VirtualizationDemo} from './VirtualizationDemo';
Expand Down Expand Up @@ -85,3 +86,6 @@ export const ColumnPinningWithReordering: StoryFn = ColumnPinningWithReorderingT

const ColumnPinningWithSelectionTemplate: StoryFn = () => <ColumnPinningWithSelectionDemo />;
export const ColumnPinningWithSelection: StoryFn = ColumnPinningWithSelectionTemplate.bind({});

const StickyHeaderTamplate: StoryFn = () => <StickyHeaderDemo />;
export const StickyHeader: StoryFn = StickyHeaderTamplate.bind({});
2 changes: 1 addition & 1 deletion src/components/__stories__/VirtualizationDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const VirtualizationDemo = () => {

return (
<div ref={containerRef} style={{height: '90vh', overflow: 'auto'}}>
<Table table={table} rowVirtualizer={rowVirtualizer} />
<Table table={table} rowVirtualizer={rowVirtualizer} stickyHeader={true} />
</div>
);
};
2 changes: 1 addition & 1 deletion src/components/__stories__/WindowVirtualizationDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export const WindowVirtualizationDemo = () => {
overscan: 5,
});

return <Table table={table} rowVirtualizer={rowVirtualizer} />;
return <Table table={table} rowVirtualizer={rowVirtualizer} stickyHeader={true} />;
};

0 comments on commit 52586de

Please sign in to comment.