# All files / components/SidebarList SidebarList.js

5.88% Statements 1/17
0% Branches 0/4
0% Functions 0/8
5.88% Lines 1/17

    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

    4x

     import React from 'react';
    import PropTypes from 'prop-types';
    import styled from 'styled-components';
    import SidebarListShowMore from './styles/SidebarListShowMore';
    import SidebarListItem from './SidebarListItem';
    import SidebarListContainer from './styles/SidebarListContainer';

    class SidebarList extends React.Component {
      static propTypes = {
        /**
         * Optional: indicate which item should be considered active.
         */
        selectedIndex: PropTypes.number,
        /**
         * List items. An 'active' prop will be passed to the item that matches selectedIndex.
         */
        children: PropTypes.node.isRequired,
        /**
         * Indicates whether the list should show that it has a next page.
         */
        hasNextPage: PropTypes.bool,
        /**
         * Indicates whether the list should show that it has a previous page.
         */
        hasPreviousPage: PropTypes.bool,
        /**
         * Called when the button to load the next page is clicked.
         */
        onNextPageClicked: PropTypes.func,
        /**
         * Called when the button to load the previous page is clicked.
         */
        onPreviousPageClicked: PropTypes.func,
        /**
         * Adds a class name to the containing list element.
         */
        className: PropTypes.string,
        /**
         * Adds an id to the containing list element.
         */
        id: PropTypes.string,
        /**
         * A compoennt to render the container around the list
         */
        Container: PropTypes.func,
        /**
         * A container that renders a clickable 'show more' area
         */
        ShowMore: PropTypes.func,
      };

      static defaultProps = {
        selectedIndex: -1,
        hasNextPage: false,
        hasPreviousPage: false,
        onNextPageClicked: () => null,
        onPreviousPageClicked: () => null,
        className: null,
        id: null,
        Container: SidebarListContainer,
        ShowMore: SidebarListShowMore,
      };

      renderPrevious = () => {
        const { hasPreviousPage, onPreviousPageClicked, ShowMore } = this.props;
        if (hasPreviousPage) {
          return <ShowMore onClick={onPreviousPageClicked}>Show Previous</ShowMore>;
        }

        return null;
      };

      renderNext = () => {
        const { hasNextPage, onNextPageClicked, ShowMore } = this.props;
        if (hasNextPage) {
          return <ShowMore onClick={onNextPageClicked}>Show Next</ShowMore>;
        }

        return null;
      };

      renderItems = () => {
        const { selectedIndex } = this.props;

        return React.Children.toArray(this.props.children)
          .filter(child => child !== null)
          .map((listItem, idx) =>
            React.cloneElement(listItem, {
              key: idx,
              active: idx === selectedIndex,
            }),
          );
      };

      render() {
        const { className, id, Container } = this.props;
        return (
          <Container className={className} id={id}>
            {this.renderPrevious()}
            {this.renderItems()}
            {this.renderNext()}
          </Container>
        );
      }
    }

    SidebarList.Item = SidebarListItem;
    export default SidebarList;