Skip to content

Commit

Permalink
fix(useTable): sort should adapt to enhanced row types (#25487)
Browse files Browse the repository at this point in the history
* fix(useTable): sort should adapt to enhanced row types

```ts
const rows = sort(getRows(row) => ({...row, foo: 'foo'}))

// Error!
console.log(rows[0].foo)
```

* update md

* changefile
  • Loading branch information
ling1726 authored Nov 2, 2022
1 parent e807070 commit 072f041
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "fix(useTable): sort should adapt to enhanced row types",
"packageName": "@fluentui/react-table",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export type TableSlots = {
export interface TableSortState<TItem> {
getSortDirection: (columnId: ColumnId) => SortDirection | undefined;
setColumnSort: (columnId: ColumnId, sortDirection: SortDirection) => void;
sort: (rows: RowState<TItem>[]) => RowState<TItem>[];
sort: <TRowState extends RowState<TItem>>(rows: TRowState[]) => TRowState[];
sortColumn: ColumnId | undefined;
sortDirection: SortDirection;
toggleColumnSort: (columnId: ColumnId) => void;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-components/react-table/src/hooks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface TableSortState<TItem> {
/**
* Sorts rows and returns a **shallow** copy of original items
*/
sort: (rows: RowState<TItem>[]) => RowState<TItem>[];
sort: <TRowState extends RowState<TItem>>(rows: TRowState[]) => TRowState[];
}

export interface TableSelectionState {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-components/react-table/src/hooks/useSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const noop = () => undefined;
export const defaultTableSortState: TableSortState<unknown> = {
getSortDirection: () => 'ascending',
setColumnSort: noop,
sort: (rows: RowState<unknown>[]) => [...rows],
sort: <TRowState extends RowState<unknown>>(rows: TRowState[]) => [...rows],
sortColumn: undefined,
sortDirection: 'ascending',
toggleColumnSort: noop,
Expand Down Expand Up @@ -53,7 +53,7 @@ export function useSortState<TItem>(tableState: TableState<TItem>, options: UseS
setSorted(newState);
};

const sort = (rows: RowState<TItem>[]) => {
const sort = <TRowState extends RowState<TItem>>(rows: TRowState[]) => {
return rows.slice().sort((a, b) => {
const sortColumnDef = columns.find(column => column.columnId === sortColumn);
if (!sortColumnDef?.compare) {
Expand Down

0 comments on commit 072f041

Please sign in to comment.