# All files / components/Table TableHeader.js

0% Statements 0/67
0% Branches 0/14
0% Functions 0/9
0% Lines 0/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

     import React from 'react';
    import PropTypes from 'prop-types';
    import { withProps } from 'recompose';
    import styled, { css } from 'styled-components';
    import { sentence } from 'change-case';
    import DefaultAnchor from '../Anchor';
    import TableHeaderSortArrowIcon from './styles/TableHeaderSortArrowIcon';
    import TableHeaderStyles from './styles/TableHeaderStyles';
    import TableHeaderSortArrows from './styles/TableHeaderSortArrows';
    import TableHeaderColumnName from './styles/TableHeaderColumnName';

    class TableHeader extends React.Component {
      static propTypes = {
        /**
         * Contents of the header cell.
         */
        children: PropTypes.node.isRequired,
        /**
         * [Deprecated] use onClick
         */
        handleClick: PropTypes.func,
        /**
         * Called when the user clicks the header cell.
         */
        onClick: PropTypes.func,
        /**
         * Specifies the sorting order of this column. [-1, 0, 1] for descending, none, or ascending.
         */
        sortOrder: PropTypes.number,
        /**
         * Indicates whether this column is sortable. This changes how it's rendered.
         */
        sortable: PropTypes.bool,
        /**
         * Adds a class name to the element.
         */
        className: PropTypes.string,
        /**
         * Adds an id to the element.
         */
        id: PropTypes.string,
        /**
         * A component for rendering an icon, used for sort arrows.
         */
        Icon: PropTypes.func,
        /**
         * An anchor component. Defaults Anchor
         */
        Anchor: PropTypes.func,
        /**
         * A component for rendering the th element styles
         */
        Styles: PropTypes.func,
        /**
         * A component for rendering the sort arrow container
         */
        SortArrows: PropTypes.func,
        /**
         * A component for rendering the column name text
         */
        ColumnName: PropTypes.func,
      };

      static defaultProps = {
        sortable: false,
        sortOrder: 0,
        handleClick: () => null,
        onClick: () => null,
        className: null,
        id: null,
        Icon: TableHeaderSortArrowIcon,
        Anchor: DefaultAnchor,
        Styles: TableHeaderStyles,
        SortArrows: TableHeaderSortArrows,
        ColumnName: TableHeaderColumnName,
      };

      createClickHandler = naturalOrder => () =>
        this.props.onClick
          ? this.props.onClick(naturalOrder)
          : this.props.handleClick(naturalOrder);

      renderColumnName = () => {
        const { sortable, children, sortOrder, Anchor, ColumnName } = this.props;

        if (sortable) {
          return (
            <Anchor
              type="text"
              onClick={this.createClickHandler(sortOrder === 0 ? 1 : -sortOrder)}
            >
              <ColumnName sortable>{children}</ColumnName>
            </Anchor>
          );
        }

        return <ColumnName>{children}</ColumnName>;
      };

      render() {
        const {
          sortable,
          sortOrder,
          handleClick,
          onClick,
          id,
          className,
          Styles,
          SortArrows,
          Anchor,
          Icon,
        } = this.props;

        return (
          <Styles sortable={sortable} className={className} id={id}>
            {this.renderColumnName()}
            {sortable && (
              <SortArrows sortOrder={sortOrder}>
                <Anchor type="icon" onClick={this.createClickHandler(1)}>
                  <Icon name="up" />
                </Anchor>
                <Anchor type="icon" onClick={this.createClickHandler(-1)}>
                  <Icon name="down" />
                </Anchor>
              </SortArrows>
            )}
          </Styles>
        );
      }
    }

    TableHeader.Small = withProps({ Styles: TableHeaderStyles.Small })(TableHeader);

    export default TableHeader;