X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/1e99be6bc2fba403c15bdbcea5b3b4165225ca3e..3ad7e33a2d50e9ed2298624ea5cac7cb5cd21a6f:/src/components/tree/tree.tsx?ds=sidebyside

diff --git a/src/components/tree/tree.tsx b/src/components/tree/tree.tsx
index 0be5499f..d8397d66 100644
--- a/src/components/tree/tree.tsx
+++ b/src/components/tree/tree.tsx
@@ -5,9 +5,29 @@
 import * as React from 'react';
 import List from "@material-ui/core/List/List";
 import ListItem from "@material-ui/core/ListItem/ListItem";
+import { StyleRulesCallback, Theme, withStyles, WithStyles } from '@material-ui/core/styles';
 import { ReactElement } from "react";
 import Collapse from "@material-ui/core/Collapse/Collapse";
 
+type CssRules = 'list' | 'activeArrow' | 'arrow' | 'arrowRotate';
+
+const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
+    list: {
+        paddingBottom: '3px',
+        paddingTop: '3px',
+    },
+    activeArrow: {
+        color: '#4285F6',
+        position: 'absolute',
+    },
+    arrow: {
+        position: 'absolute',
+    },
+    arrowRotate: {
+        transform: 'rotate(-90deg)',
+    }
+});
+
 export interface TreeItem<T> {
     data: T;
     id: string;
@@ -18,30 +38,32 @@ export interface TreeItem<T> {
 
 interface TreeProps<T> {
     items?: Array<TreeItem<T>>;
-    render: (item: TreeItem<T>) => ReactElement<{}>;
+    render: (item: TreeItem<T>, level?: number) => ReactElement<{}>;
     toggleItem: (id: string) => any;
     level?: number;
 }
 
-function colorArrows(open: boolean, color: string){
-    return <i style={{marginRight: '10px', minWidth: '10px', color}} className={open ? "fas fa-caret-down" : "fas fa-caret-right"} />
-}
-
-class Tree<T> extends React.Component<TreeProps<T>, {}> {
+class Tree<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
+    renderArrow (items: boolean, arrowClass: string, open: boolean){
+        return <i className={`${arrowClass} ${open ? "fas fa-caret-down" : `fas fa-caret-down ${this.props.classes.arrowRotate}`}`} />
+    }
     render(): ReactElement<any> {
         const level = this.props.level ? this.props.level : 0;
-        return <List component="div" style={{paddingBottom: '3px', paddingTop: '3px'}}>
-            {this.props.items && this.props.items.map((it: TreeItem<T>, idx: number) =>
-             <div key={`item/${level}/${idx}`}>      
-                <ListItem button onClick={() => this.props.toggleItem(it.id)} style={{paddingLeft: (level + 1) * 15, paddingBottom: '3px', paddingTop: '3px'}}>  
-                    {it.active ? colorArrows(it.open, "#4285F6") : colorArrows(it.open, "#333")}
-                    {this.props.render(it)}
+        const {classes, render, toggleItem, items} = this.props;
+        const {list, arrow, activeArrow} = classes;
+        return <List component="div" className={list}>
+            {items && items.map((it: TreeItem<T>, idx: number) =>
+             <div key={`item/${level}/${idx}`}>
+                <ListItem button onClick={() => toggleItem(it.id)} className={list} style={{paddingLeft: (level + 1) * 20}}>
+                    {this.renderArrow(true, it.active ? activeArrow : arrow, it.open)}
+                    {render(it, level)}
                 </ListItem>
                 {it.items && it.items.length > 0 &&
                 <Collapse in={it.open} timeout="auto" unmountOnExit>
-                    <Tree items={it.items}
-                        render={this.props.render}
-                        toggleItem={this.props.toggleItem}
+                    <StyledTree
+                        items={it.items}
+                        render={render}
+                        toggleItem={toggleItem}
                         level={level + 1}/>
                 </Collapse>}
              </div>)}
@@ -49,4 +71,5 @@ class Tree<T> extends React.Component<TreeProps<T>, {}> {
     }
 }
 
-export default Tree;
+const StyledTree = withStyles(styles)(Tree);
+export default StyledTree
