Skip to content

Commit f2f4733

Browse files
feat(muk): update datagrid attributes
ref: #MANAGER-20066 Signed-off-by: Alex Boungnaseng <[email protected]>
1 parent db6a6b7 commit f2f4733

File tree

14 files changed

+205
-62
lines changed

14 files changed

+205
-62
lines changed

packages/manager-ui-kit/src/components/datagrid/Datagrid.component.tsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { useRef } from 'react';
22

3-
import { Table } from '@ovhcloud/ods-react';
3+
import { TABLE_SIZE, TABLE_VARIANT, Table } from '@ovhcloud/ods-react';
44

5-
import { DatagridProps } from './Datagrid.props';
5+
import { ContainerHeight, DatagridProps, RowHeight } from './Datagrid.props';
66
import { TableBody } from './table/table-body/TableBody.component';
77
import { TableFooter } from './table/table-footer/TableFooter.component';
88
import { TableHeaderContent } from './table/table-head/table-header-content/TableHeaderContent.component';
99
import { Topbar } from './topbar/Topbar.component';
1010
import './translations';
1111
import { useDatagrid } from './useDatagrid';
1212

13-
const DEFAULT_ROW_HEIGHT = 50;
14-
const DEFAULT_CONTAINER_HEIGHT = 570;
15-
1613
export const Datagrid = <T extends Record<string, unknown>>({
1714
autoScroll = true,
1815
columns,
@@ -24,18 +21,22 @@ export const Datagrid = <T extends Record<string, unknown>>({
2421
filters,
2522
hasNextPage,
2623
isLoading,
27-
maxRowHeight = DEFAULT_ROW_HEIGHT,
24+
maxRowHeight,
2825
resourceType,
2926
rowSelection,
3027
search,
3128
sorting,
29+
size = TABLE_SIZE.md,
3230
subComponentHeight,
3331
topbar,
3432
totalCount,
33+
variant = TABLE_VARIANT.default,
3534
onFetchAllPages,
3635
onFetchNextPage,
3736
renderSubComponent,
3837
}: DatagridProps<T>) => {
38+
const rowHeight = RowHeight[size];
39+
const DEFAULT_CONTAINER_HEIGHT = maxRowHeight || ContainerHeight[size];
3940
const {
4041
features,
4142
getHeaderGroups,
@@ -55,21 +56,31 @@ export const Datagrid = <T extends Record<string, unknown>>({
5556
setColumnVisibility: columnVisibility?.setColumnVisibility,
5657
rowSelection,
5758
expandable,
59+
sizeRow: size,
5860
});
59-
const { hasSortingFeature, hasSearchFeature, hasColumnVisibilityFeature, hasFilterFeature } =
60-
features;
61+
const {
62+
hasSortingFeature,
63+
hasSearchFeature,
64+
hasColumnVisibilityFeature,
65+
hasFilterFeature,
66+
} = features;
6167
const rowModel = getRowModel();
6268
const { rows } = rowModel;
6369
const headerGroups = getHeaderGroups();
6470
const tableContainerRef = useRef<HTMLDivElement>(null);
6571
const visibleColumns = getAllLeafColumns();
66-
const containerSize = data?.length < 10 ? '100%' : `${DEFAULT_CONTAINER_HEIGHT}px`;
72+
const containerSize =
73+
data?.length < 10 ? '100%' : `${DEFAULT_CONTAINER_HEIGHT}px`;
6774
const containerStyle = {
6875
maxHeight: containerHeight ? `${containerHeight}px` : containerSize,
6976
height: containerHeight ? `${containerHeight}px` : containerSize,
7077
};
7178
const shouldRenderTopbar =
72-
topbar || hasSearchFeature || hasFilterFeature || hasColumnVisibilityFeature;
79+
topbar ||
80+
hasSearchFeature ||
81+
hasFilterFeature ||
82+
hasColumnVisibilityFeature;
83+
7384
return (
7485
<>
7586
{shouldRenderTopbar && (
@@ -89,8 +100,12 @@ export const Datagrid = <T extends Record<string, unknown>>({
89100
setColumnVisibility={columnVisibility?.setColumnVisibility}
90101
/>
91102
)}
92-
<div className="overflow-auto relative w-full" ref={tableContainerRef} style={containerStyle}>
93-
<Table className="w-full">
103+
<div
104+
className="overflow-auto relative w-full"
105+
ref={tableContainerRef}
106+
style={containerStyle}
107+
>
108+
<Table size={size} variant={variant}>
94109
<TableHeaderContent<T>
95110
headerGroups={headerGroups}
96111
onSortChange={sorting?.setSorting}
@@ -106,7 +121,7 @@ export const Datagrid = <T extends Record<string, unknown>>({
106121
isLoading={isLoading ?? false}
107122
renderSubComponent={renderSubComponent}
108123
subComponentHeight={subComponentHeight}
109-
maxRowHeight={maxRowHeight}
124+
maxRowHeight={rowHeight}
110125
contentAlignLeft={contentAlignLeft}
111126
/>
112127
</Table>

packages/manager-ui-kit/src/components/datagrid/Datagrid.props.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
VisibilityState,
1111
} from '@tanstack/react-table';
1212

13+
import { TABLE_SIZE, TABLE_VARIANT } from '@ovhcloud/ods-react';
14+
1315
import {
1416
FilterTypeCategories as DatagridColumnTypes,
1517
FilterComparator,
@@ -78,9 +80,11 @@ export type DatagridProps<T extends Record<string, unknown>> = {
7880
rowSelection?: RowSelectionProps<T>;
7981
search?: SearchProps;
8082
sorting?: SortingProps;
83+
size?: TABLE_SIZE;
8184
subComponentHeight?: number;
8285
topbar?: ReactNode;
8386
totalCount?: number;
87+
variant?: TABLE_VARIANT;
8488
onFetchAllPages?: () => void;
8589
onFetchNextPage?: () => void;
8690
renderSubComponent?: (
@@ -95,7 +99,18 @@ export enum ColumnMetaType {
9599
BADGE = 'badge',
96100
}
97101

98-
// export type ManagerColumnDef<T> = ColumnDef<T> & {
102+
export enum RowHeight {
103+
sm = 32.5,
104+
md = 48.5,
105+
lg = 64.5,
106+
}
107+
108+
export enum ContainerHeight {
109+
sm = 375,
110+
md = 550,
111+
lg = 725,
112+
}
113+
99114
export type DatagridColumn<T> = ColumnDef<T> & {
100115
/** set column comparator for the filter */
101116
comparator?: FilterComparator[];

packages/manager-ui-kit/src/components/datagrid/builder/TableBuilderProps.props.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { MutableRefObject } from 'react';
22

3-
import { ColumnDef, ColumnSort, Row, VisibilityState } from '@tanstack/react-table';
3+
import {
4+
ColumnDef,
5+
ColumnSort,
6+
Row,
7+
VisibilityState,
8+
} from '@tanstack/react-table';
9+
10+
import { TABLE_SIZE } from '@ovhcloud/ods-react';
411

512
import { ExpandedProps, RowSelectionProps } from '../Datagrid.props';
613
import { ExpandableRow } from '../useDatagrid.props';
@@ -21,4 +28,5 @@ export type TableBuilderProps<T extends ExpandableRow<T>> = {
2128
rowSelection: RowSelectionProps<T>;
2229
setColumnVisibility: (columnVisibility: VisibilityState) => void;
2330
sorting: ColumnSort[];
31+
size: TABLE_SIZE;
2432
};

packages/manager-ui-kit/src/components/datagrid/builder/TableHeaderBuilder.tsx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,44 @@ import {
77
CheckboxControl,
88
ICON_NAME,
99
Icon,
10+
TABLE_SIZE,
1011
} from '@ovhcloud/ods-react';
1112

1213
import { Button } from '../../button';
1314
import { ExpandedProps } from '../Datagrid.props';
1415

15-
export const getExpandable = <T,>(expandable: ExpandedProps) => ({
16+
export const getExpandable = <T,>(
17+
expandable: ExpandedProps,
18+
size: TABLE_SIZE,
19+
) => ({
1620
cell: ({ row }: { row: Row<T> }) => {
21+
const ButtonSize =
22+
size === TABLE_SIZE.sm
23+
? BUTTON_SIZE.xs
24+
: size === TABLE_SIZE.md
25+
? BUTTON_SIZE.sm
26+
: BUTTON_SIZE.md;
27+
console.info('ButtonSize : ', ButtonSize);
1728
return row.getCanExpand() ? (
1829
<div
19-
className="text-center"
2030
style={{
2131
...(expandable && { paddingLeft: `${row.depth * 2}rem` }),
2232
}}
2333
>
2434
{expandable && row.depth ? null : (
2535
<Button
36+
className="m-0 p-0"
2637
onClick={row.getToggleExpandedHandler()}
2738
variant={BUTTON_VARIANT.ghost}
28-
size={BUTTON_SIZE.xs}
39+
size={ButtonSize}
2940
>
30-
<Icon name={row.getIsExpanded() ? ICON_NAME.chevronDown : ICON_NAME.chevronRight} />
41+
<Icon
42+
name={
43+
row.getIsExpanded()
44+
? ICON_NAME.chevronDown
45+
: ICON_NAME.chevronRight
46+
}
47+
/>
3148
</Button>
3249
)}
3350
</div>
@@ -36,7 +53,8 @@ export const getExpandable = <T,>(expandable: ExpandedProps) => ({
3653
enableHiding: false,
3754
enableResizing: true,
3855
id: 'expander',
39-
maxSize: 20,
56+
maxSize: 30,
57+
minSize: 30,
4058
});
4159

4260
export const getRowSelection = <T,>() => ({
@@ -52,7 +70,6 @@ export const getRowSelection = <T,>() => ({
5270
</Checkbox>
5371
),
5472
enableHiding: true,
55-
enableResizing: true,
5673
header: ({ table: tableHeader }) => (
5774
<Checkbox
5875
id="select-all"
@@ -66,5 +83,7 @@ export const getRowSelection = <T,>() => ({
6683
</Checkbox>
6784
),
6885
id: 'select',
69-
maxSize: 20,
86+
enableResizing: false,
87+
maxSize: 30,
88+
minSize: 30,
7089
});

packages/manager-ui-kit/src/components/datagrid/builder/useTableBuilder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const useTableBuilder = <T extends ExpandableRow<T>>({
2222
rowSelection,
2323
setColumnVisibility,
2424
sorting,
25+
size,
2526
}: TableBuilderProps<T>) => {
2627
const params: Partial<TableOptions<T>> = {};
2728
const builder = {
@@ -32,7 +33,7 @@ export const useTableBuilder = <T extends ExpandableRow<T>>({
3233
cols = [getRowSelection(), ...cols];
3334
}
3435
if ((hasExpandableFeature && expandable) || renderSubComponent) {
35-
cols = [getExpandable(expandable), ...cols];
36+
cols = [getExpandable(expandable, size), ...cols];
3637
}
3738
params.columns = cols;
3839
return builder;

packages/manager-ui-kit/src/components/datagrid/table/table-body/TableBody.component.tsx

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,64 @@ export const TableBody = <T,>({
2929
estimateSize: useCallback(() => maxRowHeight, [maxRowHeight]),
3030
getScrollElement: () => tableContainerRef.current,
3131
measureElement:
32-
typeof window !== 'undefined' && navigator.userAgent.indexOf('Firefox') === -1
32+
typeof window !== 'undefined' &&
33+
navigator.userAgent.indexOf('Firefox') === -1
3334
? (el) => el?.getBoundingClientRect().height
3435
: undefined,
3536
overscan: 15,
3637
});
3738

3839
useEffect(() => {
39-
if (autoScroll && previousRowsLength !== undefined && rows?.length > previousRowsLength) {
40+
if (
41+
autoScroll &&
42+
previousRowsLength !== undefined &&
43+
rows?.length > previousRowsLength
44+
) {
4045
rowVirtualizer.scrollToIndex(previousRowsLength, { align: 'start' });
4146
}
4247
}, [rows?.length]);
4348

44-
const EXPANDED_SIZE = subComponentHeight;
4549
const getOffset = useCallback(
46-
(index: number) => {
50+
(index: number, size: number) => {
4751
let count = 0;
52+
console.info('getOffset size : ', size);
4853
for (let i = 0; i < index; i += 1) {
4954
if (rows[i]?.getIsExpanded()) count += 1;
5055
}
51-
return count * EXPANDED_SIZE;
56+
return count * subComponentHeight;
5257
},
53-
[rows],
58+
[subComponentHeight],
5459
);
5560

5661
const totalHeight = useMemo(() => {
5762
const total = rows.reduce((acc, row) => {
58-
return acc + maxRowHeight + (row.getIsExpanded() ? subComponentHeight : 0);
63+
return (
64+
acc + maxRowHeight + (row.getIsExpanded() ? subComponentHeight : 0)
65+
);
5966
}, 0);
60-
return isLoading ? total + (pageSize - 1) * maxRowHeight : total + rows.length;
67+
return isLoading
68+
? total + (pageSize - 1) * maxRowHeight
69+
: total + rows.length;
6170
}, [rows, maxRowHeight, subComponentHeight, isLoading, expanded]);
6271

6372
if (rows?.length === 0 && !isLoading) {
6473
return <EmptyRow />;
6574
}
6675

76+
console.info('tableBody totalHeight : ', totalHeight);
6777
return (
6878
<tbody
6979
key={`table-body-${rows.length}`}
70-
className="w-full relative p-0 overflow-hidden"
80+
className="relative p-0 overflow-hidden"
7181
style={{
7282
height: totalHeight,
7383
}}
7484
>
7585
{rowVirtualizer.getVirtualItems().map((virtualRow) => {
7686
const row = rows[virtualRow?.index] as Row<T>;
77-
const offset = renderSubComponent ? getOffset(virtualRow?.index) : 0;
87+
const offset = renderSubComponent
88+
? getOffset(virtualRow?.index, virtualRow?.size)
89+
: 0;
7890
return (
7991
<Fragment key={`table-body-tr-${row.id}`}>
8092
<tr
@@ -91,15 +103,18 @@ export const TableBody = <T,>({
91103
{row.getVisibleCells().map((cell) => (
92104
<td
93105
key={cell.id}
94-
className={`py-[8px] ${contentAlignLeft ? 'pl-4' : 'text-center'}`}
106+
className={`${contentAlignLeft ? 'pl-4' : 'text-center'}`}
95107
style={{
96108
width: cell.column.getSize(),
97109
minWidth: cell.column.columnDef.minSize ?? 0,
98110
maxWidth: cell.column.columnDef.maxSize ?? 'auto',
99111
borderTop: 'none',
100112
}}
101113
>
102-
<div className="overflow-hidden text-ellipsis block w-full">
114+
<div
115+
className="overflow-hidden text-ellipsis flex items-center w-full"
116+
style={{ lineHeight: 1 }}
117+
>
103118
{flexRender(cell.column.columnDef.cell, cell.getContext())}
104119
</div>
105120
</td>
@@ -113,7 +128,7 @@ export const TableBody = <T,>({
113128
offset={offset}
114129
renderSubComponent={renderSubComponent}
115130
subComponentHeight={subComponentHeight}
116-
maxRowHeight={maxRowHeight}
131+
maxRowHeight={virtualRow?.size ?? maxRowHeight}
117132
/>
118133
)}
119134
</Fragment>

packages/manager-ui-kit/src/components/datagrid/table/table-body/sub-row/SubRow.component.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,17 @@ export const SubRow = ({
1313
<tr
1414
key={`${row.id}-expanded-tr`}
1515
data-index={`${virtualRow.index}-expanded-tr`}
16-
className={`overflow-hidden absolute top-0 display-table table-layout-fixed`}
16+
className={`overflow-hidden absolute top-0 display-table table-layout-fixed h-full`}
1717
style={{
1818
left: -1,
1919
height: `${subComponentHeight}px`,
2020
width: '-webkit-fill-available',
21-
borderRight: '1px solid var(--ods-color-neutral-100)',
22-
borderLeft: '1px solid var(--ods-color-neutral-100)',
23-
borderBottom: '1px solid var(--ods-color-neutral-100)',
24-
transform: `translateY(${virtualRow.start + offset + maxRowHeight}px)`,
21+
transform: `translateY(${virtualRow.start + offset + maxRowHeight - 1}px)`,
2522
}}
2623
>
27-
<div className="overflow-hidden p-[8px] text-ellipsis block w-full">
24+
<td className="overflow-hidden p-[8px] text-ellipsis block w-full h-full">
2825
{renderSubComponent(row)}
29-
</div>
26+
</td>
3027
</tr>
3128
);
3229

0 commit comments

Comments
 (0)