forked from patternfly/react-component-groups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkeletonTableHead.tsx
More file actions
86 lines (78 loc) · 2.81 KB
/
Copy pathSkeletonTableHead.tsx
File metadata and controls
86 lines (78 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { FunctionComponent, ReactElement } from 'react';
import { ReactNode, useMemo, isValidElement } from 'react';
import {
Th,
ThProps,
Thead,
Tr
} from '@patternfly/react-table';
import { Skeleton } from '@patternfly/react-core';
export const isThObject = (value: ReactNode | { cell: ReactNode; props?: ThProps }): value is { cell: ReactNode; props?: ThProps } => value != null && typeof value === 'object' && 'cell' in value;
export const isReactElement = (value: ReactNode): value is ReactElement => isValidElement(value);
export interface SkeletonTableHeadProps {
/** Custom columns for the table */
columns?: (ReactNode | { cell: ReactNode; props?: ThProps })[];
/** Number of columns in the table */
columnsCount?: number;
/** Custom OUIA ID */
ouiaId?: string | number;
/** Flag indicating if the table is selectable */
isSelectable?: boolean;
/** Flag indicating if the table is expandable */
isExpandable?: boolean;
/** Flag indicating if the table is a tree table */
isTreeTable?: boolean;
}
export const SkeletonTableHead: FunctionComponent<SkeletonTableHeadProps> = ({
ouiaId = 'SkeletonTableHeader',
isSelectable,
isExpandable,
columnsCount,
columns,
isTreeTable,
...rest
}: SkeletonTableHeadProps) => {
const hasCustomColumns = Array.isArray(columns);
const rowCellsCount = hasCustomColumns ? columns.length : columnsCount;
const cells = useMemo(() => [
...(isExpandable ? [ <Th key="row-expand" screenReaderText='Data expansion table header cell' /> ] : []),
...(isSelectable && !isTreeTable ? [ <Th key="row-select" screenReaderText='Data selection table header cell' /> ] : []),
...(hasCustomColumns ? (
columns.map((column, index) => {
// If the column is an object with cell and props, wrap in Th
if (isThObject(column)) {
return (
<Th key={index} {...column.props} data-ouia-component-id={`${ouiaId}-th-${index}`}>
{column.cell}
</Th>
);
}
// If the column is already a React element (like <Th>), render it directly
if (isReactElement(column)) {
return column;
}
// Otherwise, wrap the content in Th
return (
<Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}>
{column}
</Th>
);
})
) : (
[ ...Array(rowCellsCount) ].map((_, index) => (
<Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}>
<Skeleton />
</Th>
))
))
]
, [ hasCustomColumns, isExpandable, isSelectable, isTreeTable, columns, rowCellsCount, ouiaId ]);
return (
<Thead data-ouia-component-id={`${ouiaId}-thead`} {...rest}>
<Tr ouiaId={`${ouiaId}-tr-head`}>
{cells}
</Tr>
</Thead>
);
};
export default SkeletonTableHead;