From 77a3fe58dd5d9a18f3ce4881ce2efeacc179d799 Mon Sep 17 00:00:00 2001 From: Lingfan Gao Date: Wed, 2 Nov 2022 13:04:06 +0100 Subject: [PATCH 1/3] fix(useTable): sort should adapt to enhanced row types ```ts const rows = sort(getRows(row) => ({...row, foo: 'foo'})) // Error! console.log(rows[0].foo) ``` --- packages/react-components/react-table/src/hooks/types.ts | 2 +- packages/react-components/react-table/src/hooks/useSort.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/react-components/react-table/src/hooks/types.ts b/packages/react-components/react-table/src/hooks/types.ts index 06fdb272924f6d..467cd86f836ed7 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 fae66913632d02..54646d422dc8eb 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) { From f490107639d546b1296a42eed7e55001db8db6e2 Mon Sep 17 00:00:00 2001 From: Lingfan Gao Date: Wed, 2 Nov 2022 13:10:04 +0100 Subject: [PATCH 2/3] update md --- packages/react-components/react-table/etc/react-table.api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 04ecce72040d80..b6132dd2db27ee 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; From f1853a10c9adac1d9332c1c3229dc8a14ab771cf Mon Sep 17 00:00:00 2001 From: Lingfan Gao Date: Wed, 2 Nov 2022 13:10:53 +0100 Subject: [PATCH 3/3] changefile --- ...i-react-table-c3838956-bc8b-4f1d-bcc9-3e89d914bb6f.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 change/@fluentui-react-table-c3838956-bc8b-4f1d-bcc9-3e89d914bb6f.json 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 00000000000000..3550054b534d5f --- /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" +}