# All files / components/BulkSelect BulkSelect.js

0% Statements 0/13
0% Branches 0/12
0% Functions 0/8
0% Lines 0/13

    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

     import React from 'react';
    import PropTypes from 'prop-types';
    import theme from '../../theme';
    import styled from 'styled-components';
    import BulkSelectItem from './styles/BulkSelectItem';
    import BulkSelectBorder from './styles/BulkSelectBorder';
    import BulkSelectDivider from './styles/BulkSelectDivider';
    import BulkSelectItemContainer from './styles/BulkSelectItemContainer';

    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: BulkSelectItem,
        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}
                  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>;
      }
    }

    export default BulkSelect;