Skip to content

Commit

Permalink
Refactored sorting to its own component
Browse files Browse the repository at this point in the history
  • Loading branch information
wrigh393 committed Dec 17, 2020
1 parent 561d435 commit 3effeb7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
60 changes: 26 additions & 34 deletions src/components/ItemTable/ItemTable.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import React, { useState } from "react";
import PropTypes from "prop-types";
import { useTable, useFilters, useFlexLayout, useSortBy } from "react-table";
import { Table, TableBody, TableCell, TableHead, TableRow, TableContainer, Grid, ButtonGroup, IconButton, makeStyles, useTheme } from "@material-ui/core";
import { Table, TableBody, TableCell, TableHead, TableRow, TableContainer, Grid, makeStyles, useTheme } from "@material-ui/core";
import { useHistory } from "react-router-dom";
import RelativeTime from "react-relative-time";
import ItemTableFilter from "../ItemTableFilter/"
import { ArrowDownward, ArrowUpward } from "@material-ui/icons";

import ItemTableFilter from "../ItemTableFilter/";
import ItemtTableSortButtons from "../ItemTableSortButtons.js";

export default function ItemTable({ data, rowCanBeSelected }) {
const [selectedRow, setSelectedRow] = useState({ queue: null, number: null});
const [selectedRow, setSelectedRow] = useState({ queue: null, number: null });

const theme = useTheme();
const theme = useTheme();
const useStyles = makeStyles({
// Fully visible for active icons
activeSortIcon: {
Expand Down Expand Up @@ -74,7 +73,12 @@ export default function ItemTable({ data, rowCanBeSelected }) {

return (
<TableContainer>
<Table {...getTableProps} aria-label="Table of Queue Items" size="small">
<Table
{...getTableProps}
aria-label="Table of Queue Items"
size="small"
stickyHeader
>
<TableHead>
{headerGroups.map(headerGroup => (
<TableRow {...headerGroup.getHeaderGroupProps()}>
Expand All @@ -86,32 +90,20 @@ export default function ItemTable({ data, rowCanBeSelected }) {
{column.render("Filter")}
</Grid>
<Grid item sm={2} alignItems='center' justify='center'>
<ButtonGroup orientation="vertical" size="small">
<IconButton
edge="start"
onClick={(_ => {
const isSortedAsc = column.isSorted && !column.isSortedDesc;
isSortedAsc ? column.clearSortBy() : column.toggleSortBy(false)
})}
>
<ArrowUpward {...column.getSortByToggleProps()}
fontSize="inherit"
color={column.isSorted && column.isSortedDesc === false ? "secondary" : "default"}
/>
</IconButton>
<IconButton
edge="start"
onClick={(_ => {
const isSortedDesc = column.isSorted && column.isSortedDesc;
isSortedDesc ? column.clearSortBy() : column.toggleSortBy(true)
})}
>
<ArrowDownward {...column.getSortByToggleProps(column.isSortedDesc)}
fontSize="inherit"
color={column.isSorted && column.isSortedDesc ? "secondary" : "default"}
/>
</IconButton>
</ButtonGroup>
<ItemtTableSortButtons
onClickAsc={_ => {
const isSortedAsc = column.isSorted && !column.isSortedDesc;
isSortedAsc ? column.clearSortBy() : column.toggleSortBy(false);
}}
onClickDesc={_ => {
const isSortedDesc = column.isSorted && column.isSortedDesc;
isSortedDesc ? column.clearSortBy() : column.toggleSortBy(true)
}}
colorAsc={column.isSorted && column.isSortedDesc === false ? "secondary" : "default"}
colorDesc={column.isSorted && column.isSortedDesc ? "secondary" : "default"}
columnSortAscProps={column.getSortByToggleProps()}
columnSortDescProps={column.getSortByToggleProps(column.isSortedDesc)}
/>
</Grid>
</Grid>
</TableCell>
Expand All @@ -136,7 +128,7 @@ export default function ItemTable({ data, rowCanBeSelected }) {
classes={{ root: (isSelected && rowCanBeSelected) ? classes.rowSelected : classes.bandedRows }}
{...row.getRowProps()} >
{row.cells.map(cell => (
<TableCell
<TableCell
classes={{ root: classes.columnBorders }}
{...cell.getCellProps()}
>
Expand Down
31 changes: 31 additions & 0 deletions src/components/ItemTableSortButtons.js/ItemTableSortButtons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import PropTypes from "prop-types";
import { ButtonGroup, IconButton } from "@material-ui/core";
import { ArrowDownward, ArrowUpward } from "@material-ui/icons";

export default function ItemTableSortButtons({ onClickAsc, onClickDesc, colorAsc, colorDesc, columnSortAscProps, columnSortDescProps }) {
return (
<ButtonGroup orientation="vertical" size="small">
<IconButton
edge="start"
onClick={onClickAsc}
>
<ArrowUpward {...columnSortAscProps}
fontSize="inherit"
color={colorAsc}
/>
</IconButton>
<IconButton
edge="start"
onClick={onClickDesc}
>
<ArrowDownward {...columnSortDescProps}
fontSize="inherit"
color={colorDesc}
/>
</IconButton>
</ButtonGroup>
)
}


Empty file.
3 changes: 3 additions & 0 deletions src/components/ItemTableSortButtons.js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ItemtTableSortButtons from './ItemTableSortButtons'

export default ItemtTableSortButtons;

0 comments on commit 3effeb7

Please sign in to comment.