[ARVADOS-WORKBENCH2] updated: 1.1.4-111-g7444429

Git user git at public.curoverse.com
Wed Jun 20 03:56:28 EDT 2018


Summary of changes:
 src/components/data-explorer/data-explorer.tsx | 13 +++++++++-
 src/components/data-table/data-column.ts       | 15 ++++++++++-
 src/components/data-table/data-table.test.tsx  | 36 ++++++++++++++++++--------
 src/components/data-table/data-table.tsx       | 15 ++++++++---
 4 files changed, 62 insertions(+), 17 deletions(-)

       via  7444429308f438fb2e29188db8f208223ed39128 (commit)
      from  a01f29eb2238fbab8c87254ab23bbc82107832be (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit 7444429308f438fb2e29188db8f208223ed39128
Author: Michal Klobukowski <michal.klobukowski at contractors.roche.com>
Date:   Wed Jun 20 09:56:12 2018 +0200

    Extend DataColumn and DataTable to handle sorting
    
    Feature #13633
    
    Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski at contractors.roche.com>

diff --git a/src/components/data-explorer/data-explorer.tsx b/src/components/data-explorer/data-explorer.tsx
index 874c325..ded1b5e 100644
--- a/src/components/data-explorer/data-explorer.tsx
+++ b/src/components/data-explorer/data-explorer.tsx
@@ -3,10 +3,11 @@
 // 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';
@@ -41,6 +42,8 @@ class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState>
         columns: [{
             name: "Name",
             selected: true,
+            sortDirection: "asc",
+            onSortToggle: () => this.toggleSort("Name"),
             render: item => this.renderName(item)
         }, {
             name: "Status",
@@ -61,6 +64,7 @@ class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState>
         }, {
             name: "Last modified",
             selected: true,
+            onSortToggle: () => this.toggleSort("Last modified"),
             render: item => renderDate(item.lastModified)
         }, {
             name: "Actions",
@@ -184,6 +188,13 @@ class DataExplorer extends React.Component<DataExplorerProps, DataExplorerState>
         };
     }
 
+    toggleSort = (columnName: string) => {
+        this.setState({
+            columns: this.state.columns.map((column, index) =>
+                column.name === columnName ? toggleSortDirection(column) : resetSortDirection(column))
+        });
+    }
+
 }
 
 const renderIcon = (dataItem: DataItem) => {
diff --git a/src/components/data-table/data-column.ts b/src/components/data-table/data-column.ts
index d3b1473..f3d9576 100644
--- a/src/components/data-table/data-column.ts
+++ b/src/components/data-table/data-column.ts
@@ -7,10 +7,23 @@ export interface DataColumn<T> {
     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 };
+};
diff --git a/src/components/data-table/data-table.test.tsx b/src/components/data-table/data-table.test.tsx
index 4a34a6b..ad00280 100644
--- a/src/components/data-table/data-table.test.tsx
+++ b/src/components/data-table/data-table.test.tsx
@@ -7,7 +7,7 @@ import { mount, configure } from "enzyme";
 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() });
 
@@ -30,10 +30,10 @@ describe("<DataTable />", () => {
                 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>> = [
             {
@@ -42,10 +42,10 @@ describe("<DataTable />", () => {
                 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>> = [
             {
@@ -55,10 +55,10 @@ describe("<DataTable />", () => {
                 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>> = [
             {
@@ -68,11 +68,11 @@ describe("<DataTable />", () => {
                 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>> = [
             {
@@ -81,7 +81,7 @@ describe("<DataTable />", () => {
                 selected: true
             }
         ];
-        const dataTable = mount(<DataTable columns={columns} items={[]}/>);
+        const dataTable = mount(<DataTable columns={columns} items={[]} />);
         expect(dataTable.find(Typography).text()).toBe("No items");
     });
 
@@ -98,10 +98,24 @@ describe("<DataTable />", () => {
                 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
diff --git a/src/components/data-table/data-table.tsx b/src/components/data-table/data-table.tsx
index ec260e9..565630b 100644
--- a/src/components/data-table/data-table.tsx
+++ b/src/components/data-table/data-table.tsx
@@ -3,7 +3,7 @@
 // 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> {
@@ -23,9 +23,16 @@ class DataTable<T> extends React.Component<DataTableProps<T> & WithStyles<CssRul
                         <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>
@@ -48,7 +55,7 @@ class DataTable<T> extends React.Component<DataTableProps<T> & WithStyles<CssRul
                                 </TableRow>
                             )}
                     </TableBody>
-                </Table> : <Typography 
+                </Table> : <Typography
                     className={classes.noItemsInfo}
                     variant="body2"
                     gutterBottom>

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list