1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { Chip, Grid, StyleRulesCallback, withStyles } from '@material-ui/core';
7 import { DragSource, DragSourceSpec, DragSourceCollector, ConnectDragSource, DropTarget, DropTargetSpec, DropTargetCollector, ConnectDropTarget } from 'react-dnd';
8 import { compose, noop } from 'lodash/fp';
9 import { WithStyles } from '@material-ui/core/styles';
10 interface ChipsProps<Value> {
12 getLabel?: (value: Value) => string;
13 filler?: React.ReactNode;
16 onChange: (value: Value[]) => void;
19 type CssRules = 'root';
21 const styles: StyleRulesCallback<CssRules> = ({ spacing }) => ({
23 margin: `0px -${spacing.unit / 2}px`,
26 export const Chips = withStyles(styles)(
27 class Chips<Value> extends React.Component<ChipsProps<Value> & WithStyles<CssRules>> {
29 const { values, filler } = this.props;
30 return <Grid container spacing={8} className={this.props.classes.root}>
31 {values.map(this.renderChip)}
32 {filler && <Grid item xs>{filler}</Grid>}
36 renderChip = (value: Value, index: number) =>
37 <Grid item key={index}>
38 <this.chip {...{ value }} />
43 dragSpec: DragSourceSpec<DraggableChipProps<Value>, { value: Value }> = {
44 beginDrag: ({ value }) => ({ value }),
45 endDrag: ({ value: dragValue }, monitor) => {
46 const result = monitor.getDropResult();
48 const { value: dropValue } = monitor.getDropResult();
49 const dragIndex = this.props.values.indexOf(dragValue);
50 const dropIndex = this.props.values.indexOf(dropValue);
51 const newValues = this.props.values.slice(0);
52 if (dragIndex < dropIndex) {
53 newValues.splice(dragIndex, 1);
54 newValues.splice(dropIndex - 1 || 0, 0, dragValue);
55 } else if (dragIndex > dropIndex) {
56 newValues.splice(dragIndex, 1);
57 newValues.splice(dropIndex, 0, dragValue);
59 this.props.onChange(newValues);
64 dragCollector: DragSourceCollector<{}> = connect => ({
65 connectDragSource: connect.dragSource(),
68 dropSpec: DropTargetSpec<DraggableChipProps<Value>> = {
69 drop: ({ value }) => ({ value }),
72 dropCollector: DropTargetCollector<{}> = (connect, monitor) => ({
73 connectDropTarget: connect.dropTarget(),
74 isOver: monitor.isOver(),
77 DragSource(this.type, this.dragSpec, this.dragCollector),
78 DropTarget(this.type, this.dropSpec, this.dropCollector),
80 ({ connectDragSource, connectDropTarget, isOver, value }: DraggableChipProps<Value> & CollectedProps) => {
81 const connect = compose(
89 color={isOver ? 'primary' : 'default'}
90 onDelete={this.props.deletable
91 ? this.deleteValue(value)
93 label={this.props.getLabel ?
94 this.props.getLabel(value)
95 : typeof value === 'object'
96 ? JSON.stringify(value)
100 return this.props.orderable
106 deleteValue = (value: Value) => () => {
107 const { values } = this.props;
108 const index = values.indexOf(value);
109 const newValues = values.slice(0);
110 newValues.splice(index, 1);
111 this.props.onChange(newValues);
115 interface CollectedProps {
116 connectDragSource: ConnectDragSource;
117 connectDropTarget: ConnectDropTarget;
122 interface DraggableChipProps<Value> {