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

[PBNTR-661] Create a "stickyLeftcolumn" prop for the Table kit #3913

Merged
merged 7 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
127 changes: 103 additions & 24 deletions playbook/app/pb_kits/playbook/pb_table/_table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { buildAriaProps, buildDataProps, buildHtmlProps } from '../utilities/pro
import { globalProps, GlobalProps } from '../utilities/globalProps'
import PbTable from '.'
import {
TableHead,
TableHeader,
TableBody,
TableRow,
TableCell,
TableHead,
TableHeader,
TableBody,
TableRow,
TableCell,
} from "./subcomponents";

type TableProps = {
Expand All @@ -28,6 +28,7 @@ type TableProps = {
singleLine?: boolean,
size?: "sm" | "md" | "lg",
sticky?: boolean,
stickyLeftcolumn?: string[],
striped?: boolean,
tag?: "table" | "div",
verticalBorder?: boolean,
Expand All @@ -51,6 +52,7 @@ const Table = (props: TableProps): React.ReactElement => {
singleLine = false,
size = 'sm',
sticky = false,
stickyLeftcolumn = [],
striped = false,
tag = 'table',
verticalBorder = false,
Expand All @@ -76,6 +78,7 @@ const Table = (props: TableProps): React.ReactElement => {
'single-line': singleLine,
'no-hover': disableHover,
'sticky-header': sticky,
'sticky-left-column': stickyLeftcolumn,
'striped': striped,
[outerPaddingCss]: outerPadding !== '',
},
Expand All @@ -85,33 +88,109 @@ const Table = (props: TableProps): React.ReactElement => {
className
)

useEffect(() => {
const handleStickyColumns = () => {
nickamantia marked this conversation as resolved.
Show resolved Hide resolved
let accumulatedWidth = 0;

stickyLeftcolumn.forEach((colId, index) => {
const isLastColumn = index === stickyLeftcolumn.length - 1;
const header = document.querySelector(`th[id="${colId}"]`);
const cells = document.querySelectorAll(`td[id="${colId}"]`);

if (header) {
header.classList.add('sticky');
(header as HTMLElement).style.left = `${accumulatedWidth}px`;

if (!isLastColumn) {
header.classList.add('with-border');
header.classList.remove('sticky-shadow');
} else {
header.classList.remove('with-border');
header.classList.add('sticky-shadow');
}

accumulatedWidth += (header as HTMLElement).offsetWidth;
}

cells.forEach((cell) => {
cell.classList.add('sticky');
(cell as HTMLElement).style.left = `${accumulatedWidth - (header as HTMLElement).offsetWidth}px`;

if (!isLastColumn) {
cell.classList.add('with-border');
cell.classList.remove('sticky-shadow');
} else {
cell.classList.remove('with-border');
cell.classList.add('sticky-shadow');
}
});
});
};

setTimeout(() => {
handleStickyColumns();
}, 10);

window.addEventListener('resize', handleStickyColumns);

return () => {
window.removeEventListener('resize', handleStickyColumns);
};
}, [stickyLeftcolumn]);

useEffect(() => {
const instance = new PbTable()
instance.connect()
}, [])

return (
<>
{isTableTag ? (
<table
{...ariaProps}
{...dataProps}
{...htmlProps}
className={classNames}
id={id}
>
{children}
</table>
) : (
<div
{...ariaProps}
{...dataProps}
{...htmlProps}
className={classNames}
id={id}
>
{children}
{responsive === 'scroll' ? (
<div className='table-responsive-scroll'>
nickamantia marked this conversation as resolved.
Show resolved Hide resolved
{isTableTag ? (
<table
{...ariaProps}
{...dataProps}
{...htmlProps}
className={classNames}
id={id}
>
{children}
</table>
) : (
<div
{...ariaProps}
{...dataProps}
{...htmlProps}
className={classNames}
id={id}
>
{children}
</div>
)}
</div>
) : (
isTableTag ? (
<table
{...ariaProps}
{...dataProps}
{...htmlProps}
className={classNames}
id={id}
>
{children}
</table>
) : (
<div
{...ariaProps}
{...dataProps}
{...htmlProps}
className={classNames}
id={id}
>
{children}
</div>
)
)}
</>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import React from 'react'
import Table from '../_table'

const TableStickyLeftColumns = () => {
return (
<Table
responsive="scroll"
size="md"
stickyLeftcolumn={["1", "2", "3"]}
>
<thead>
<tr>
<th id="1">{'Column 1'}</th>
<th id="2">{'Column 2'}</th>
<th id="3">{'Column 3'}</th>
<th>{'Column 4'}</th>
<th>{'Column 5'}</th>
<th>{'Column 6'}</th>
<th>{'Column 7'}</th>
<th>{'Column 8'}</th>
<th>{'Column 9'}</th>
<th>{'Column 10'}</th>
<th>{'Column 11'}</th>
<th>{'Column 12'}</th>
<th>{'Column 13'}</th>
<th>{'Column 14'}</th>
<th>{'Column 15'}</th>
</tr>
</thead>
<tbody>
<tr>
<td id="1">{'Value 1'}</td>
<td id="2">{'Value 2'}</td>
<td id="3">{'Value 3'}</td>
<td>{'Value 4'}</td>
<td>{'Value 5'}</td>
<td>{'Value 6'}</td>
<td>{'Value 7'}</td>
<td>{'Value 8'}</td>
<td>{'Value 9'}</td>
<td>{'Value 10'}</td>
<td>{'Value 11'}</td>
<td>{'Value 12'}</td>
<td>{'Value 13'}</td>
<td>{'Value 14'}</td>
<td>{'Value 15'}</td>
</tr>
<tr>
<td id="1">{'Value 1'}</td>
<td id="2">{'Value 2'}</td>
<td id="3">{'Value 3'}</td>
<td>{'Value 4'}</td>
<td>{'Value 5'}</td>
<td>{'Value 6'}</td>
<td>{'Value 7'}</td>
<td>{'Value 8'}</td>
<td>{'Value 9'}</td>
<td>{'Value 10'}</td>
<td>{'Value 11'}</td>
<td>{'Value 12'}</td>
<td>{'Value 13'}</td>
<td>{'Value 14'}</td>
<td>{'Value 15'}</td>
</tr>
<tr>
<td id="1">{'Value 1'}</td>
<td id="2">{'Value 2'}</td>
<td id="3">{'Value 3'}</td>
<td>{'Value 4'}</td>
<td>{'Value 5'}</td>
<td>{'Value 6'}</td>
<td>{'Value 7'}</td>
<td>{'Value 8'}</td>
<td>{'Value 9'}</td>
<td>{'Value 10'}</td>
<td>{'Value 11'}</td>
<td>{'Value 12'}</td>
<td>{'Value 13'}</td>
<td>{'Value 14'}</td>
<td>{'Value 15'}</td>
</tr>
</tbody>
</Table>
)
}

export default TableStickyLeftColumns
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The `stickyLeftcolumn` take in an array of strings of the column ids you want to be sticky. Make sure to add the corresponding id to the `<th>` and `<td>`.
If you are using the sub-component variant, then you will pass the id to `<Table.Header>` and `<Table.Cell>`
1 change: 1 addition & 0 deletions playbook/app/pb_kits/playbook/pb_table/docs/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ examples:
- table_md: Medium
- table_lg: Large
- table_sticky: Sticky Header
- table_sticky_left_columns: Sticky Left Column
- table_alignment_row: Row Alignment
- table_alignment_column: Cell Alignment
- table_alignment_shift_row: Row Shift
Expand Down
1 change: 1 addition & 0 deletions playbook/app/pb_kits/playbook/pb_table/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export { default as TableDiv } from './_table_div.jsx'
export { default as TableWithSubcomponents } from './_table_with_subcomponents.jsx'
export { default as TableWithSubcomponentsAsDivs } from './_table_with_subcomponents_as_divs.jsx'
export { default as TableOuterPadding } from './_table_outer_padding.jsx'
export { default as TableStickyLeftColumns } from './_table_sticky_left_columns.jsx'
2 changes: 2 additions & 0 deletions playbook/app/pb_kits/playbook/pb_table/styles/_all.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
@import "table_header";
@import "striped";
@import "outer_padding";
@import "sticky_columns";
@import "scroll";
4 changes: 4 additions & 0 deletions playbook/app/pb_kits/playbook/pb_table/styles/_scroll.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.table-responsive-scroll {
display: block;
overflow-x: auto;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@import "../../tokens/colors";

[class^="pb_table"] {
.sticky {
position: sticky !important;
left: 0;
z-index: 1;
background-color: white;
}

.with-border {
border-right: 1px solid $border_light !important;
}

.sticky-shadow {
box-shadow: 4px 0 10px rgba(60, 106, 172, 0.16) !important;
}
}
Loading