Skip to content

Commit

Permalink
#148 - Simplify sorting logic for WithDefaultAscendingSortByName Story
Browse files Browse the repository at this point in the history
  • Loading branch information
imikulec authored and jtomic-croz committed Jan 3, 2024
1 parent 0cbb884 commit 1df5d56
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions storybook/src/data-display/DataTable.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,14 @@ Here is an example utilizing the `useMemo` hook. This specific logic is applied

const sortedData = React.useMemo(() => {
return [...smallData].sort((a, b) => {
const result = a.name.localeCompare(b.name, undefined, { sensitivity: 'base' });
return dataTableState.sortBy[0]?.sortDirection === "DESCENDING" ? -result : result;
const sortInfo = dataTableState.sortBy[0] ?? { column: "", sortDirection: "ASCENDING" };

if (a[sortInfo.column] !== undefined && b[sortInfo.column] !== undefined) {
const result = a[sortInfo.column].localeCompare(b[sortInfo.column]);
return sortInfo.sortDirection === "DESCENDING" ? -result : result;
}

return 0;
});
}, [dataTableState.sortBy]);
```
Expand Down
10 changes: 8 additions & 2 deletions storybook/src/data-display/DataTable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,14 @@ export const WithDefaultAscendingSortByName = () => {

const sortedData = React.useMemo(() => {
return [...smallData].sort((a, b) => {
const result = a.name.localeCompare(b.name, undefined, { sensitivity: 'base' });
return dataTableState.sortBy[0]?.sortDirection === "DESCENDING" ? -result : result;
const sortInfo = dataTableState.sortBy[0] ?? { column: "", sortDirection: "ASCENDING" };

if (a[sortInfo.column] !== undefined && b[sortInfo.column] !== undefined) {
const result = a[sortInfo.column].localeCompare(b[sortInfo.column]);
return sortInfo.sortDirection === "DESCENDING" ? -result : result;
}

return 0;
});
}, [dataTableState.sortBy]);

Expand Down

0 comments on commit 1df5d56

Please sign in to comment.