18169: Post review changes, Cypress tests added
[arvados-workbench2.git] / src / components / chips / chips.tsx
index 8f597de6035c58f05f1b303a45d3227965d3cf86..2a6fafc3991e415ff38e52095f2a518deb540b62 100644 (file)
@@ -2,16 +2,28 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import * as React from 'react';
+import React from 'react';
 import { Chip, Grid, StyleRulesCallback, withStyles } from '@material-ui/core';
-import { DragSource, DragSourceSpec, DragSourceCollector, ConnectDragSource, DropTarget, DropTargetSpec, DropTargetCollector, ConnectDropTarget } from 'react-dnd';
+import {
+    DragSource,
+    DragSourceSpec,
+    DragSourceCollector,
+    ConnectDragSource,
+    DropTarget,
+    DropTargetSpec,
+    DropTargetCollector,
+    ConnectDropTarget
+} from 'react-dnd';
 import { compose } from 'lodash/fp';
 import { WithStyles } from '@material-ui/core/styles';
 interface ChipsProps<Value> {
     values: Value[];
     getLabel?: (value: Value) => string;
     filler?: React.ReactNode;
+    deletable?: boolean;
+    orderable?: boolean;
     onChange: (value: Value[]) => void;
+    clickable?: boolean;
 }
 
 type CssRules = 'root';
@@ -75,22 +87,31 @@ export const Chips = withStyles(styles)(
             DragSource(this.type, this.dragSpec, this.dragCollector),
             DropTarget(this.type, this.dropSpec, this.dropCollector),
         )(
-            ({ connectDragSource, connectDropTarget, isOver, value }: DraggableChipProps<Value> & CollectedProps) =>
-                compose(
+            ({ connectDragSource, connectDropTarget, isOver, value }: DraggableChipProps<Value> & CollectedProps) => {
+                const connect = compose(
                     connectDragSource,
                     connectDropTarget,
-                )(
+                );
+
+                const chip =
                     <span>
                         <Chip
                             color={isOver ? 'primary' : 'default'}
-                            onDelete={this.deleteValue(value)}
+                            onDelete={this.props.deletable
+                                ? this.deleteValue(value)
+                                : undefined}
+                            clickable={this.props.clickable}
                             label={this.props.getLabel ?
                                 this.props.getLabel(value)
                                 : typeof value === 'object'
                                     ? JSON.stringify(value)
                                     : value} />
-                    </span>
-                )
+                    </span>;
+
+                return this.props.orderable
+                    ? connect(chip)
+                    : chip;
+            }
         );
 
         deleteValue = (value: Value) => () => {