# All files / components/DragGroups DragGroup.js

9.52% Statements 2/21
0% Branches 0/8
0% Functions 0/14
10% Lines 2/20

    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

    1x

    1x

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

    class DragGroup extends React.Component {
      static propTypes = {
        Container: PropTypes.func,

        ItemsContainer: PropTypes.func,
        DropArea: PropTypes.func,
        Separator: PropTypes.func,
        separatorContent: PropTypes.node,
        Title: PropTypes.func,

        // provided by DragGroups; cannot be required, but can be expected.
        name: PropTypes.string,
        itemType: PropTypes.string,
        groupIndex: PropTypes.number,
        groupCount: PropTypes.number,
        onGroupInserted: PropTypes.func,
        onItemDropped: PropTypes.func,
        onGroupRemoved: PropTypes.func,
      };

      static defaultProps = {
        Container: GroupContainer,
        ItemsContainer: GroupItemsContainer,
        DropArea: DragGroupDropArea,
        Separator: DragGroupSeparator,
        separatorContent: 'Click here to add a group',
        Title: DragGroupTitle,

        name: null,
        itemType: null,
        groupIndex: null,
        groupCount: null,
        onGroupInserted: () => null,
        onItemDropped: () => null,
        onGroupRemoved: () => null,
      };

      createItemDropHandler = index => (dropType, targetGroupIndex) => {
        this.props.onItemDropped({
          sourceGroupIndex: this.props.groupIndex,
          sourceIndex: index,
          targetGroupIndex: targetGroupIndex,
          dropType,
        });
      };

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

        return React.cloneElement(child, {
          itemType,
          onDrop: this.createItemDropHandler(index),
          canDrag: groupCount > 1,
        });
      };

      renderInterspersedSeparators() {
        const {
          children,
          Separator,
          name,
          onGroupInserted,
          separatorContent,
        } = this.props;
        const childArray = React.Children.toArray(children);
        return childArray.reduce(
          (withSeparators, child, idx) => [
            ...withSeparators,
            this.shimChild(child, idx),
            <Separator
              onClick={() => onGroupInserted(idx + 1)}
              key={`separator${child.key || idx + 1}`}
            >
              {separatorContent}
            </Separator>,
          ],
          [
            <Separator onClick={() => onGroupInserted(0)} key="finalSeparator">
              {separatorContent}
            </Separator>,
          ],
        );
      }

      renderTitle = expanded => this.props.groupCount > 1 ? (
        <DragGroupTitle expanded={expanded} onDelete={this.props.onGroupRemoved}>
          {this.props.title}
        </DragGroupTitle>
      ) : null;

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

        return (
          <Container>
            <ExpandToggle
              startExpanded
              toggleContent={this.renderTitle}
              isExpanded={groupCount > 1 ? null : true}
            >
              <div style={{ height: canDrop ? '0' : 'auto' }}>
                <ItemsContainer>
                  {this.renderInterspersedSeparators()}
                </ItemsContainer>
              </div>
            </ExpandToggle>
            <DropArea itemType={itemType} groupIndex={groupIndex} />
          </Container>
        );
      }
    }

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

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

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