diff --git a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTableComponent.tsx b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTableComponent.tsx index 32aa19954..738f35dfb 100644 --- a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTableComponent.tsx +++ b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTableComponent.tsx @@ -12,6 +12,7 @@ import { TableCell, TableCellLayout, TableColumnDefinition, + TableColumnId, TableColumnSizingOptions, TableHeader, TableHeaderCell, @@ -22,13 +23,17 @@ import { useTableColumnSizing_unstable, useTableFeatures, useTableSelection, + useTableSort } from "@fluentui/react-components"; import { ArrowClockwise16Regular, + ArrowResetRegular, DeleteRegular, EditRegular, MoreHorizontalRegular, TableResizeColumnRegular, + TextSortAscendingRegular, + TextSortDescendingRegular, } from "@fluentui/react-icons"; import { NormalizedEventKey } from "Common/Constants"; import { TableColumnSelectionPane } from "Explorer/Panes/TableColumnSelectionPane/TableColumnSelectionPane"; @@ -99,11 +104,13 @@ export const DocumentsTableComponent: React.FC = }); const [columnSizingOptions, setColumnSizingOptions] = React.useState(initialSizingOptions); - - // This is for the menu to select columns - // const [columnSearchText, setColumnSearchText] = useState(""); - - // const restoreFocusTargetAttribute = useRestoreFocusTarget(); + const [sortState, setSortState] = React.useState<{ + sortDirection: "ascending" | "descending"; + sortColumn: TableColumnId | undefined; + }>({ + sortDirection: undefined, + sortColumn: undefined, + }); const onColumnResize = React.useCallback( (_, { columnId, width }) => { @@ -119,6 +126,8 @@ export const DocumentsTableComponent: React.FC = [_onColumnResize], ); + // const restoreFocusTargetAttribute = useRestoreFocusTarget(); + // Columns must be a static object and cannot change on re-renders otherwise React will complain about too many refreshes const columns: TableColumnDefinition[] = useMemo( () => @@ -155,6 +164,9 @@ export const DocumentsTableComponent: React.FC = } onClick={onRefreshTable}> Refresh + } onClick={(e) => setColumnSort(e, column.id, "ascending")}>Sort ascending + } onClick={(e) => setColumnSort(e, column.id, "descending")}>Sort descending + } onClick={(e) => setColumnSort(e, undefined, undefined)}>Reset sorting } onClick={openColumnSelectionPane}> Edit columns @@ -278,6 +290,7 @@ export const DocumentsTableComponent: React.FC = columnSizing_unstable: columnSizing, tableRef, selection: { allRowsSelected, someRowsSelected, toggleAllRows, toggleRow, isRowSelected }, + sort: { getSortDirection, setColumnSort, sort }, } = useTableFeatures( { columns, @@ -291,10 +304,19 @@ export const DocumentsTableComponent: React.FC = // eslint-disable-next-line react/prop-types onSelectionChange: (e, data) => onSelectedRowsChange(data.selectedItems), }), + useTableSort({ + sortState, + onSortChange: (e, nextSortState) => setSortState(nextSortState), + }), ], ); - const rows: TableRowData[] = getRows((row) => { + const headerSortProps = (columnId: TableColumnId) => ({ + // onClick: (e: React.MouseEvent) => toggleColumnSort(e, columnId), + sortDirection: getSortDirection(columnId), + }); + + const rows: TableRowData[] = sort(getRows((row) => { const selected = isRowSelected(row.rowId); return { ...row, @@ -308,7 +330,7 @@ export const DocumentsTableComponent: React.FC = selected, appearance: selected ? ("brand" as const) : ("none" as const), }; - }); + })); const toggleAllKeydown = React.useCallback( (e: React.KeyboardEvent) => { @@ -357,7 +379,7 @@ export const DocumentsTableComponent: React.FC = }; return ( - +
{!isSelectionDisabled && ( @@ -374,6 +396,7 @@ export const DocumentsTableComponent: React.FC = className="documentsTableCell" key={column.columnId} {...columnSizing.getTableHeaderCellProps(column.columnId)} + {...headerSortProps(column.columnId)} > {column.renderHeaderCell()}