diff --git a/change/@fluentui-react-table-c3838956-bc8b-4f1d-bcc9-3e89d914bb6f.json b/change/@fluentui-react-table-c3838956-bc8b-4f1d-bcc9-3e89d914bb6f.json new file mode 100644 index 0000000000000..3550054b534d5 --- /dev/null +++ b/change/@fluentui-react-table-c3838956-bc8b-4f1d-bcc9-3e89d914bb6f.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "fix(useTable): sort should adapt to enhanced row types", + "packageName": "@fluentui/react-table", + "email": "lingfangao@hotmail.com", + "dependentChangeType": "patch" +} diff --git a/packages/react-components/react-table/etc/react-table.api.md b/packages/react-components/react-table/etc/react-table.api.md index 04ecce72040d8..b6132dd2db27e 100644 --- a/packages/react-components/react-table/etc/react-table.api.md +++ b/packages/react-components/react-table/etc/react-table.api.md @@ -427,7 +427,7 @@ export type TableSlots = { export interface TableSortState { getSortDirection: (columnId: ColumnId) => SortDirection | undefined; setColumnSort: (columnId: ColumnId, sortDirection: SortDirection) => void; - sort: (rows: RowState[]) => RowState[]; + sort: >(rows: TRowState[]) => TRowState[]; sortColumn: ColumnId | undefined; sortDirection: SortDirection; toggleColumnSort: (columnId: ColumnId) => void; diff --git a/packages/react-components/react-table/src/hooks/types.ts b/packages/react-components/react-table/src/hooks/types.ts index 06fdb272924f6..467cd86f836ed 100644 --- a/packages/react-components/react-table/src/hooks/types.ts +++ b/packages/react-components/react-table/src/hooks/types.ts @@ -44,7 +44,7 @@ export interface TableSortState { /** * Sorts rows and returns a **shallow** copy of original items */ - sort: (rows: RowState[]) => RowState[]; + sort: >(rows: TRowState[]) => TRowState[]; } export interface TableSelectionState { diff --git a/packages/react-components/react-table/src/hooks/useSort.ts b/packages/react-components/react-table/src/hooks/useSort.ts index fae66913632d0..54646d422dc8e 100644 --- a/packages/react-components/react-table/src/hooks/useSort.ts +++ b/packages/react-components/react-table/src/hooks/useSort.ts @@ -6,7 +6,7 @@ const noop = () => undefined; export const defaultTableSortState: TableSortState = { getSortDirection: () => 'ascending', setColumnSort: noop, - sort: (rows: RowState[]) => [...rows], + sort: >(rows: TRowState[]) => [...rows], sortColumn: undefined, sortDirection: 'ascending', toggleColumnSort: noop, @@ -53,7 +53,7 @@ export function useSortState(tableState: TableState, options: UseS setSorted(newState); }; - const sort = (rows: RowState[]) => { + const sort = >(rows: TRowState[]) => { return rows.slice().sort((a, b) => { const sortColumnDef = columns.find(column => column.columnId === sortColumn); if (!sortColumnDef?.compare) {