All files / src/components/Checkbox Checkbox.react.js

100% Statements 18/18
75% Branches 9/12
100% Functions 8/8
100% Lines 17/17

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                                                                            1x                             4x 4x       2x 2x       3x   3x 3x   3x   3x         8x 8x 6x                     1x           2x                       1x               1x  
// @flow
 
import React, { Component } from 'react';
 
import MuiCheckbox from 'material-ui/Checkbox';
import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
 
type Props = {
  /** Checkbox is checked if true */
  checked?: boolean,
  /** Checkbox is disabled if true */
  disabled?: boolean,
  /** A callback for firing events to dash */
  fireEvent?: () => void,
  /** Overrides the inline-styles of the icon element */
  iconStyle?: Object,
  /** The element's ID */
  id: string,
  /** Overrides the inline styles of the input element */
  inputStyle?: Object,
  /** The text label for the checkbox */
  label?: string,
  /** Where the label will be placed next to the checkbox */
  labelPosition?: 'left' | 'right',
  /** Overrides the inline styles of the Checkbox element label */
  labelStyle?: Object,
  /** Dash callback to update props on the server */
  setProps?: () => void,
  /** Override the inline styles of the root element */
  style?: Object,
};
 
type State = {
  checked: boolean,
};
 
const defaultProps = {
  checked: false,
  disabled: false,
  fireEvent: () => {},
  iconStyle: {},
  inputStyle: {},
  label: '',
  labelPosition: 'right',
  labelStyle: {},
  setProps: () => {},
  style: {},
};
 
export default class Checkbox extends Component<Props, State> {
  constructor(props: Props) {
    super(props);
    this.state = {checked: props.checked};
  }
 
  componentWillReceiveProps(nextProps: Props): void {
    Eif (nextProps.checked !== null && nextProps.checked !== this.props.checked)
      this.handleClick(nextProps.checked);
  }
 
  handleClick = (checked: boolean) => {
    const { setProps } = this.props;
 
    Eif (typeof setProps === 'function')
      setProps({checked});
 
    this.setState({checked});
 
    Eif (this.props.fireEvent) this.props.fireEvent({event: 'click'});
  };
 
  render() {
    const { disabled, iconStyle, id, inputStyle, label, labelPosition, labelStyle,
      style} = this.props;
    if (this.props.fireEvent || this.props.setProps) {
      return (
        <div id={id}>
          <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
            <MuiCheckbox
              checked={this.state.checked}
              disabled={disabled}
              iconStyle={iconStyle}
              inputStyle={inputStyle}
              label={label}
              labelPosition={labelPosition}
              labelStyle={labelStyle}
              onCheck={(event: object, isInputChecked: boolean) => this.handleClick(isInputChecked)}
              style={style}
            />
          </MuiThemeProvider>
        </div>);
    }
    return (
      <div id={id}>
        <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
          <MuiCheckbox
            checked={this.state.checked}
            disabled={disabled}
            iconStyle={iconStyle}
            inputStyle={inputStyle}
            label={label}
            labelPosition={labelPosition}
            labelStyle={labelStyle}
            onCheck={(event: object, isInputChecked: boolean) =>
              this.setState({checked: isInputChecked})}
            style={style}
          />
        </MuiThemeProvider>
      </div>);
  }
}
 
Checkbox.defaultProps = defaultProps;