Skip to content

Commit

Permalink
feat: basic sorting for the table (learnwithparam#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Param-Harrison authored Nov 24, 2019
1 parent 8403cca commit 3e74534
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
34 changes: 26 additions & 8 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ table td {
padding: 0.5rem;
border-bottom: 1px solid #ededed;
border-right: 1px solid #ededed;
position: relative;
}
table th:last-child,
table td:last-child {
Expand All @@ -20,19 +21,28 @@ table tr:nth-child(even) {
background-color: #fafafa;
}

table th::before {
position: absolute;
right: 15px;
top: 16px;
content: "";
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
}
table th.sort-asc::before {
border-bottom: 5px solid #22543d;
}
table th.sort-desc::before {
border-top: 5px solid #22543d;
}

.App {
display: flex;
flex-direction: column;
padding: 20px;
}
input {
padding: 10px;
margin-bottom: 20px;
font-size: 18px;
border-radius: 5px;
border: 1px solid #ddd;
box-shadow: none;
}
.badge {
background-color: #9ae6b4;
color: #22543d;
Expand All @@ -43,3 +53,11 @@ input {
font-weight: bold;
text-transform: uppercase;
}
input {
padding: 10px;
margin-bottom: 20px;
font-size: 18px;
border-radius: 5px;
border: 1px solid #ddd;
box-shadow: none;
}
18 changes: 15 additions & 3 deletions src/Table.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from "react";
import { useTable, useFilters } from "react-table";
import { useTable, useFilters, useSortBy } from "react-table";

export default function Table({ columns, data }) {
const [filterInput, setFilterInput] = useState("");
Expand All @@ -16,7 +16,8 @@ export default function Table({ columns, data }) {
columns,
data
},
useFilters
useFilters,
useSortBy
);

const handleFilterChange = e => {
Expand All @@ -38,7 +39,18 @@ export default function Table({ columns, data }) {
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()}>{column.render("Header")}</th>
<th
{...column.getHeaderProps(column.getSortByToggleProps())}
className={
column.isSorted
? column.isSortedDesc
? "sort-desc"
: "sort-asc"
: ""
}
>
{column.render("Header")}
</th>
))}
</tr>
))}
Expand Down

0 comments on commit 3e74534

Please sign in to comment.