refatoring project tree component, create component for list item
authorJanicki Artur <artur.janicki@contractors.roche.com>
Wed, 18 Jul 2018 12:24:09 +0000 (14:24 +0200)
committerJanicki Artur <artur.janicki@contractors.roche.com>
Wed, 18 Jul 2018 12:24:09 +0000 (14:24 +0200)
Feature #13797

Arvados-DCO-1.1-Signed-off-by: Janicki Artur <artur.janicki@contractors.roche.com>

src/components/side-panel/side-panel.tsx
src/components/single-list-item/single-list-item.tsx [new file with mode: 0644]
src/components/tree/tree.tsx
src/views-components/project-tree/project-tree.tsx

index 2d384ef7fa223131bff7e6d5434726746d604dda..8105ba7507fca31d0305d0cd54e7259a0ebe6b81 100644 (file)
@@ -9,6 +9,7 @@ import { ArvadosTheme } from '../../common/custom-theme';
 import { List, ListItem, ListItemText, ListItemIcon, Collapse, Typography } from "@material-ui/core";
 import { SidePanelRightArrowIcon, IconType } from '../icon/icon';
 import * as classnames from "classnames";
+import SingleListItem from '../single-list-item/single-list-item';
 
 export interface SidePanelItem {
     id: string;
@@ -30,9 +31,9 @@ interface SidePanelProps {
 class SidePanel extends React.Component<SidePanelProps & WithStyles<CssRules>> {
     render(): ReactElement<any> {
         const { classes, toggleOpen, toggleActive, sidePanelItems, children } = this.props;
-        const { leftSidePanelContainer, row, list, toggableIconContainer } = classes;
+        const { root, row, list, toggableIconContainer } = classes;
         return (
-            <div className={leftSidePanelContainer}>
+            <div className={root}>
                 <List>
                     {sidePanelItems.map(it => (
                         <span key={it.name}>
@@ -41,14 +42,11 @@ class SidePanel extends React.Component<SidePanelProps & WithStyles<CssRules>> {
                                     {it.openAble ? (
                                         <i onClick={() => toggleOpen(it.id)} className={toggableIconContainer}>
                                             <ListItemIcon className={this.getToggableIconClassNames(it.open, it.active)}>
-                                                {< SidePanelRightArrowIcon />}
+                                                < SidePanelRightArrowIcon />
                                             </ListItemIcon>
                                         </i>
                                     ) : null}
-                                    <ListItemIcon className={this.getListItemIconClassNames(it.margin, it.active)}>
-                                        {<it.icon />}
-                                    </ListItemIcon>
-                                    <ListItemText primary={this.renderListItemText(it.name, it.active)} />
+                                    <SingleListItem icon={it.icon} name={it.name} isActive={it.active} hasMargin={it.margin} />
                                 </span>
                             </ListItem>
                             {it.openAble ? (
@@ -72,38 +70,16 @@ class SidePanel extends React.Component<SidePanelProps & WithStyles<CssRules>> {
         });
     }
 
-    getListItemIconClassNames = (hasMargin?: boolean, isActive?: boolean) => {
-        const { classes } = this.props;
-        return classnames({
-            [classes.hasMargin]: hasMargin,
-            [classes.active]: isActive
-        });
-    }
-
-    renderListItemText = (name: string, isActive?: boolean) => {
-        return <Typography variant='body1' className={this.getListItemTextClassNames(isActive)}>
-                {name}
-            </Typography>;
-    }
-
-    getListItemTextClassNames = (isActive?: boolean) => {
-        const { classes } = this.props;
-        return classnames(classes.listItemText, {
-            [classes.active]: isActive
-        });
-    }
-
     handleRowContextMenu = (item: SidePanelItem) =>
         (event: React.MouseEvent<HTMLElement>) =>
             item.openAble ? this.props.onContextMenu(event, item) : null
 
 }
 
-type CssRules = 'active' | 'listItemText' | 'row' | 'leftSidePanelContainer' | 'list' | 
-    'hasMargin' | 'iconClose' | 'iconOpen' | 'toggableIconContainer' | 'toggableIcon';
+type CssRules = 'active' | 'row' | 'root' | 'list' | 'iconClose' | 'iconOpen' | 'toggableIconContainer' | 'toggableIcon';
 
 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
-    leftSidePanelContainer: {
+    root: {
         overflowY: 'auto',
         minWidth: '240px',
         whiteSpace: 'nowrap',
@@ -127,15 +103,9 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     toggableIcon: {
         fontSize: '14px'
     },
-    listItemText: {
-        fontWeight: 700
-    },
     active: {
         color: theme.palette.primary.main,
     },
-    hasMargin: {
-        marginLeft: '18px',
-    },
     iconClose: {
         transition: 'all 0.1s ease',
     },
diff --git a/src/components/single-list-item/single-list-item.tsx b/src/components/single-list-item/single-list-item.tsx
new file mode 100644 (file)
index 0000000..13338c3
--- /dev/null
@@ -0,0 +1,74 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import * as React from 'react';
+import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
+import { ArvadosTheme } from '../../common/custom-theme';
+import { ListItemIcon, ListItemText, Typography } from '@material-ui/core';
+import { IconType } from '../icon/icon';
+import * as classnames from "classnames";
+
+export interface SingleListItemDataProps {
+    icon: IconType;
+    name: string;
+    isActive?: boolean;
+    hasMargin?: boolean;
+}
+
+type SingleListItemProps = SingleListItemDataProps & WithStyles<CssRules>;
+
+class SingleListItem extends React.Component<SingleListItemProps, {}> {
+    render() {
+        const { classes, isActive, hasMargin, name, icon: Icon } = this.props;
+        return (
+            <Typography component='span' className={classes.root}>
+                <ListItemIcon className={this.getListItemIconClassNames(hasMargin, isActive)}>
+                    <Icon />
+                </ListItemIcon>
+                <ListItemText primary={
+                    <Typography variant='body1' className={this.getListItemTextClassNames(isActive)}>
+                        {name}
+                    </Typography>
+                } />
+            </Typography>
+        );
+    }
+
+    getListItemIconClassNames = (hasMargin?: boolean, isActive?: boolean) => {
+        const { classes } = this.props;
+        return classnames({
+            [classes.hasMargin]: hasMargin,
+            [classes.active]: isActive
+        });
+    }
+
+    getListItemTextClassNames = (isActive?: boolean) => {
+        const { classes } = this.props;
+        return classnames(classes.listItemText, {
+            [classes.active]: isActive
+        });
+    }
+
+
+}
+        
+type CssRules = 'root' | 'listItemText' | 'hasMargin' | 'active';
+        
+const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
+    root: {
+        display: 'flex',
+        alignItems: 'center'
+    },
+    listItemText: {
+        fontWeight: 700
+    },
+    active: {
+        color: theme.palette.primary.main,
+    },
+    hasMargin: {
+        marginLeft: '18px',
+    },
+});
+
+export default withStyles(styles)(SingleListItem);
\ No newline at end of file
index 8de9bda5970ffe7d495f94f24b51229aacf9c1df..1ceb3b97cf692e0d5d7e8917621c3635865d2d75 100644 (file)
@@ -5,10 +5,11 @@
 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 { StyleRulesCallback, withStyles, WithStyles } from '@material-ui/core/styles';
 import { ReactElement } from "react";
 import Collapse from "@material-ui/core/Collapse/Collapse";
 import CircularProgress from '@material-ui/core/CircularProgress';
+import { ArvadosTheme } from '../../common/custom-theme';
 
 export enum TreeItemStatus {
     Initial,
@@ -77,13 +78,13 @@ class Tree<T> extends React.Component<TreeProps<T> & WithStyles<CssRules>, {}> {
 
 type CssRules = 'list' | 'activeArrow' | 'inactiveArrow' | 'arrowRotate' | 'arrowTransition' | 'loader' | 'arrowVisibility';
 
-const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
+const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     list: {
         paddingBottom: '3px',
         paddingTop: '3px',
     },
     activeArrow: {
-        color: '#4285F6',
+        color: theme.palette.primary.main,
         position: 'absolute',
     },
     inactiveArrow: {
index 17592a7f36d9374e8c6bef603e132d9690567944..68c4c85772e72c452433250fc8ba22ffd93e2e86 100644 (file)
@@ -5,12 +5,11 @@
 import * as React from 'react';
 import { ReactElement } from 'react';
 import { StyleRulesCallback, Theme, WithStyles, withStyles } from '@material-ui/core/styles';
-import ListItemText from "@material-ui/core/ListItemText/ListItemText";
-import ListItemIcon from '@material-ui/core/ListItemIcon';
-import Typography from '@material-ui/core/Typography';
-
 import Tree, { TreeItem, TreeItemStatus } from '../../components/tree/tree';
 import { ProjectResource } from '../../models/project';
+import { ProjectIcon } from '../../components/icon/icon';
+import { ArvadosTheme } from '../../common/custom-theme';
+import SingleListItem from '../../components/single-list-item/single-list-item';
 
 export interface ProjectTreeProps {
     projects: Array<TreeItem<ProjectResource>>;
@@ -22,46 +21,27 @@ export interface ProjectTreeProps {
 class ProjectTree<T> extends React.Component<ProjectTreeProps & WithStyles<CssRules>> {
     render(): ReactElement<any> {
         const { classes, projects, toggleOpen, toggleActive, onContextMenu } = this.props;
-        const { active, listItemText, row, treeContainer } = classes;
+        const { root } = classes;
         return (
-            <div className={treeContainer}>
+            <div className={root}>
                 <Tree items={projects}
                     onContextMenu={onContextMenu}
                     toggleItemOpen={toggleOpen}
                     toggleItemActive={toggleActive}
-                    render={(project: TreeItem<ProjectResource>) =>
-                        <span className={row}>
-                            <ListItemIcon className={project.active ? active : ''}>
-                                <i className="fas fa-folder" />
-                            </ListItemIcon>
-                            <ListItemText className={listItemText} primary={
-                                <Typography className={project.active ? active : ''}>{project.data.name}</Typography>
-                            } />
-                        </span>
-                    } />
+                    render={
+                        (project: TreeItem<ProjectResource>) =>
+                            <SingleListItem icon={ProjectIcon} name={project.data.name} isActive={project.active} hasMargin={true} />
+                    }/>
             </div>
         );
     }
 }
 
-type CssRules = 'active' | 'listItemText' | 'row' | 'treeContainer';
+type CssRules = 'root';
 
-const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
-    active: {
-        color: '#4285F6',
-    },
-    listItemText: {
-        padding: '0px',
-    },
-    row: {
-        display: 'flex',
-        alignItems: 'center',
-        marginLeft: '20px',
-    },
-    treeContainer: {
-        minWidth: '240px',
-        whiteSpace: 'nowrap',
-        marginLeft: '13px',
+const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
+    root: {
+        marginLeft: `${theme.spacing.unit * 1.5}px`,
     }
 });