Skip to content
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

feat(Table): sticky header support added #18

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/components/Table/Table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ $block: '.#{variables.$ns}table';
}
}

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

&__cell,
&__header-cell {
height: inherit;
Expand Down Expand Up @@ -124,7 +132,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 @@ -38,6 +38,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 @@ -61,6 +62,7 @@ export const Table = React.forwardRef(
rowClassName,
rowVirtualizer,
sortIndicatorClassName,
stickyHeader = false,
table,
withHeader = true,
}: TableProps<TData, TScrollElement>,
Expand Down Expand Up @@ -95,7 +97,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
/>
);
};
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 {cn} from '../../utils';

export const cnStickyHeaderDemo = cn('sticky-header-demo');
30 changes: 30 additions & 0 deletions src/components/__stories__/StickyHeaderDemo.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.sticky-header-demo {
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-line-generic-solid);

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;
}
}
}
25 changes: 25 additions & 0 deletions src/components/__stories__/StickyHeaderDemo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';

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

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

import './StickyHeaderDemo.scss';

const data = generateData(30);

export const StickyHeaderDemo = () => {
const table = useTable({
columns,
data,
});

return (
<div className={cnStickyHeaderDemo()}>
<Table className={cnStickyHeaderDemo('table')} table={table} stickyHeader />
</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({});
1 change: 1 addition & 0 deletions src/components/__stories__/VirtualizationDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const VirtualizationDemo = () => {
rowVirtualizer={rowVirtualizer}
className={cnVirtualizationDemo()}
headerClassName={cnVirtualizationDemo('header')}
stickyHeader
/>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/__stories__/WindowVirtualizationDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const WindowVirtualizationDemo = () => {
rowVirtualizer={rowVirtualizer}
className={cnVirtualizationDemo()}
headerClassName={cnVirtualizationDemo('header')}
stickyHeader
/>
);
};
Loading