Refactor breadcrumbs component
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 14 Jun 2018 07:48:55 +0000 (09:48 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 14 Jun 2018 07:48:55 +0000 (09:48 +0200)
Feature #13590

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/components/breadcrumbs/breadcrumbs.tsx

index 33ff33aea1720cc2ae7c5f781b8150fdb4773188..25f30a1bdae5da9abc02fb6a9f7bcb511ce7a942 100644 (file)
@@ -13,55 +13,43 @@ export interface Breadcrumb {
 
 interface BreadcrumbsProps {
     items: Breadcrumb[];
-    onClick: (breadcrumb: Breadcrumb) => any;
+    onClick: (breadcrumb: Breadcrumb) => void;
 }
 
-const Breadcrumbs: React.SFC<BreadcrumbsProps & WithStyles<CssRules>> = (props) => {
-    const { classes, onClick, items } = props;
+const Breadcrumbs: React.SFC<BreadcrumbsProps & WithStyles<CssRules>> = ({ classes, onClick, items }) => {
     return <Grid container alignItems="center">
         {
-            getInactiveItems(items).map((item, index) => (
-                <React.Fragment key={index}>
-                    <Button
-                        color="inherit"
-                        className={classes.inactiveItem}
-                        onClick={() => onClick(item)}
-                    >
-                        {item.label}
-                    </Button>
-                    <ChevronRightIcon color="inherit" className={classes.inactiveItem} />
-                </React.Fragment>
-            ))
-        }
-        {
-            getActiveItem(items).map((item, index) => (
-                <Button
-                    color="inherit"
-                    key={index}
-                    onClick={() => onClick(item)}
-                >
-                    {item.label}
-                </Button>
-            ))
+            items.map((item, index) => {
+                const isLastItem = index === items.length - 1;
+                return (
+                    <React.Fragment key={index}>
+                        <Button
+                            color="inherit"
+                            className={isLastItem ? classes.currentItem : classes.item}
+                            onClick={() => onClick(item)}
+                        >
+                            {item.label}
+                        </Button>
+                        {
+                            !isLastItem && <ChevronRightIcon color="inherit" className={classes.item} />
+                        }
+                    </React.Fragment>
+                );
+            })
         }
     </Grid>;
 };
 
-const getInactiveItems = (items: Breadcrumb[]) => {
-    return items.slice(0, -1);
-};
-
-const getActiveItem = (items: Breadcrumb[]) => {
-    return items.slice(-1);
-};
-
-type CssRules = 'inactiveItem';
+type CssRules = "item" | "currentItem";
 
 const styles: StyleRulesCallback<CssRules> = theme => {
     const { unit } = theme.spacing;
     return {
-        inactiveItem: {
+        item: {
             opacity: 0.6
+        },
+        currentItem: {
+            opacity: 1
         }
     };
 };