diff --git a/docs/src/app/components/pages/components/switches.jsx b/docs/src/app/components/pages/components/switches.jsx index df80775b52bbbb..310fb0072666be 100644 --- a/docs/src/app/components/pages/components/switches.jsx +++ b/docs/src/app/components/pages/components/switches.jsx @@ -202,9 +202,9 @@ var SwitchesPage = React.createClass({ }, { name: 'setSelectedValue', - header: 'RadioButtonGroup.setSelectedValue(newSelection)', + header: 'RadioButtonGroup.setSelectedValue(newSelectionValue)', desc: 'Sets the selected radio button to the radio button whose value matches ' + - 'newSelection' + 'newSelectionValue' }, { name: 'clearValue', @@ -338,7 +338,7 @@ var SwitchesPage = React.createClass({ name="checkboxName2" value="checkboxValue2" label="fed the dog" - defaultSwitched={true}/> + defaultChecked={true}/>
- ); }, diff --git a/src/js/checkbox.jsx b/src/js/checkbox.jsx index 3b4eb2f9c90b69..411cd6fdb94529 100644 --- a/src/js/checkbox.jsx +++ b/src/js/checkbox.jsx @@ -1,39 +1,101 @@ var React = require('react'); var EnhancedSwitch = require('./enhanced-switch'); -var Classable = require('./mixins/classable'); +var StylePropable = require('./mixins/style-propable.js'); +var Transitions = require('./styles/mixins/transitions.js'); var CheckboxOutline = require('./svg-icons/toggle-check-box-outline-blank'); var CheckboxChecked = require('./svg-icons/toggle-check-box-checked'); +var CustomVariables = require('./styles/variables/custom-variables.js'); var Checkbox = React.createClass({ - mixins: [Classable], + mixins: [StylePropable], propTypes: { onCheck: React.PropTypes.func, }, + getInitialState: function() { + return { + switched: + this.props.checked || + this.props.defaultChecked || + (this.props.valueLink && this.props.valueLink.value) || + false, + } + }, + render: function() { var { onCheck, ...other } = this.props; - var classes = this.getClasses("mui-checkbox"); + var checkboxSize = 24; + + var iconStyles = { + height: checkboxSize, + width: checkboxSize, + } + + var checkStyles = { + positiion: 'absolute', + opacity: 0, + transform: 'scale(0)', + transitionOrigin: '50% 50%', + transition: Transitions.easeOut('450ms', 'opacity', '0ms') + ', ' + + Transitions.easeOut('0ms', 'transform', '450ms'), + fill: CustomVariables.checkboxCheckedColor + } + + var boxStyles = { + position: 'absolute', + opacity: 1, + fill: CustomVariables.checkboxBoxColor, + transition: Transitions.easeOut('2s', null, '200ms') + } + + if (this.state.switched) { + checkStyles = this.mergePropStyles(checkStyles, { + opacity: 1, + transform: 'scale(1)', + transition: Transitions.easeOut('0ms', 'opacity', '0ms') + ', ' + + Transitions.easeOut('800ms', 'transform', '0ms') + }); + boxStyles = this.mergePropStyles(boxStyles, { + transition: Transitions.easeOut('100ms', null, '0ms'), + fill: CustomVariables.checkboxCheckedColor + }); + } + + if (this.props.disabled) { + checkStyles = this.mergePropStyles(checkStyles, { + opacity: 0.3, + fill: CustomVariables.disabledColor, + }); + boxStyles = this.mergePropStyles(boxStyles, { + opacity: 0.3, + fill: CustomVariables.disabledColor, + }); + } + + if (this.state.switched && this.props.disabled) boxStyles.opacity = 0; var checkboxElement = (
- - + +
); var enhancedSwitchProps = { ref: "enhancedSwitch", inputType: "checkbox", + switched: this.state.switched, switchElement: checkboxElement, - className: classes, - iconClassName: "mui-checkbox-icon", + iconStyle: iconStyles, onSwitch: this._handleCheck, + onParentShouldUpdate: this._handleStateChange, + defaultSwitched: this.props.defaultChecked, labelPosition: (this.props.labelPosition) ? this.props.labelPosition : "right" }; @@ -54,7 +116,12 @@ var Checkbox = React.createClass({ _handleCheck: function(e, isInputChecked) { if (this.props.onCheck) this.props.onCheck(e, isInputChecked); + }, + + _handleStateChange: function(newSwitched) { + this.setState({switched: newSwitched}); } + }); module.exports = Checkbox; diff --git a/src/js/enhanced-switch.jsx b/src/js/enhanced-switch.jsx index ea05a676e4c3f9..53714904e82ad0 100644 --- a/src/js/enhanced-switch.jsx +++ b/src/js/enhanced-switch.jsx @@ -1,9 +1,10 @@ var React = require('react'); var KeyCode = require('./utils/key-code'); -var Classable = require('./mixins/classable'); var DomIdable = require('./mixins/dom-idable'); var StylePropable = require('./mixins/style-propable.js'); +var Transitions = require('./styles/mixins/transitions.js'); var WindowListenable = require('./mixins/window-listenable'); +var CustomVariables = require('./styles/variables/custom-variables.js'); var FocusRipple = require('./ripples/focus-ripple'); var TouchRipple = require('./ripples/touch-ripple'); var Paper = require('./paper'); @@ -11,14 +12,18 @@ var Theme = require('./styles/theme.js').get(); var EnhancedSwitch = React.createClass({ - mixins: [Classable, DomIdable, WindowListenable, StylePropable], + mixins: [DomIdable, WindowListenable, StylePropable], propTypes: { id: React.PropTypes.string, inputType: React.PropTypes.string.isRequired, switchElement: React.PropTypes.element.isRequired, - iconClassName: React.PropTypes.string.isRequired, + onParentShouldUpdate: React.PropTypes.func.isRequired, + switched: React.PropTypes.bool.isRequired, rippleStyle: React.PropTypes.object, + iconStyle: React.PropTypes.object, + thumbStyle: React.PropTypes.object, + trackStyle: React.PropTypes.object, name: React.PropTypes.string, value: React.PropTypes.string, label: React.PropTypes.string, @@ -28,7 +33,7 @@ var EnhancedSwitch = React.createClass({ defaultSwitched: React.PropTypes.bool, labelPosition: React.PropTypes.oneOf(['left', 'right']), disableFocusRipple: React.PropTypes.bool, - disableTouchRipple: React.PropTypes.bool + disableTouchRipple: React.PropTypes.bool, }, windowListeners: { @@ -36,23 +41,27 @@ var EnhancedSwitch = React.createClass({ 'keyup': '_handleWindowKeyup' }, - getDefaultProps: function() { - return { - iconClassName: '' - }; - }, - getInitialState: function() { return { - switched: this.props.defaultSwitched || - (this.props.valueLink && this.props.valueLink.value), isKeyboardFocused: false } }, + getEvenWidth: function(){ + return ( + parseInt(window + .getComputedStyle(this.getDOMNode()) + .getPropertyValue('width'), 10) + ); + }, + componentDidMount: function() { var inputNode = this.refs.checkbox.getDOMNode(); - this.setState({switched: inputNode.checked}); + if (!this.props.switched || + this.props.switched == undefined || + inputNode.checked != this.props.switched) this.props.onParentShouldUpdate(inputNode.checked); + + this.setState({parentWidth: this.getEvenWidth()}); }, componentWillReceiveProps: function(nextProps) { @@ -72,7 +81,7 @@ var EnhancedSwitch = React.createClass({ newState.switched = nextProps.checkedLink.value; } - if (newState) this.setState(newState); + if (newState.switched != undefined && (newState.switched != this.props.switched)) this.props.onParentShouldUpdate(newState.switched); }, render: function() { @@ -92,20 +101,52 @@ var EnhancedSwitch = React.createClass({ onTouchEnd, disableTouchRipple, disableFocusRipple, - iconClassName, ...other } = this.props; - var classes = this.getClasses('mui-enhanced-switch', { - 'mui-is-switched': this.state.switched, - 'mui-is-disabled': this.props.disabled, - 'mui-is-required': this.props.required + var switchWidth = 60 - CustomVariables.spacing.desktopGutterLess; + var labelWidth = this.state.parentWidth - switchWidth - 30; + var styles = this.mergePropStyles({ + position: 'relative', + cursor: this.props.disabled ? 'default' : 'pointer', + overflow: 'visible', + display: 'table', + height: 'auto', + width: '100%' }); + var inputStyles = { + position: 'absolute', + cursor: this.props.disabled ? 'default' : 'pointer', + pointerEvents: 'all', + opacity: 0, + width: '100%', + height: '100%', + zIndex: 2, + left: 0 + }; + var wrapStyles = this.mergePropStyles({ + transition: Transitions.easeOut(), + float: 'left', + position: 'relative', + display: 'table-column', + width: switchWidth, + marginRight: (this.props.labelPosition == 'right') ? + CustomVariables.spacing.desktopGutterLess : 0, + marginLeft: (this.props.labelPosition == 'left') ? + CustomVariables.spacing.desktopGutterLess : 0 + }, this.props.iconStyle); + var labelStyles = { + float: 'left', + position: 'relative', + display: 'table-column', + width: labelWidth, + lineHeight: '24px' + } var inputId = this.props.id || this.getDomId(); var labelElement = this.props.label ? ( -