# All files / components/ToggleButton ToggleButton.js

0% Statements 0/65
0% Branches 0/12
0% Functions 0/7
0% Lines 0/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

     import React from 'react';
    import PropTypes from 'prop-types';
    import styled, { css } from 'styled-components';
    import { defaultProps } from 'recompose';
    import get from 'extensions/themeGet';
    import { noop } from 'lodash';
    import StyledButton from './styles/StyledToggleButton';
    import ColorfulButton from './styles/ColorfulButton';
    import SmallButton from './styles/SmallButton';

    /**
     * **ToggleButton** is a simple styled button with some extra functionality built-in for handling selection. Control
     * it by setting `selected`, then use its `onClick` or `onSelect/onDeselect` handlers to implement selection or hook
     * into the [Selectable](/#!/Selectable) behavior. Set `name` to keep track of which button was pressed when a
     * click handler fires.
     */
    class ToggleButton extends React.PureComponent {
      static propTypes = {
        /**
         * Callback when the toggle is clicked. Called with the signature `(name, selected)`.
         */
        onClick: PropTypes.func,
        /**
         * Callback when the toggle is deselected. Called with the signature `(name)`.
         */
        onDeselect: PropTypes.func,
        /**
         * Callback when the toggle is selected. Called with the signature `(name)`.
         */
        onSelect: PropTypes.func,
        /**
         * Has no effect on rendering; acts as a key that you can use to identify the
         * **ToggleButton** when it invokes `onClick`, `onSelect`, or `onDeselect`.
         */
        name: PropTypes.any.isRequired,
        /**
         * Whether the button is selected or not
         */
        selected: PropTypes.bool,
        /**
         * Whether the button is hovered or not
         */
        hovered: PropTypes.bool,
      };

      static Small = defaultProps({
        Button: SmallButton,
      })(ToggleButton);

      static Colorful = defaultProps({
        Button: ColorfulButton,
      })(ToggleButton);

      static defaultProps = {
        onClick: noop,
        onDeselect: noop,
        onSelect: noop,
        selected: false,
        hovered: null,
        Button: StyledButton,
      };

      state = {
        internalHovered: false,
      };

      handleClick = ev => {
        ev.preventDefault();
        const { props: { onClick, onDeselect, onSelect, selected, name } } = this;
        onClick(name, selected);
        selected ? onDeselect(name) : onSelect(name);
      };

      handleMouseEnter = () => this.setState({ internalHovered: true });

      handleMouseLeave = () => this.setState({ internalHovered: false });

      render() {
        const {
          props: { Button, onClick, hovered, ...rest },
          state: { internalHovered },
          handleClick,
        } = this;
        return (
          <Button
            onMouseEnter={this.handleMouseEnter}
            onMouseLeave={this.handleMouseLeave}
            onMouseUp={this.handleMouseLeave}
            onClick={handleClick}
            hovered={
              hovered !== null && hovered != undefined ? hovered : internalHovered
            }
            {...rest}
          />
        );
      }
    }

    export default ToggleButton;