Skip to content

Commit 86d930c

Browse files
author
Hector Arce De Las Heras
committed
Implemented sticky column headers and scroll features in table
This commit addresses the following enhancements in the table component: 1. When the user scrolls, the column header now remains sticky with a shadow effect, improving readability and user experience. 2. Only vertical scrolling is enabled, providing a more focused view of the data. 3. The maximum height of the table is set to 768px. Beyond this height, scrolling is enabled on desktop devices, ensuring the table remains within a manageable size.
1 parent 525da9c commit 86d930c

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

src/components/table/component/table.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const TableComponent = (
5252
>
5353
<TableCaptionStyled id={uniqueId}>{props.captionDescription}</TableCaptionStyled>
5454
<TableRowGroupHeaderStyled
55+
$scrolling={props.scrolling}
5556
hidden={props.hiddenHeaderOn?.[props.device]}
5657
lineSeparatorBottomOnHeader={props.lineSeparatorBottomOnHeader}
5758
lineSeparatorLineStyles={props.lineSeparatorLineStyles}

src/components/table/table.styled.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
LineSeparatorPositionType,
66
} from '@/components/lineSeparator';
77
import { srOnlyMixin } from '@/styles/mixins/srOnly.mixin';
8-
import { CommonStyleType } from '@/types';
8+
import { CommonStyleType, DeviceBreakpointsType } from '@/types';
99
import { getStyles, getTypographyStyles } from '@/utils/getStyles/getStyles';
1010

1111
import {
@@ -59,6 +59,7 @@ export const TableRowGroupHeaderStyled = styled.thead<{
5959
lineSeparatorLineStyles?: LineSeparatorLinePropsStylesType;
6060
lineSeparatorTopOnHeader?: boolean;
6161
lineSeparatorBottomOnHeader?: boolean;
62+
$scrolling?: boolean;
6263
}>`
6364
${({ styles }) => getStyles(styles?.container)}
6465
${({ styles }) => getTypographyStyles(styles?.typography)}
@@ -77,6 +78,27 @@ export const TableRowGroupHeaderStyled = styled.thead<{
7778
}
7879
return lineSeparator;
7980
}}
81+
${() =>
82+
({
83+
theme: {
84+
MEDIA_QUERIES: { onlyDesktop },
85+
},
86+
styles,
87+
$scrolling,
88+
}) => {
89+
return css`
90+
${onlyDesktop} {
91+
${$scrolling
92+
? css`
93+
box-shadow: ${styles?.container?.[DeviceBreakpointsType.DESKTOP]?.box_shadow ??
94+
'none'};
95+
`
96+
: css`
97+
box-shadow: none;
98+
`}
99+
}
100+
`;
101+
}}
80102
`;
81103

82104
export const TableRowHeaderStyled = styled.tr<{

src/components/table/table.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,28 @@ const TableComponent = React.forwardRef(
2727
);
2828
const device = useMediaDevice();
2929

30+
// Indicates if there is scrolling behind the table body to add shadow to column headers.
31+
const [scrollPosition, setScrollPosition] = React.useState(0);
32+
const refTableBody = React.useRef<HTMLTableSectionElement>(null);
33+
34+
React.useEffect(() => {
35+
const updatePosition = () => {
36+
setScrollPosition(refTableBody?.current?.scrollTop ?? 0);
37+
};
38+
39+
refTableBody?.current?.addEventListener('scroll', updatePosition);
40+
41+
return () => refTableBody?.current?.removeEventListener('scroll', updatePosition);
42+
}, []);
43+
3044
return (
3145
<TableStandAlone
3246
{...props}
3347
ref={ref}
3448
device={device}
3549
lineSeparatorLineStyles={lineSeparatorLineStyles}
50+
refTableBody={refTableBody}
51+
scrolling={scrollPosition > 0}
3652
styles={styles}
3753
/>
3854
);
@@ -47,7 +63,7 @@ const TableBoundary = <V extends string | unknown>(
4763
<ErrorBoundary
4864
fallBackComponent={
4965
<FallbackComponent>
50-
<TableStandAlone {...(props as unknown as ITableStandAlone)} />
66+
<TableStandAlone {...(props as unknown as ITableStandAlone)} ref={ref} scrolling={false} />
5167
</FallbackComponent>
5268
}
5369
>

src/components/table/types/table.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ type TableAriaAttributes = Pick<
138138
*/
139139
export interface ITableStandAlone extends TableAriaAttributes {
140140
styles: TableRowHeaderTypes<string, string>;
141+
refTableBody?: React.Ref<HTMLTableSectionElement>;
141142
lineSeparatorLineStyles: LineSeparatorLinePropsStylesType;
142143
lineSeparatorTopOnHeader?: boolean;
143144
lineSeparatorBottomOnHeader?: boolean;
@@ -152,6 +153,7 @@ export interface ITableStandAlone extends TableAriaAttributes {
152153
dataTestId?: string;
153154
hiddenHeaderOn?: HiddenType;
154155
device: DeviceBreakpointsType;
156+
scrolling: boolean;
155157
headerVariant?: string;
156158
expandedContentHelpMessage?: string;
157159
formatListInMobile?: boolean;
@@ -169,7 +171,10 @@ export interface ITableStandAlone extends TableAriaAttributes {
169171
* @interface ITable
170172
*/
171173
export interface ITable<V = undefined extends string ? unknown : string>
172-
extends Omit<ITableStandAlone, 'styles' | 'lineSeparatorLineStyles' | 'device'>,
174+
extends Omit<
175+
ITableStandAlone,
176+
'styles' | 'lineSeparatorLineStyles' | 'device' | 'scrolling' | 'refTableBody'
177+
>,
173178
Omit<CustomTokenTypes<TableRowHeaderTypes<string, string>>, 'cts' | 'extraCt'> {
174179
variant: V;
175180
lineSeparatorLineVariant?: string;

0 commit comments

Comments
 (0)