Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated sort state on dashboard refresh #163

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 1.8.0 (IN PROGRESS)

### Features / Enhancements

- Updated sort state on dashboard refresh (#163)

## 1.7.0 (2024-11-16)

### Features / Enhancements
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,5 @@
"test:e2e:docker": "docker compose --profile e2e up --exit-code-from test",
"upgrade": "npm upgrade --save"
},
"version": "1.7.0"
"version": "1.8.0"
}
56 changes: 56 additions & 0 deletions src/components/Table/hooks/useSortState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,60 @@ describe('useSortState', () => {

expect(result.current.sorting).toEqual([{ id: 'age', desc: true }]);
});

it('Should update sorting state when columns sort option is changed', () => {
const initialColumns = [
{
id: 'name',
enableSorting: true,
sortDescFirst: false,
},
];

const { result, rerender } = renderHook(({ columns }) => useSortState({ columns }), {
initialProps: { columns: initialColumns },
});

expect(result.current.sorting).toEqual([{ id: 'name', desc: false }]);

const newColumns = [
{
id: 'name',
enableSorting: true,
sortDescFirst: true,
},
];

rerender({ columns: newColumns });

expect(result.current.sorting).toEqual([{ id: 'name', desc: true }]);
});

it('Should update sorting state when column sort options is changed and no more sort columns', () => {
const initialColumns = [
{
id: 'name',
enableSorting: true,
sortDescFirst: false,
},
];

const { result, rerender } = renderHook(({ columns }) => useSortState({ columns }), {
initialProps: { columns: initialColumns },
});

expect(result.current.sorting).toEqual([{ id: 'name', desc: false }]);

const newColumns = [
{
id: 'name',
enableSorting: false,
sortDescFirst: false,
},
];

rerender({ columns: newColumns });

expect(result.current.sorting).toEqual([]);
});
});
29 changes: 27 additions & 2 deletions src/components/Table/hooks/useSortState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const useSortState = <TData>({ columns }: { columns: Array<ColumnDef<TDat
* State
*/
const [sorting, setSorting] = useState<SortingState>([]);
const [column, setColumnId] = useState<ColumnDef<TData>>();

/**
* Update Sorting State
Expand All @@ -19,8 +20,32 @@ export const useSortState = <TData>({ columns }: { columns: Array<ColumnDef<TDat
*/
const firstSortableColumn = columns.find((column) => column.enableSorting);

setSorting(firstSortableColumn ? [{ id: firstSortableColumn.id!, desc: firstSortableColumn.sortDescFirst! }] : []);
}, [columns]);
if (firstSortableColumn) {
/**
* Change if new column
*/
if (firstSortableColumn.id !== column?.id) {
setSorting([{ id: firstSortableColumn.id!, desc: firstSortableColumn.sortDescFirst! }]);
setColumnId(firstSortableColumn);
}

/**
* Change if sort direction for column is changed
*/
if (firstSortableColumn.id === column?.id && firstSortableColumn.sortDescFirst !== column?.sortDescFirst) {
setSorting([{ id: firstSortableColumn.id!, desc: firstSortableColumn.sortDescFirst! }]);
setColumnId(firstSortableColumn);
}
}

/**
* Reset state
*/
if (!firstSortableColumn && !!sorting.length) {
setSorting([]);
setColumnId(undefined);
}
}, [column, columns, sorting]);

/**
* Change state via table handler
Expand Down
Loading