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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | 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 Toggle 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 = { disabled?: boolean, elementStyle?: Object, fireEvent?: () => void, iconStyle?: Object, id: string, inputStyle?: Object, label?: Node, labelPosition?: string, labelStyle?: Object, rippleStyle?: Object, setProps?: () => void, style?: Object, thumbStyle?: Object, thumbSwitchedStyle?: Object, toggled?: boolean, trackStyle?: Object, trackSwitchedStyle?: Object, }; const propTypes = { /** * Will disable the toggle if true. */ disabled: PropTypes.bool, /** * Overrides the inline-styles of the Toggle element. */ elementStyle: PropTypes.objectOf(PropTypes.any), /** * 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), /** * Label for toggle. */ label: PropTypes.node, /** * Where the label will be placed next to the toggle. */ labelPosition: PropTypes.string, /** * Overrides the inline-styles of the Toggle element label. */ labelStyle: PropTypes.objectOf(PropTypes.any), /** * Override style of ripple. */ rippleStyle: 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), /** * Override style for thumb. */ thumbStyle: PropTypes.objectOf(PropTypes.any), /** * Override the inline styles for thumb when the toggle switch is toggled on. */ thumbSwitchedStyle: PropTypes.objectOf(PropTypes.any), /** * Toggled if set to true. */ toggled: PropTypes.bool, /** * Override style for track. */ trackStyle: PropTypes.objectOf(PropTypes.any), /** * Override the inline styles for track when the toggle switch is toggled on. */ trackSwitchedStyle: PropTypes.objectOf(PropTypes.any), }; 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 SDToggle 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)}> <Toggle 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> ); } else { return ( <div id={id}> <MuiThemeProvider muiTheme={getMuiTheme(lightBaseTheme)}> <Toggle 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> ); } } } SDToggle.propTypes = propTypes; SDToggle.defaultProps = defaultProps; |