All files / molecules/Button Button.jsx

11.62% Statements 5/43
0% Branches 0/20
0% Functions 0/12
13.15% Lines 5/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                                                                                                                                                                                                                                                              1x 1x   1x                                                                                               1x                                           1x      
import React, { Children, Component, createRef } from 'react';
import PropTypes from 'prop-types';
 
import Context from '../../Theme/Context';
import withStyle from '../../Theme/withStyle';
import Paper from '../../atoms/Paper';
import Ripple from '../../fx/Ripple/Ripple';
 
import Spinner from '../Spinner';
import wrapChild from './wrapChild';
 
class Button extends Component {
  state = {
    hasHighlight: false,
  };
 
  contentRef = createRef();
 
  rippleRef = createRef();
 
  get css() {
    const { css, round } = this.props;
    if (round) {
      const { width, height, minHeight, ...rest } = css;
      return {
        ...rest,
        paddingTop: 0,
        paddingLeft: 0,
        paddingBottom: 0,
        paddingRight: 0,
      };
    }
    return css;
  }
 
  get content() {
    const { busy, children } = this.props;
    return Children.map(children, wrapChild(busy));
  }
 
  handleBlur = () => {
    const { current } = this.rippleRef;
    if (current) current.removeFocus();
  };
 
  handleFocus = event => {
    const { current } = this.rippleRef;
    if (current) current.setFocus(event, this.contentRef);
  };
 
  handleMouseEnter = () => {
    this.setState({ hasHighlight: !this.context.touchDetected });
  };
 
  handleMouseLeave = () => this.setState({ hasHighlight: false });
 
  handleDown = event => {
    const { current } = this.rippleRef;
    if (current) current.pushEvent(event);
    this.props.onPointerDown(event);
  };
 
  handleUp = event => {
    const { current } = this.rippleRef;
    if (current) current.release(event);
    this.props.onPointerUp(event);
  };
 
  render() {
    const {
      autoHeight,
      blockLevel,
      busy,
      children,
      css,
      color,
      element,
      fitAll,
      fitLeft,
      fitRight,
      large,
      noRipple,
      onPointerDown,
      onPointerUp,
      outline,
      round,
      small,
      spinnerProps,
      vertical,
      ...rest
    } = this.props;
 
    const { hasFocus, hasHighlight } = this.state;
    return (
      <Paper
        button
        element={element}
        tabIndex={0}
        atomRef={this.contentRef}
        color={color}
        {...rest}
        css={this.css}
        onBlur={this.handleBlur}
        onFocus={this.handleFocus}
        onMouseEnter={this.handleMouseEnter}
        onMouseLeave={this.handleMouseLeave}
        onPointerDown={this.handleDown}
        onPointerUp={this.handleUp}
        round={round}
      >
        {!noRipple && (
          <Ripple
            {...rest}
            color={color}
            highlight={hasHighlight}
            forceShow={hasHighlight}
            focus={hasFocus}
            ref={this.rippleRef}
          />
        )}
        {this.content}
        {busy && <Spinner blockLevel color={color} {...spinnerProps} />}
      </Paper>
    );
  }
}
 
Button.displayName = 'Button';
Button.contextType = Context;
 
Button.propTypes = {
  /** height: auto */
  autoHeight: PropTypes.bool,
  /** display: block */
  blockLevel: PropTypes.bool,
  /** Enables spinner state */
  busy: PropTypes.bool,
  /** Button content */
  children: PropTypes.node.isRequired,
  /** Sets color for texts and borders */
  color: PropTypes.string,
  /** Custom styling */
  css: PropTypes.shape(),
  /** Change dom element */
  element: PropTypes.node,
  /** Disable paddings */
  fitAll: PropTypes.bool,
  /** Disable left padding  */
  fitLeft: PropTypes.bool,
  /** Disable right padding  */
  fitRight: PropTypes.bool,
  /** Large size  */
  large: PropTypes.bool,
  /** Disable ripple effect  */
  noRipple: PropTypes.bool,
  onPointerDown: PropTypes.func,
  onPointerUp: PropTypes.func,
  /** Otlined mode  */
  outline: PropTypes.bool,
  /** Rounds button  */
  round: PropTypes.bool,
  /** Required   */
  size(props, propName) {
    if (props.round === true && props[propName] === undefined) {
      return typeof props[propName] === 'number'
        ? new Error('Please provide a size!')
        : new Error('Size should be a number');
    }
    return undefined;
  },
  /** Small size  */
  small: PropTypes.bool,
  /** Spinner props  */
  spinnerProps: PropTypes.shape(),
  /** Render children vertically  */
  vertical: PropTypes.bool,
};
 
Button.defaultProps = {
  autoHeight: false,
  blockLevel: false,
  busy: false,
  color: null,
  css: {},
  element: 'button',
  fitAll: false,
  fitLeft: false,
  fitRight: false,
  large: false,
  noRipple: false,
  onPointerDown() {},
  onPointerUp() {},
  outline: false,
  round: false,
  size: null,
  small: false,
  spinnerProps: null,
  vertical: false,
};
 
export const SimpleButton = Button;
 
export default withStyle(Button);