All files / src/components/Toggle Toggle.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 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                                                                                                    1x                                         4x 4x       2x 2x       3x   3x 3x   3x   3x           8x   8x 6x                       1x                         2x                       1x                           1x  
// @flow
 
import React, { Component } from 'react';
 
import MuiToggle from 'material-ui/Toggle';
import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
 
type Props = {
  /** Toggle disabled? */
  disabled?: boolean,
  /** Override the inline style of the Toggle element */
  elementStyle?: Object,
  /** Dash event handler for click events */
  fireEvent?: () => void,
  /** Override the inline style of the Icon element */
  iconStyle?: Object,
  /** Element ID */
  id: string,
  /** Override the inline styles of the input element */
  inputStyle?: Object,
  /** Label for toggle */
  label?: Node,
  /** Where the label will be placed next to the toggle */
  labelPosition?: 'left' | 'right',
  /** Override the inline styles of the Toggle element label */
  labelStyle?: Object,
  /** Override ripple style on click */
  rippleStyle?: Object,
  /** Dash callback to update props */
  setProps?: () => void,
  /** Override the inline styles of the root element */
  style?: Object,
  /** Override style for thumb */
  thumbStyle?: Object,
  /** Override the inline styles for thumb when the toggle switches */
  thumbSwitchedStyle?: Object,
  /** Toggled on if true */
  toggled?: boolean,
  /** Override the inline style for track */
  trackStyle?: Object,
  /** Override the inline styles for track when the toggle switches */
  trackSwitchedStyle?: Object,
};
 
type State = {
  switched: boolean,
};
 
const defaultProps = {
  disabled: false,
  elementStyle: {},
  fireEvent: () => {},
  iconStyle: {},
  inputStyle: {},
  label: '',
  labelPosition: 'left',
  labelStyle: {},
  rippleStyle: {},
  setProps: () => {},
  style: {},
  thumbStyle: {},
  thumbSwitchedStyle: {},
  toggled: false,
  trackStyle: {},
  trackSwitchedStyle: {},
};
 
export default class Toggle extends Component<Props, State> {
  constructor(props: Props) {
    super(props);
    this.state = {switched: props.toggled};
  }
 
  componentWillReceiveProps(nextProps: Props): void {
    Eif (nextProps.toggled !== null && nextProps.toggled !== this.props.toggled)
      this.handleToggle(nextProps.toggled);
  }
 
  handleToggle = (toggled: boolean) => {
    const { setProps } = this.props;
 
    Eif (typeof setProps === 'function')
      setProps({toggled});
 
    this.setState({switched: toggled});
 
    Eif (this.props.fireEvent) this.props.fireEvent({event: 'click'});
  };
 
  render() {
    const { disabled, elementStyle, iconStyle, id, inputStyle, label, labelPosition,
      labelStyle, rippleStyle, style, thumbStyle, thumbSwitchedStyle,
      trackStyle, trackSwitchedStyle } = this.props;
 
    if (this.props.fireEvent || this.props.setProps) {
      return (
        <div id={id}>
          <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
            <MuiToggle
              disabled={disabled}
              elementStyle={elementStyle}
              iconStyle={iconStyle}
              inputStyle={inputStyle}
              label={label}
              labelPosition={labelPosition}
              labelStyle={labelStyle}
              onToggle={(event: object, isInputChecked: boolean) =>
                this.handleToggle(isInputChecked)}
              rippleStyle={rippleStyle}
              style={style}
              thumbStyle={thumbStyle}
              thumbSwitchedStyle={thumbSwitchedStyle}
              toggled={this.state.switched}
              trackStyle={trackStyle}
              trackSwitchedStyle={trackSwitchedStyle}
            />
          </MuiThemeProvider>
        </div>
      );
    }
    return (
      <div id={id}>
        <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}>
          <MuiToggle
            disabled={disabled}
            elementStyle={elementStyle}
            iconStyle={iconStyle}
            inputStyle={inputStyle}
            label={label}
            labelposition={labelPosition}
            labelStyle={labelStyle}
            onToggle={(event: object, isInputChecked: boolean) =>
              this.setState({switched: isInputChecked})}
            rippleStyle={rippleStyle}
            style={style}
            thumbStyle={thumbStyle}
            thumbSwitchedStyle={thumbSwitchedStyle}
            toggled={this.state.switched}
            trackStyle={trackStyle}
            trackSwitchedStyle={trackSwitchedStyle}
          />
        </MuiThemeProvider>
      </div>);
  }
}
 
Toggle.defaultProps = defaultProps;