Skip to content

Commit a3c3764

Browse files
authored
fix: make sortable table header chevron clickable (#860)
* fix: snapshots * fix: make sortable table header chevron clickable * chore: remove not required changes * chore: resolve conflicts * chore: resolve comments * chore: updates screenshot
1 parent 06cf768 commit a3c3764

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

packages/tablev2/Table.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ function HeaderCell<Entry>({
146146
className={style.sortableButton}
147147
>
148148
{column.header}
149+
{Boolean(column.sorter) && (
150+
<span
151+
data-cy="sortIcon"
152+
className={cx(style.sortable(order), "sortIcon")}
153+
/>
154+
)}
149155
</ResetButton>
150156
) : (
151157
column.header
@@ -155,7 +161,7 @@ function HeaderCell<Entry>({
155161
<div
156162
className={cx(style.headerCell(column.textAlign), {
157163
[style.makeSpaceForSortIndicator]: Boolean(column.sorter),
158-
[style.sortable(order)]: Boolean(column.sorter),
164+
[style.headerHover]: Boolean(column.sorter),
159165
[style.rowScrollShadow]: showScrollShadow
160166
})}
161167
id={id}

packages/tablev2/__snapshots__/tablev2.test.tsx.snap

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ exports[`Table v2 rendering renders default 1`] = `
1212
>
1313
<div
1414
aria-sort="none"
15-
class="css-1kf7gto"
15+
class="css-1il2bhb"
1616
id="name"
1717
role="columnheader"
1818
>
@@ -27,6 +27,10 @@ exports[`Table v2 rendering renders default 1`] = `
2727
<div>
2828
Name
2929
</div>
30+
<span
31+
class="css-123nj8f sortIcon"
32+
data-cy="sortIcon"
33+
/>
3034
</div>
3135
</button>
3236
<div
@@ -966,7 +970,7 @@ exports[`Table v2 rendering renders without a sticky first column 1`] = `
966970
>
967971
<div
968972
aria-sort="none"
969-
class="css-kzvdt0"
973+
class="css-1me0nmx"
970974
id="name"
971975
role="columnheader"
972976
>
@@ -981,6 +985,10 @@ exports[`Table v2 rendering renders without a sticky first column 1`] = `
981985
<div>
982986
Name
983987
</div>
988+
<span
989+
class="css-123nj8f sortIcon"
990+
data-cy="sortIcon"
991+
/>
984992
</div>
985993
</button>
986994
<div

packages/tablev2/style.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,14 @@ export const sortable = (dir: "asc" | "desc" | null) => css`
7979
"currentColor"
8080
)};
8181
}
82+
`;
83+
84+
export const headerHover = css`
8285
&:hover {
83-
&:after {
84-
display: inline-block;
86+
.sortIcon {
87+
:after {
88+
display: inline-block;
89+
}
8590
}
8691
}
8792
`;

packages/tablev2/tablev2.test.tsx

+29
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,35 @@ describe("Table v2", () => {
265265

266266
expect(sorterFn).toHaveBeenCalledWith("desc", -1);
267267
});
268+
it("calls the function passed to the 'sorter' prop when clicking the arrow besides heading", () => {
269+
const sorterFn = jest.fn();
270+
const { getByTestId } = render(
271+
<Table
272+
data={mockData}
273+
toId={el => el.id.toString()}
274+
columns={[
275+
{
276+
id: "name",
277+
header: <div>Name</div>,
278+
textAlign: "left",
279+
render: x => (
280+
<a href={`http://google.com/?q=${encodeURIComponent(x.name)}`}>
281+
{x.name}
282+
</a>
283+
),
284+
initialWidth: "300px",
285+
sorter: sorterFn
286+
}
287+
]}
288+
initialSorter={{ by: "name", order: "asc" }}
289+
/>
290+
);
291+
292+
expect(sorterFn).toHaveBeenCalledWith("asc", 1);
293+
const sortIcon = getByTestId("sortIcon");
294+
sortIcon.click();
295+
expect(sorterFn).toHaveBeenLastCalledWith("desc", -1);
296+
});
268297
it("sorts strings with Sorter.string", () => {
269298
const nameSort = Sorter.string("name");
270299
const ascSort = nameSort("asc", -1);

0 commit comments

Comments
 (0)