Add orderable and deletable flafs to chips
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Sun, 14 Oct 2018 16:54:25 +0000 (18:54 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Sun, 14 Oct 2018 16:54:25 +0000 (18:54 +0200)
Feature #14232

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

src/components/chips-input/chips-input.tsx
src/components/chips/chips.tsx
src/views/run-process-panel/inputs/file-array-input.tsx
src/views/run-process-panel/inputs/string-array-input.tsx

index 12932c54f5775c3d55fe1653171b32fbf7dfb325..fc5fda01be0e7366c9de395b9c38a0b9bafe459d 100644 (file)
@@ -15,6 +15,8 @@ interface ChipsInputProps<Value> {
     createNewValue: (value: string) => Value;
     inputComponent?: React.ComponentType<InputProps>;
     inputProps?: InputProps;
+    deletable?: boolean;
+    orderable?: boolean;
 }
 
 type CssRules = 'chips' | 'input' | 'inputContainer';
index 8f597de6035c58f05f1b303a45d3227965d3cf86..36cf2412c42d1300340ff6dafa4656c30fc97cfe 100644 (file)
@@ -5,12 +5,14 @@
 import * as 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 { compose } from 'lodash/fp';
+import { compose, noop } 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;
 }
 
@@ -75,22 +77,30 @@ 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}
                             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) => () => {
index 39884a6d7ea9726af9b25be6de49f46e307f1027..391cc7fe810d7328b417b80c5988dd424403feae 100644 (file)
@@ -267,6 +267,8 @@ const FileArrayInputComponent = connect(mapStateToProps)(
                     <div className={classes.chips}>
                         <Typography variant='subheading'>Selected files ({this.state.files.length}):</Typography>
                         <Chips
+                            orderable
+                            deletable
                             values={this.state.files}
                             onChange={this.setFiles}
                             getLabel={(file: CollectionFile) => file.name} />
index dafd9466d7e0b420c8c271cecb89292dc4cde11c..da03f2966149ebe4abd7be9d85588e71b62f8ab5 100644 (file)
@@ -43,6 +43,8 @@ const StringArrayInputComponent = (props: GenericInputProps) =>
 class InputComponent extends React.PureComponent<GenericInputProps>{
     render() {
         return <ChipsInput
+            deletable
+            orderable
             value={this.props.input.value}
             onChange={this.handleChange}
             createNewValue={identity}