Resizable columns on the document table

This commit is contained in:
Laurent Nguyen
2024-02-27 14:21:26 +01:00
parent ed95f0e922
commit 2165d968b9

View File

@@ -1,4 +1,4 @@
import { Table, TableBody, TableCell, TableCellLayout, TableColumnDefinition, TableHeader, TableHeaderCell, TableRow, TableRowId, TableSelectionCell, createTableColumn, useArrowNavigationGroup, useTableFeatures, useTableSelection } from '@fluentui/react-components'; import { Menu, MenuItem, MenuList, MenuPopover, MenuTrigger, Table, TableBody, TableCell, TableCellLayout, TableColumnDefinition, TableColumnSizingOptions, TableHeader, TableHeaderCell, TableRow, TableRowId, TableSelectionCell, createTableColumn, useArrowNavigationGroup, useTableColumnSizing_unstable, useTableFeatures, useTableSelection } from '@fluentui/react-components';
import React, { useEffect } from 'react'; import React, { useEffect } from 'react';
export type DocumentsTableComponentItem = { export type DocumentsTableComponentItem = {
@@ -12,50 +12,60 @@ export interface IDocumentsTableComponentProps {
style?: React.CSSProperties; style?: React.CSSProperties;
} }
const columns: TableColumnDefinition<DocumentsTableComponentItem>[] = [
createTableColumn<DocumentsTableComponentItem>({
columnId: "id",
compare: (a, b) => {
return a.id.localeCompare(b.id);
},
renderHeaderCell: () => {
return "id";
},
renderCell: (item) => {
return (
<TableCellLayout>{item.id}</TableCellLayout>
);
},
}),
createTableColumn<DocumentsTableComponentItem>({
columnId: "type",
compare: (a, b) => {
return a.type.localeCompare(b.type);
},
renderHeaderCell: () => {
return "/type";
},
renderCell: (item) => {
return (
<TableCellLayout>{item.type}</TableCellLayout>
);
},
}),
];
export const DocumentsTableComponent: React.FC<IDocumentsTableComponentProps> = ({ export const DocumentsTableComponent: React.FC<IDocumentsTableComponentProps> = ({
items, onSelectedItem, style, items, onSelectedItem, style,
}: IDocumentsTableComponentProps) => { }: IDocumentsTableComponentProps) => {
const columns: TableColumnDefinition<DocumentsTableComponentItem>[] = [ const [columnSizingOptions, setColumnSizingOptions] = React.useState<TableColumnSizingOptions>({
createTableColumn<DocumentsTableComponentItem>({ id: {
columnId: "id", idealWidth: 300,
compare: (a, b) => { minWidth: 273,
return a.id.localeCompare(b.id); },
}, type: {
renderHeaderCell: () => { minWidth: 110,
return "id"; defaultWidth: 120,
}, },
renderCell: (item) => { });
return (
<TableCellLayout>{item.id}</TableCellLayout>
);
},
}),
createTableColumn<DocumentsTableComponentItem>({
columnId: "type",
compare: (a, b) => {
return a.type.localeCompare(b.type);
},
renderHeaderCell: () => {
return "/type";
},
renderCell: (item) => {
return (
<TableCellLayout>{item.type}</TableCellLayout>
);
},
}),
];
// const [columnSizingOptions] = React.useState<TableColumnSizingOptions>({ const onColumnResize = React.useCallback((_, { columnId, width }) => {
// id: { setColumnSizingOptions((state) => ({
// idealWidth: 300, ...state,
// minWidth: 150, [columnId]: {
// }, ...state[columnId],
// type: { idealWidth: width,
// minWidth: 110, },
// defaultWidth: 250, }));
// }, }, []);
// });
const [selectedRows, setSelectedRows] = React.useState<Set<TableRowId>>( const [selectedRows, setSelectedRows] = React.useState<Set<TableRowId>>(
() => new Set<TableRowId>([0]) () => new Set<TableRowId>([0])
@@ -63,8 +73,8 @@ export const DocumentsTableComponent: React.FC<IDocumentsTableComponentProps> =
const { const {
getRows, getRows,
// columnSizing_unstable, columnSizing_unstable: columnSizing,
// tableRef, tableRef,
selection: { selection: {
allRowsSelected, allRowsSelected,
someRowsSelected, someRowsSelected,
@@ -78,7 +88,7 @@ export const DocumentsTableComponent: React.FC<IDocumentsTableComponentProps> =
items, items,
}, },
[ [
// useTableColumnSizing_unstable({ columnSizingOptions }), useTableColumnSizing_unstable({ columnSizingOptions, onColumnResize }),
useTableSelection({ useTableSelection({
selectionMode: "multiselect", selectionMode: "multiselect",
selectedItems: selectedRows, selectedItems: selectedRows,
@@ -126,10 +136,10 @@ export const DocumentsTableComponent: React.FC<IDocumentsTableComponentProps> =
const tableProps = { const tableProps = {
"aria-label": "Filtered documents table", "aria-label": "Filtered documents table",
role: "grid", role: "grid",
...columnSizing.getTableProps(),
...keyboardNavAttr, ...keyboardNavAttr,
// ...columnSizing_unstable.getTableProps(),
size: "extra-small", size: "extra-small",
// ref: tableRef, ref: tableRef,
...style, ...style,
}; };
@@ -145,8 +155,29 @@ export const DocumentsTableComponent: React.FC<IDocumentsTableComponentProps> =
onKeyDown={toggleAllKeydown} onKeyDown={toggleAllKeydown}
checkboxIndicator={{ "aria-label": "Select all rows " }} checkboxIndicator={{ "aria-label": "Select all rows " }}
/> />
<TableHeaderCell>id</TableHeaderCell> {columns.map((column, index) => (
<TableHeaderCell>/type</TableHeaderCell> <Menu openOnContext key={column.columnId}>
<MenuTrigger>
<TableHeaderCell
key={column.columnId}
{...columnSizing.getTableHeaderCellProps(column.columnId)}
>
{column.renderHeaderCell()}
</TableHeaderCell>
</MenuTrigger>
<MenuPopover>
<MenuList>
<MenuItem
onClick={columnSizing.enableKeyboardMode(
column.columnId
)}
>
Keyboard Column Resizing
</MenuItem>
</MenuList>
</MenuPopover>
</Menu>
))}
</TableRow> </TableRow>
</TableHeader> </TableHeader>
<TableBody> <TableBody>
@@ -164,12 +195,16 @@ export const DocumentsTableComponent: React.FC<IDocumentsTableComponentProps> =
onClick={onClick} onClick={onClick}
onKeyDown={onKeyDown} onKeyDown={onKeyDown}
/> />
<TableCell onClick={(/* e */) => setSelectedRows(new Set<TableRowId>([index]))} onKeyDown={onKeyDown}> {columns.map((column) => (
<TableCellLayout>{item.id}</TableCellLayout> <TableCell
</TableCell> key={column.columnId}
<TableCell> onClick={(/* e */) => setSelectedRows(new Set<TableRowId>([index]))}
<TableCellLayout>{item.type}</TableCellLayout> onKeyDown={onKeyDown}
</TableCell> {...columnSizing.getTableCellProps(column.columnId)}
>
{column.renderCell(item)}
</TableCell>
))}
</TableRow> </TableRow>
))} ))}
</TableBody> </TableBody>