// SPDX-License-Identifier: AGPL-3.0
import * as React from 'react';
-import { DataTable, DataColumn, ColumnSelector } from "../../components/data-table";
+import { DataTable, DataColumn, ColumnSelector, toggleSortDirection, SortDirection, resetSortDirection } from "../../components/data-table";
import { Typography, Grid, Paper, Toolbar } from '@material-ui/core';
import IconButton from '@material-ui/core/IconButton';
import MoreVertIcon from "@material-ui/icons/MoreVert";
+import TableSortLabel from '@material-ui/core/TableSortLabel';
import { formatFileSize, formatDate } from '../../common/formatters';
import { DataItem } from './data-item';
import { mockAnchorFromMouseEvent } from '../popover/helpers';
columns: [{
name: "Name",
selected: true,
+ sortDirection: "asc",
+ onSortToggle: () => this.toggleSort("Name"),
render: item => this.renderName(item)
}, {
name: "Status",
}, {
name: "Last modified",
selected: true,
+ onSortToggle: () => this.toggleSort("Last modified"),
render: item => renderDate(item.lastModified)
}, {
name: "Actions",
};
}
+ toggleSort = (columnName: string) => {
+ this.setState({
+ columns: this.state.columns.map((column, index) =>
+ column.name === columnName ? toggleSortDirection(column) : resetSortDirection(column))
+ });
+ }
+
}
const renderIcon = (dataItem: DataItem) => {
selected: boolean;
configurable?: boolean;
key?: React.Key;
+ sortDirection?: SortDirection;
+ onSortToggle?: () => void;
render: (item: T) => React.ReactElement<void>;
renderHeader?: () => React.ReactElement<void> | null;
}
+export type SortDirection = "asc" | "desc";
+
export const isColumnConfigurable = <T>(column: DataColumn<T>) => {
return column.configurable === undefined || column.configurable;
-};
\ No newline at end of file
+};
+
+export const toggleSortDirection = <T>(column: DataColumn<T>): DataColumn<T> => {
+ const sortDirection = column.sortDirection === undefined || column.sortDirection === "desc" ? "asc" : "desc";
+ return { ...column, sortDirection };
+};
+
+export const resetSortDirection = <T>(column: DataColumn<T>): DataColumn<T> => {
+ return { ...column, sortDirection: undefined };
+};
import * as Adapter from "enzyme-adapter-react-16";
import DataTable from "./data-table";
import { DataColumn } from "./data-column";
-import { TableHead, TableCell, Typography, TableBody, Button } from "@material-ui/core";
+import { TableHead, TableCell, Typography, TableBody, Button, TableSortLabel } from "@material-ui/core";
configure({ adapter: new Adapter() });
selected: false
}
];
- const dataTable = mount(<DataTable columns={columns} items={["item 1"]}/>);
+ const dataTable = mount(<DataTable columns={columns} items={["item 1"]} />);
expect(dataTable.find(TableHead).find(TableCell)).toHaveLength(2);
});
-
+
it("renders column name", () => {
const columns: Array<DataColumn<string>> = [
{
selected: true
}
];
- const dataTable = mount(<DataTable columns={columns} items={["item 1"]}/>);
+ const dataTable = mount(<DataTable columns={columns} items={["item 1"]} />);
expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column 1");
});
-
+
it("uses renderHeader instead of name prop", () => {
const columns: Array<DataColumn<string>> = [
{
selected: true
}
];
- const dataTable = mount(<DataTable columns={columns} items={["item 1"]}/>);
+ const dataTable = mount(<DataTable columns={columns} items={["item 1"]} />);
expect(dataTable.find(TableHead).find(TableCell).text()).toBe("Column Header");
});
-
+
it("passes column key prop to corresponding cells", () => {
const columns: Array<DataColumn<string>> = [
{
selected: true
}
];
- const dataTable = mount(<DataTable columns={columns} items={["item 1"]}/>);
+ const dataTable = mount(<DataTable columns={columns} items={["item 1"]} />);
expect(dataTable.find(TableHead).find(TableCell).key()).toBe("column-1-key");
expect(dataTable.find(TableBody).find(TableCell).key()).toBe("column-1-key");
});
-
+
it("shows information that items array is empty", () => {
const columns: Array<DataColumn<string>> = [
{
selected: true
}
];
- const dataTable = mount(<DataTable columns={columns} items={[]}/>);
+ const dataTable = mount(<DataTable columns={columns} items={[]} />);
expect(dataTable.find(Typography).text()).toBe("No items");
});
selected: true
}
];
- const dataTable = mount(<DataTable columns={columns} items={["item 1"]}/>);
+ const dataTable = mount(<DataTable columns={columns} items={["item 1"]} />);
expect(dataTable.find(TableBody).find(Typography).text()).toBe("item 1");
expect(dataTable.find(TableBody).find(Button).text()).toBe("item 1");
});
+ it("passes sorting props to <TableSortLabel />", () => {
+ const columns: Array<DataColumn<string>> = [{
+ name: "Column 1",
+ sortDirection: "asc",
+ onSortToggle: jest.fn(),
+ selected: true,
+ render: (item) => <Typography>{item}</Typography>
+ }];
+ const dataTable = mount(<DataTable columns={columns} items={["item 1"]} />);
+ expect(dataTable.find(TableSortLabel).prop("active")).toBeTruthy();
+ dataTable.find(TableSortLabel).at(0).simulate("click");
+ expect(columns[0].onSortToggle).toHaveBeenCalled();
+ });
+
});
\ No newline at end of file
// SPDX-License-Identifier: AGPL-3.0
import * as React from 'react';
-import { Table, TableBody, TableRow, TableCell, TableHead, StyleRulesCallback, Theme, WithStyles, withStyles, Typography } from '@material-ui/core';
+import { Table, TableBody, TableRow, TableCell, TableHead, TableSortLabel, StyleRulesCallback, Theme, WithStyles, withStyles, Typography } from '@material-ui/core';
import { DataColumn } from './data-column';
export interface DataTableProps<T> {
<TableRow>
{columns
.filter(column => column.selected)
- .map(({ name, renderHeader, key }, index) =>
+ .map(({ name, renderHeader, key, sortDirection, onSortToggle }, index) =>
<TableCell key={key || index}>
- {renderHeader ? renderHeader() : name}
+ {renderHeader ?
+ renderHeader() :
+ <TableSortLabel
+ active={!!sortDirection}
+ direction={sortDirection}
+ onClick={() => onSortToggle && onSortToggle()}>
+ {name}
+ </TableSortLabel>}
</TableCell>
)}
</TableRow>
</TableRow>
)}
</TableBody>
- </Table> : <Typography
+ </Table> : <Typography
className={classes.noItemsInfo}
variant="body2"
gutterBottom>