# All files / components/Button SubmitButton.js

0% Statements 0/9
0% Branches 0/7
0% Functions 0/3
0% Lines 0/9

    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

     import React from 'react';
    import PropTypes from 'prop-types';
    import Button from './Button';
    import Loader from '../Loader';

    class SubmitButton extends React.Component {
      static propTypes = {
        /**
         * Indicates that the form has not been touched yet. Will disable submission.
         */
        pristine: PropTypes.bool,
        /**
         * Indicates that the form is loading something. Will disable submission.
         */
        loading: PropTypes.bool,
        /**
         * Indicates that the user cannot submit the form.
         */
        disabled: PropTypes.bool,
        /**
         * Glyph name for an icon to pop out of the right side of the button on hover.
         */
        rightIcon: PropTypes.string,
        /**
         * Glyph name for an icon to pop out of the left side of the butotn on hover.
         */
        leftIcon: PropTypes.string,
        /**
         * Contents of the button.
         */
        children: PropTypes.node.isRequired,
        /**
         * Alternate optional contents of the button when 'pristine' is true.
         */
        pristineContents: PropTypes.node,
        /**
         * Adds an id to the button.
         */
        id: PropTypes.string,
        /**
         * Adds a class name to the button.
         */
        className: PropTypes.string,
        /**
         * Adds a click handler to the button
         */
        onClick: PropTypes.func,
      };

      static defaultProps = {
        pristine: false,
        loading: false,
        disabled: false,
        rightIcon: null,
        leftIcon: null,
        pristineContents: 'Up to Date',
        id: null,
        className: null,
        onClick: () => null,
      };

      renderContents = () => {
        const { pristine, loading, children, pristineContents } = this.props;

        if (pristine) {
          return <span>{pristineContents}</span>;
        }
        if (loading) {
          return <Loader size="14px" />;
        }

        return children;
      };

      render() {
        const {
          pristine,
          loading,
          disabled,
          leftIcon,
          rightIcon,
          id,
          className,
          onClick,
        } = this.props;
        return (
          <Button
            disabled={pristine || loading || disabled}
            leftIcon={leftIcon}
            rightIcon={rightIcon}
            type="submit"
            id={id}
            className={className}
            onClick={onClick}
          >
            {this.renderContents()}
          </Button>
        );
      }
    }

    export default SubmitButton;