Skip to content

Commit

Permalink
Fix bug when updating column within sorting entry
Browse files Browse the repository at this point in the history
  • Loading branch information
seancolsen committed Dec 7, 2023
1 parent d2afe77 commit 7e99f09
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,27 @@
}
function updateSortEntry(
oldColumnId: number,
columnId: number,
newColumnId: number,
sortDirection: SortDirection,
newSortDirection: SortDirection,
) {
sorting.update((s) => {
let newSort = s;
if (oldColumnId !== newColumnId) {
/**
* This check will ensure that the order of the sorters are not changed
* when the user changes the SortDirection of the top sorters
*/
newSort = newSort.without(oldColumnId);
}
return newSort.with(newColumnId, sortDirection);
});
// This logic may seem a bit complex for a simple update, but it's necessary
// to preserve the order of the sort entries in the store. We need to ensure
// that if the user changes the column or sort direction within a sort
// entry, then the position of that entry is preserved among all entries.
// This is tricky because the entries have no unique identifier. We map over
// all entries in order to build a new sorting object with the same order of
// entries.
sorting.update(
(oldSorting) =>
new Sorting(
[...oldSorting].map(([oldColumnId, oldDirection]) =>
oldColumnId === columnId
? [newColumnId, newSortDirection]
: [oldColumnId, oldDirection],
),
),
);
}
</script>

Expand Down

0 comments on commit 7e99f09

Please sign in to comment.