# All files / components/BulkSelect BulkSelect.js

0% Statements 0/65
0% Branches 0/18
0% Functions 0/11
0% Lines 0/23

    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

     import React from 'react';
    import PropTypes from 'prop-types';
    import { withProps } from 'recompose';
    import theme from '../../theme';
    import styled from 'styled-components';
    import ToggleButton from '../ToggleButton';
    import BulkSelectBorder from './styles/BulkSelectBorder';
    import BulkSelectDivider from './styles/BulkSelectDivider';
    import BulkSelectItemContainer from './styles/BulkSelectItemContainer';

    /**
     * @deprecated BulkSelect has been deprecated in favor of using [Grid](/#!/Grid) with
     * [ToggleButton](/#!/ToggleButton), optionally with [Selectable](/#!/Selectable) and/or [DragBox](/#!/DragBox).
     */
    class BulkSelect extends React.Component {
      static propTypes = {
        sections: PropTypes.arrayOf(
          PropTypes.shape({
            title: PropTypes.string,
            items: PropTypes.arrayOf(
              PropTypes.shape({
                selected: PropTypes.bool.isRequired,
                data: PropTypes.any.isRequired,
              }),
            ).isRequired,
          }),
        ),
        /**
         * DEPRECATED: see renderItemContents
         */
        renderItem: PropTypes.func,
        /**
         * Renders the node inside an item. Passed (item, isSelected).
         */
        renderItemContents: PropTypes.func,
        /**
         * Should return a unique key for an item
         */
        computeItemKey: PropTypes.func,
        onItemSelected: PropTypes.func,
        onItemDeselected: PropTypes.func,
        /**
         * A component to render an item. Receives 'selected' prop.
         */
        Item: PropTypes.func,
        /**
         * A component to render the border box around the BulkSelect
         */
        Border: PropTypes.func,
        /**
         * A component to render the divider between sections
         */
        Divider: PropTypes.func,
        /**
         * A component to render the element that contains items
         */
        ItemContainer: PropTypes.func,
      };

      static defaultProps = {
        sections: [],
        computeItemKey: item =>
          item.get ? item.get('id') : item.id || JSON.stringify(item),
        onItemSelected: () => null,
        onItemDeselected: () => null,
        Item: ToggleButton,
        Border: BulkSelectBorder,
        Divider: BulkSelectDivider,
        ItemContainer: BulkSelectItemContainer,
      };

      createItemClickHandler = item => () => {
        if (item.selected) {
          this.props.onItemDeselected(item.data);
        } else {
          this.props.onItemSelected(item.data);
        }
      };

      renderSection = section => {
        const {
          Divider,
          Item,
          ItemContainer,
          computeItemKey,
          renderItem,
          renderItemContents,
        } = this.props;

        const resolvedRenderItemContents = renderItemContents || renderItem;

        return (
          <div key={section.title || 'all'}>
            {section.title && (
              <Divider>
                <h3>{section.title}</h3>
                <span />
              </Divider>
            )}
            <ItemContainer>
              {section.items.map(item => (
                <Item
                  key={computeItemKey(item.data)}
                  selected={item.selected}
                  disabled={item.disabled}
                  onClick={this.createItemClickHandler(item)}
                >
                  {resolvedRenderItemContents(item.data, item.selected)}
                </Item>
              ))}
            </ItemContainer>
          </div>
        );
      };

      render() {
        const { sections, Border } = this.props;

        return <Border>{sections.map(this.renderSection)}</Border>;
      }
    }

    BulkSelect.Small = withProps({ ItemContainer: BulkSelectItemContainer.Small })(
      BulkSelect,
    );

    BulkSelect.Colorful = withProps({ Item: ToggleButton.Colorful })(BulkSelect);

    export default BulkSelect;