11var React = require ( 'react' ) ;
22var KeyCode = require ( './utils/key-code' ) ;
3- var Classable = require ( './mixins/classable' ) ;
43var DomIdable = require ( './mixins/dom-idable' ) ;
54var StylePropable = require ( './mixins/style-propable.js' ) ;
5+ var Transitions = require ( './styles/mixins/transitions.js' ) ;
66var WindowListenable = require ( './mixins/window-listenable' ) ;
7+ var CustomVariables = require ( './styles/variables/custom-variables.js' ) ;
78var FocusRipple = require ( './ripples/focus-ripple' ) ;
89var TouchRipple = require ( './ripples/touch-ripple' ) ;
910var Paper = require ( './paper' ) ;
1011var Theme = require ( './styles/theme.js' ) . get ( ) ;
1112
1213var EnhancedSwitch = React . createClass ( {
1314
14- mixins : [ Classable , DomIdable , WindowListenable , StylePropable ] ,
15+ mixins : [ DomIdable , WindowListenable , StylePropable ] ,
1516
1617 propTypes : {
1718 id : React . PropTypes . string ,
1819 inputType : React . PropTypes . string . isRequired ,
1920 switchElement : React . PropTypes . element . isRequired ,
20- iconClassName : React . PropTypes . string . isRequired ,
21+ onParentShouldUpdate : React . PropTypes . func . isRequired ,
22+ switched : React . PropTypes . bool . isRequired ,
2123 rippleStyle : React . PropTypes . object ,
24+ iconStyle : React . PropTypes . object ,
25+ thumbStyle : React . PropTypes . object ,
26+ trackStyle : React . PropTypes . object ,
2227 name : React . PropTypes . string ,
2328 value : React . PropTypes . string ,
2429 label : React . PropTypes . string ,
@@ -28,31 +33,35 @@ var EnhancedSwitch = React.createClass({
2833 defaultSwitched : React . PropTypes . bool ,
2934 labelPosition : React . PropTypes . oneOf ( [ 'left' , 'right' ] ) ,
3035 disableFocusRipple : React . PropTypes . bool ,
31- disableTouchRipple : React . PropTypes . bool
36+ disableTouchRipple : React . PropTypes . bool ,
3237 } ,
3338
3439 windowListeners : {
3540 'keydown' : '_handleWindowKeydown' ,
3641 'keyup' : '_handleWindowKeyup'
3742 } ,
3843
39- getDefaultProps : function ( ) {
40- return {
41- iconClassName : ''
42- } ;
43- } ,
44-
4544 getInitialState : function ( ) {
4645 return {
47- switched : this . props . defaultSwitched ||
48- ( this . props . valueLink && this . props . valueLink . value ) ,
4946 isKeyboardFocused : false
5047 }
5148 } ,
5249
50+ getEvenWidth : function ( ) {
51+ return (
52+ parseInt ( window
53+ . getComputedStyle ( this . getDOMNode ( ) )
54+ . getPropertyValue ( 'width' ) , 10 )
55+ ) ;
56+ } ,
57+
5358 componentDidMount : function ( ) {
5459 var inputNode = this . refs . checkbox . getDOMNode ( ) ;
55- this . setState ( { switched : inputNode . checked } ) ;
60+ if ( ! this . props . switched ||
61+ this . props . switched == undefined ||
62+ inputNode . checked != this . props . switched ) this . props . onParentShouldUpdate ( inputNode . checked ) ;
63+
64+ this . setState ( { parentWidth : this . getEvenWidth ( ) } ) ;
5665 } ,
5766
5867 componentWillReceiveProps : function ( nextProps ) {
@@ -72,7 +81,7 @@ var EnhancedSwitch = React.createClass({
7281 newState . switched = nextProps . checkedLink . value ;
7382 }
7483
75- if ( newState ) this . setState ( newState ) ;
84+ if ( newState . switched != undefined && ( newState . switched != this . props . switched ) ) this . props . onParentShouldUpdate ( newState . switched ) ;
7685 } ,
7786
7887 render : function ( ) {
@@ -92,20 +101,52 @@ var EnhancedSwitch = React.createClass({
92101 onTouchEnd,
93102 disableTouchRipple,
94103 disableFocusRipple,
95- iconClassName,
96104 ...other
97105 } = this . props ;
98106
99- var classes = this . getClasses ( 'mui-enhanced-switch' , {
100- 'mui-is-switched' : this . state . switched ,
101- 'mui-is-disabled' : this . props . disabled ,
102- 'mui-is-required' : this . props . required
107+ var switchWidth = 60 - CustomVariables . spacing . desktopGutterLess ;
108+ var labelWidth = this . state . parentWidth - switchWidth - 30 ;
109+ var styles = this . mergePropStyles ( {
110+ position : 'relative' ,
111+ cursor : this . props . disabled ? 'default' : 'pointer' ,
112+ overflow : 'visible' ,
113+ display : 'table' ,
114+ height : 'auto' ,
115+ width : '100%'
103116 } ) ;
117+ var inputStyles = {
118+ position : 'absolute' ,
119+ cursor : this . props . disabled ? 'default' : 'pointer' ,
120+ pointerEvents : 'all' ,
121+ opacity : 0 ,
122+ width : '100%' ,
123+ height : '100%' ,
124+ zIndex : 2 ,
125+ left : 0
126+ } ;
127+ var wrapStyles = this . mergePropStyles ( {
128+ transition : Transitions . easeOut ( ) ,
129+ float : 'left' ,
130+ position : 'relative' ,
131+ display : 'table-column' ,
132+ width : switchWidth ,
133+ marginRight : ( this . props . labelPosition == 'right' ) ?
134+ CustomVariables . spacing . desktopGutterLess : 0 ,
135+ marginLeft : ( this . props . labelPosition == 'left' ) ?
136+ CustomVariables . spacing . desktopGutterLess : 0
137+ } , this . props . iconStyle ) ;
138+ var labelStyles = {
139+ float : 'left' ,
140+ position : 'relative' ,
141+ display : 'table-column' ,
142+ width : labelWidth ,
143+ lineHeight : '24px'
144+ }
104145
105146 var inputId = this . props . id || this . getDomId ( ) ;
106147
107148 var labelElement = this . props . label ? (
108- < label className = "mui-switch-label" htmlFor = { inputId } >
149+ < label style = { labelStyles } htmlFor = { inputId } >
109150 { this . props . label }
110151 </ label >
111152 ) : null ;
@@ -133,7 +174,7 @@ var EnhancedSwitch = React.createClass({
133174 < input
134175 { ...other }
135176 { ...inputProps }
136- className = "mui-enhanced-switch-input" />
177+ style = { inputStyles } />
137178 ) ;
138179
139180 var rippleStyle = this . mergePropStyles ( {
@@ -148,15 +189,15 @@ var EnhancedSwitch = React.createClass({
148189 ref = "touchRipple"
149190 key = "touchRipple"
150191 style = { rippleStyle }
151- color = { this . state . switched ? Theme . primary1Color : Theme . textColor }
192+ color = { this . props . switched ? Theme . primary1Color : Theme . textColor }
152193 centerRipple = { true } />
153194 ) ;
154195
155196 var focusRipple = (
156197 < FocusRipple
157198 key = "focusRipple"
158199 innerStyle = { rippleStyle }
159- color = { this . state . switched ? Theme . primary1Color : Theme . textColor }
200+ color = { this . props . switched ? Theme . primary1Color : Theme . textColor }
160201 show = { this . state . isKeyboardFocused } />
161202 ) ;
162203
@@ -165,17 +206,17 @@ var EnhancedSwitch = React.createClass({
165206 this . props . disabled || disableFocusRipple ? null : focusRipple
166207 ] ;
167208
168- iconClassName += ' mui-enhanced-switch-wrap' ;
169-
170- var switchElement = ( this . props . iconClassName . indexOf ( "toggle" ) == - 1 ) ? (
171- < div className = { iconClassName } >
209+ // If toggle component (indicated by whether the style includes thumb) manually lay out
210+ // elements in order to nest ripple elements
211+ var switchElement = ! this . props . thumbStyle ? (
212+ < div style = { wrapStyles } >
172213 { this . props . switchElement }
173214 { ripples }
174215 </ div >
175216 ) : (
176- < div className = { iconClassName } >
177- < div className = "mui-toggle-track" />
178- < Paper className = "mui-toggle-thumb" zDepth = { 1 } > { ripples } </ Paper >
217+ < div style = { wrapStyles } >
218+ < div style = { this . props . trackStyle } />
219+ < Paper style = { this . props . thumbStyle } zDepth = { 1 } > { ripples } </ Paper >
179220 </ div >
180221 ) ;
181222
@@ -196,7 +237,7 @@ var EnhancedSwitch = React.createClass({
196237 ) ;
197238
198239 return (
199- < div className = { classes } >
240+ < div style = { styles } >
200241 { inputElement }
201242 { elementsInOrder }
202243 </ div >
@@ -211,7 +252,8 @@ var EnhancedSwitch = React.createClass({
211252 // no callback here because there is no event
212253 setSwitched : function ( newSwitchedValue ) {
213254 if ( ! this . props . hasOwnProperty ( 'checked' ) || this . props . checked == false ) {
214- this . setState ( { switched : newSwitchedValue } ) ;
255+
256+ this . props . onParentShouldUpdate ( newSwitchedValue ) ;
215257 this . refs . checkbox . getDOMNode ( ) . checked = newSwitchedValue ;
216258 } else {
217259 var message = 'Cannot call set method while checked is defined as a property.' ;
@@ -228,15 +270,14 @@ var EnhancedSwitch = React.createClass({
228270 } ,
229271
230272 _handleChange : function ( e ) {
231-
232273 this . _tabPressed = false ;
233274 this . setState ( {
234275 isKeyboardFocused : false
235276 } ) ;
236277
237278 var isInputChecked = this . refs . checkbox . getDOMNode ( ) . checked ;
238279
239- if ( ! this . props . hasOwnProperty ( 'checked' ) ) this . setState ( { switched : isInputChecked } ) ;
280+ if ( ! this . props . hasOwnProperty ( 'checked' ) ) this . props . onParentShouldUpdate ( isInputChecked ) ;
240281 if ( this . props . onSwitch ) this . props . onSwitch ( e , isInputChecked ) ;
241282 } ,
242283
0 commit comments