# All files / components/DragGroup DragGroup.js

9.09% Statements 2/22
0% Branches 0/9
0% Functions 0/12
9.52% Lines 2/21

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211

    4x

    4x

     import React from 'react';
    import PropTypes from 'prop-types';
    import { DropTarget } from 'react-dnd';
    import GroupContainer from './styles/DragGroupContainer';
    import GroupItemsContainer from './styles/DragGroupItemsContainer';
    import DragGroupDropArea from './DragGroupDropArea';
    import DragGroupSeparator from './styles/DragGroupSeparator';
    import DragGroupTitle from './styles/DragGroupTitle';
    import ExpandToggle from '../../behaviors/ExpandToggle';
    import DragItem from './DragGroupItem';

    /**
     * A group of items which can be dragged into other DragGroups depending
     * on itemType.
     */
    class DragGroup extends React.Component {
      static Item = DragItem;

      static propTypes = {
        /**
         * Text to render inside the clickable separators which allow
         * new groups to be created.
         */
        separatorContent: PropTypes.node,

        /**
         * Override the outer container visual component
         * Defaults DragGroupContainer
         */
        Container: PropTypes.func,
        /**
         * Override the visual component which wraps the child items
         * Defaults DragGroupItemsContainer
         */
        ItemsContainer: PropTypes.func,
        /**
         * Override the behavioral component which renders the drop area
         * Defaults DragGroupDropArea
         */
        DropArea: PropTypes.func,
        /**
         * Overrides the behavioral component which renders a clickable separator
         * between items.
         * Defaults DragGroupSeparator
         */
        Separator: PropTypes.func,
        /**
         * Overrides the visual component which renders the toggle-enabled title
         * of a group.
         * Defaults DragGroupTitle
         */
        Title: PropTypes.func,

        // The following are provided by DragGroups;
        // they cannot be required, but can be expected.

        /**
         * Provides a name for the group, rendered in the title
         */
        name: PropTypes.string,
        /**
         * The type of items which can be dropped in this group.
         */
        itemType: PropTypes.string,
        /**
         * A unique identifier for this group.
         */
        groupId: PropTypes.any.isRequired,
        /**
         * Provided by parent DragGroups
         * The number of groups in the list
         */
        groupCount: PropTypes.number,
        /**
         * Whether to allow items to be dragged out of this group or not
         */
        allowDrag: PropTypes.bool,
        /**
         * Whether to allow this group to collapse.
         */
        collapsible: PropTypes.bool,
        /**
         * A callback to call when a user inserts a group. Called with (groupId, itemSplitIndex).
         */
        onSplit: PropTypes.func,
        /**
         * Provided by parent DragGroups
         * A callback to call when an item has been dropped on this group
         */
        onItemDropped: PropTypes.func,
        /**
         * A callback to call when this group is removed
         */
        onRemoved: PropTypes.func,
      };

      static defaultProps = {
        separatorContent: 'Click here to add a group',

        name: 'Group',

        allowDrag: true,
        collapsible: true,

        Container: GroupContainer,
        ItemsContainer: GroupItemsContainer,
        DropArea: DragGroupDropArea,
        Separator: DragGroupSeparator,
        Title: DragGroupTitle,

        itemType: null,
        groupId: null,
        groupCount: null,
        onSplit: () => null,
        onItemDropped: () => null,
        onRemoved: () => null,
      };

      calcOverrideExpanded = () => {
        const { allowDrag, canDrop, collapsible } = this.props;
        if (!collapsible || !allowDrag || canDrop) {
          return true;
        }

        return null;
      };

      shimChild = (child, index) => {
        const { itemType, allowDrag, groupId } = this.props;

        return React.cloneElement(child, {
          itemType,
          canDrag: allowDrag,
          groupId,
        });
      };

      renderInterspersedSeparators() {
        const { children, Separator, onSplit, separatorContent } = this.props;
        const childArray = React.Children.toArray(children);
        return childArray
          .reduce(
            (withSeparators, child, idx) => [
              ...withSeparators,
              <Separator
                onClick={() => onSplit(idx)}
                key={`separator${child.key || idx}`}
              >
                {separatorContent}
              </Separator>,
              this.shimChild(child, idx),
            ],
            [],
          )
          .slice(1); // removing first separator
      }

      renderTitle = expanded =>
        this.props.allowDrag ? (
          <DragGroupTitle expanded={expanded} onDelete={this.props.onRemoved}>
            {this.props.name}
          </DragGroupTitle>
        ) : null;

      render() {
        const {
          Container,
          ItemsContainer,
          ExpandContentContainer,
          DropArea,
          itemType,
          groupId,
          canDrop,
        } = this.props;

        return (
          <Container>
            <ExpandToggle
              startExpanded
              toggleContent={this.renderTitle}
              isExpanded={this.calcOverrideExpanded()}
              springConfig={{ stiffness: 200, damping: 20 }}
            >
              <DropArea itemType={itemType} groupId={groupId}>
                <ItemsContainer>
                  {this.renderInterspersedSeparators()}
                </ItemsContainer>
              </DropArea>
            </ExpandToggle>
          </Container>
        );
      }
    }

    const dropSource = {
      drop(props) {
        return {
          type: 'items',
          groupId: null,
        };
      },
    };

    const collect = (connect, monitor) => ({
      canDrop: monitor.canDrop(),
    });

    export default DropTarget(props => props.itemType, dropSource, collect)(
      DragGroup,
    );