Skip to content

Commit

Permalink
fix: apply reversed sorting in a column (#172)
Browse files Browse the repository at this point in the history
* fix: apply reversed sorting in a column
* fix: wrong type applied
* fix: sort order variable used
* fix: tests fixed
* fix: review comments
* chore: use sort order type in table header
---------
Co-authored-by: Manos Smyrnakis <[email protected]>
Co-authored-by: Thrasos Kafasis <[email protected]>
  • Loading branch information
smyrn96 authored Jul 3, 2023
1 parent 181f708 commit 893681e
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 18 deletions.
64 changes: 52 additions & 12 deletions src/components/Table/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,50 @@ export default {
},
args: {
columns: [
{ accessor: "id", cell: "Code", classNames: ["id"] },
{ accessor: "description", cell: "Description", classNames: ["description"] },
{ accessor: "name", cell: "Name", classNames: ["name"] },
{ accessor: "category", cell: "Category", classNames: ["category"] },
{ accessor: "id", cell: "Code", classNames: ["id"], sortOrder: "asc" },
{
accessor: "description",
cell: "Description",
classNames: ["description"],
sortOrder: "asc",
},
{ accessor: "name", cell: "Name", classNames: ["name"], sortOrder: "asc" },
{ accessor: "category", cell: "Category", classNames: ["category"], sortOrder: "asc" },
{ accessor: "date", cell: "Date", classNames: ["date"], sortOrder: "desc" },
],
rows: [
{ id: 271, description: "Test", name: "Test", category: "Test", code: "Test" },
{ id: 272, description: "Test", name: "Test", category: "Test", code: "Test" },
{ id: 273, description: "Test", name: "Test", category: "Test", code: "Test" },
{ id: 274, description: "Test", name: "Test", category: "Test", code: "Test" },
{
id: 271,
description: "Test",
name: "Test",
category: "Test",
code: "Test",
date: "27/05/1997",
},
{
id: 272,
description: "Test",
name: "Test",
category: "Test",
code: "Test",
date: "27/05/1997",
},
{
id: 273,
description: "Test",
name: "Test",
category: "Test",
code: "Test",
date: "27/05/1997",
},
{
id: 274,
description: "Test",
name: "Test",
category: "Test",
code: "Test",
date: "27/05/1997",
},
],
emptyState: {
title: "No results found with these criteria",
Expand Down Expand Up @@ -69,10 +103,16 @@ export const WithOverflowColumns = Template.bind({});

WithOverflowColumns.args = {
columns: [
{ accessor: "id", cell: "Code", classNames: ["id"] },
{ accessor: "description", cell: "Description", classNames: ["description"], maxWidth: 100 },
{ accessor: "name", cell: "Name", classNames: ["name"], maxWidth: 100 },
{ accessor: "category", cell: "Category", classNames: ["category"] },
{ accessor: "id", cell: "Code", classNames: ["id"], sortOrder: "asc" },
{
accessor: "description",
cell: "Description",
classNames: ["description"],
maxWidth: 100,
sortOrder: "asc",
},
{ accessor: "name", cell: "Name", classNames: ["name"], maxWidth: 100, sortOrder: "asc" },
{ accessor: "category", cell: "Category", classNames: ["category"], sortOrder: "asc" },
],
rows: [
{
Expand Down
9 changes: 9 additions & 0 deletions src/components/Table/__tests__/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const COLUMNS = [
cell: "Header4",
classNames: ["header4"],
},
{
accessor: "header5",
cell: "Header5",
classNames: ["header5"],
},
];

const ROWS = [
Expand All @@ -33,27 +38,31 @@ const ROWS = [
name: "name1",
category: "category1",
code: "code1",
date: "date1",
},
{
id: "id2",
description: "description2",
name: "name2",
category: "category2",
code: "code2",
date: "date2",
},
{
id: "id3",
description: "description3",
name: "name3",
category: "category3",
code: "code3",
date: "date3",
},
{
id: "id4",
description: "description4",
name: "name4",
category: "category4",
code: "code4",
date: "date4",
},
];

Expand Down
57 changes: 57 additions & 0 deletions src/components/Table/__tests__/__snapshots__/Table.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ exports[`<Table> matches snapshot 1`] = `
</span>
</div>
</th>
<th
class="header-cell header5"
>
<div
aria-expanded="false"
>
<span>
<span>
Header5
</span>
</span>
</div>
</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -111,6 +124,17 @@ exports[`<Table> matches snapshot 1`] = `
/>
</div>
</td>
<td
style="max-width: auto;"
>
<div
aria-expanded="false"
>
<span
style="max-width: auto;"
/>
</div>
</td>
</tr>
<tr
class=""
Expand Down Expand Up @@ -159,6 +183,17 @@ exports[`<Table> matches snapshot 1`] = `
/>
</div>
</td>
<td
style="max-width: auto;"
>
<div
aria-expanded="false"
>
<span
style="max-width: auto;"
/>
</div>
</td>
</tr>
<tr
class=""
Expand Down Expand Up @@ -207,6 +242,17 @@ exports[`<Table> matches snapshot 1`] = `
/>
</div>
</td>
<td
style="max-width: auto;"
>
<div
aria-expanded="false"
>
<span
style="max-width: auto;"
/>
</div>
</td>
</tr>
<tr
class=""
Expand Down Expand Up @@ -255,6 +301,17 @@ exports[`<Table> matches snapshot 1`] = `
/>
</div>
</td>
<td
style="max-width: auto;"
>
<div
aria-expanded="false"
>
<span
style="max-width: auto;"
/>
</div>
</td>
</tr>
</tbody>
</table>
Expand Down
13 changes: 7 additions & 6 deletions src/components/Table/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FC } from "react";
import Checkbox from "../../FormElements/CheckboxGroup/Checkbox";
import { IconChevronDownSVG, IconChevronUpSVG } from "../../../icons/index";
import { Sorting } from "../types";
import { Column, Sorting } from "../types";
import { ChildrenProps } from "../Table";
import { Actions } from "../constants";
import Cell from "./Cell";
Expand All @@ -20,14 +20,15 @@ const Header: FC<ChildrenProps> = ({ selectable = false, state, dispatch, onSort
: dispatch({ type: Actions.removeAll, payload: null });
};

const handleSortingChange = (accesor: string): void => {
const handleSortingChange = (accesor: string, sortOrder: Column["sortOrder"]): void => {
if (sorting) {
const isDescendingOrder = sortOrder === "desc";
// new sorting object
const newSorting: Sorting = {
column: accesor,
// when select new column to sort apply ascending order
// when select new column to sort apply default sortOrder
// when select the same column change the current order
isDescending: sorting.column !== accesor ? false : !sorting.isDescending,
isDescending: sorting.column !== accesor ? isDescendingOrder : !sorting.isDescending,
};

dispatch({ type: Actions.sortingChanged, payload: newSorting });
Expand All @@ -51,15 +52,15 @@ const Header: FC<ChildrenProps> = ({ selectable = false, state, dispatch, onSort
</Cell>
)}
{columns.map(
({ accessor, cell, hidden, sortableHeader = true, classNames = [] }) =>
({ accessor, cell, hidden, sortableHeader = true, classNames = [], sortOrder }) =>
!hidden && (
<Cell
as="th"
key={accessor}
className={`header-cell ${classNames.length > 0 && classNames.join(" ")}`}
onClick={(): void => {
if (sortableHeader) {
handleSortingChange(accessor);
handleSortingChange(accessor, sortOrder);
}
}}
>
Expand Down
1 change: 1 addition & 0 deletions src/components/Table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Column = {
classNames?: string[];
sortableHeader?: boolean;
maxWidth?: number;
sortOrder?: "asc" | "desc";
};

export type Row = {
Expand Down

0 comments on commit 893681e

Please sign in to comment.