All files / src/components SDCheckbox.react.js

100% Statements 20/20
75% Branches 9/12
100% Functions 8/8
100% Lines 19/19
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                                                1x                                                                                                                         1x                             4x 4x       2x 2x       3x   3x 3x   3x   3x         8x 8x 6x                     1x           2x                       1x                 1x 1x  
// @flow
 
import React, { Component } from 'react';
import PropTypes from 'prop-types';
 
import Checkbox 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 = {
  checked?: boolean,
  disabled?: boolean,
  fireEvent?: () => void,
  iconStyle?: Object,
  id: string,
  inputStyle?: Object,
  label?: string,
  labelPosition?: string,
  labelStyle?: Object,
  setProps?: () => void,
  style?: Object,
};
 
const propTypes = {
  /**
   * Checkbox is checked if true.
   */
  checked: PropTypes.bool,
 
  /**
   * Disabled if true.
   */
  disabled: PropTypes.bool,
 
  /**
   * A callback for firing events to dash.
   */
  fireEvent: PropTypes.func,
 
  /**
   * Overrides the inline-styles of the icon element.
   */
  iconStyle: PropTypes.objectOf(PropTypes.any),
 
  /**
   * The element's ID
   */
  id: PropTypes.string.isRequired,
 
  /**
   * Overrides the inline-styles of the input element.
   */
  inputStyle: PropTypes.objectOf(PropTypes.any),
 
  /**
   * The text label for the checkbox
   */
  label: PropTypes.string,
 
  /**
   * Where the label will be placed next to the checkbox.
   */
  labelPosition: PropTypes.string,
 
  /**
   * Overrides the inline-styles of the Checkbox element label.
   */
  labelStyle: PropTypes.objectOf(PropTypes.any),
 
  /**
   * Dash callback to update props on the server
   */
  setProps: PropTypes.func,
 
  /**
   * Override the inline-styles of the root element.
   */
  style: PropTypes.objectOf(PropTypes.any),
};
 
type State = {
  checked: boolean,
};
 
const defaultProps = {
  checked: false,
  disabled: false,
  fireEvent: () => {},
  iconStyle: {},
  inputStyle: {},
  label: '',
  labelPosition: 'right',
  labelStyle: {},
  setProps: () => {},
  style: {},
};
 
export default class SDCheckbox 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)}>
            <Checkbox
              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>);
    } else {
      return (
        <div id={id}>
          <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
            <Checkbox
              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>);
    }
  }
}
 
SDCheckbox.propTypes = propTypes;
SDCheckbox.defaultProps = defaultProps;