1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { Chip, Grid, StyleRulesCallback, withStyles } from '@material-ui/core';
17 import { compose } from 'lodash/fp';
18 import { WithStyles } from '@material-ui/core/styles';
19 interface ChipsProps<Value> {
21 getLabel?: (value: Value) => string;
22 filler?: React.ReactNode;
25 onChange: (value: Value[]) => void;
29 type CssRules = 'root';
31 const styles: StyleRulesCallback<CssRules> = ({ spacing }) => ({
33 margin: `0px -${spacing.unit / 2}px`,
36 export const Chips = withStyles(styles)(
37 class Chips<Value> extends React.Component<ChipsProps<Value> & WithStyles<CssRules>> {
39 const { values, filler } = this.props;
40 return <Grid container spacing={8} className={this.props.classes.root}>
41 {values.map(this.renderChip)}
42 {filler && <Grid item xs>{filler}</Grid>}
46 renderChip = (value: Value, index: number) =>
47 <Grid item key={index}>
48 <this.chip {...{ value }} />
53 dragSpec: DragSourceSpec<DraggableChipProps<Value>, { value: Value }> = {
54 beginDrag: ({ value }) => ({ value }),
55 endDrag: ({ value: dragValue }, monitor) => {
56 const result = monitor.getDropResult();
58 const { value: dropValue } = monitor.getDropResult();
59 const dragIndex = this.props.values.indexOf(dragValue);
60 const dropIndex = this.props.values.indexOf(dropValue);
61 const newValues = this.props.values.slice(0);
62 if (dragIndex < dropIndex) {
63 newValues.splice(dragIndex, 1);
64 newValues.splice(dropIndex - 1 || 0, 0, dragValue);
65 } else if (dragIndex > dropIndex) {
66 newValues.splice(dragIndex, 1);
67 newValues.splice(dropIndex, 0, dragValue);
69 this.props.onChange(newValues);
74 dragCollector: DragSourceCollector<{}> = connect => ({
75 connectDragSource: connect.dragSource(),
78 dropSpec: DropTargetSpec<DraggableChipProps<Value>> = {
79 drop: ({ value }) => ({ value }),
82 dropCollector: DropTargetCollector<{}> = (connect, monitor) => ({
83 connectDropTarget: connect.dropTarget(),
84 isOver: monitor.isOver(),
87 DragSource(this.type, this.dragSpec, this.dragCollector),
88 DropTarget(this.type, this.dropSpec, this.dropCollector),
90 ({ connectDragSource, connectDropTarget, isOver, value }: DraggableChipProps<Value> & CollectedProps) => {
91 const connect = compose(
99 color={isOver ? 'primary' : 'default'}
100 onDelete={this.props.deletable
101 ? this.deleteValue(value)
103 clickable={this.props.clickable}
104 label={this.props.getLabel ?
105 this.props.getLabel(value)
106 : typeof value === 'object'
107 ? JSON.stringify(value)
111 return this.props.orderable
117 deleteValue = (value: Value) => () => {
118 const { values } = this.props;
119 const index = values.indexOf(value);
120 const newValues = values.slice(0);
121 newValues.splice(index, 1);
122 this.props.onChange(newValues);
126 interface CollectedProps {
127 connectDragSource: ConnectDragSource;
128 connectDropTarget: ConnectDropTarget;
133 interface DraggableChipProps<Value> {