|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 'use strict';
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
-
- var _extends2 = require('babel-runtime/helpers/extends');
-
- var _extends3 = _interopRequireDefault(_extends2);
-
- var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
-
- var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
-
- var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
-
- var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
-
- var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
-
- var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
-
- var _createClass2 = require('babel-runtime/helpers/createClass');
-
- var _createClass3 = _interopRequireDefault(_createClass2);
-
- var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
-
- var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
-
- var _inherits2 = require('babel-runtime/helpers/inherits');
-
- var _inherits3 = _interopRequireDefault(_inherits2);
-
- var _simpleAssign = require('simple-assign');
-
- var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
-
- var _react = require('react');
-
- var _react2 = _interopRequireDefault(_react);
-
- var _propTypes = require('prop-types');
-
- var _propTypes2 = _interopRequireDefault(_propTypes);
-
- var _transitions = require('../styles/transitions');
-
- var _transitions2 = _interopRequireDefault(_transitions);
-
- var _Paper = require('../Paper');
-
- var _Paper2 = _interopRequireDefault(_Paper);
-
- var _EnhancedSwitch = require('../internal/EnhancedSwitch');
-
- var _EnhancedSwitch2 = _interopRequireDefault(_EnhancedSwitch);
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- function getStyles(props, context, state) {
- var disabled = props.disabled,
- elementStyle = props.elementStyle,
- trackSwitchedStyle = props.trackSwitchedStyle,
- thumbSwitchedStyle = props.thumbSwitchedStyle,
- trackStyle = props.trackStyle,
- thumbStyle = props.thumbStyle,
- iconStyle = props.iconStyle,
- rippleStyle = props.rippleStyle,
- labelStyle = props.labelStyle;
- var _context$muiTheme = context.muiTheme,
- baseTheme = _context$muiTheme.baseTheme,
- toggle = _context$muiTheme.toggle;
-
-
- var toggleSize = 20;
- var toggleTrackWidth = 36;
- var styles = {
- icon: {
- width: 36,
- padding: '4px 0px 6px 2px'
- },
- ripple: {
- top: -10,
- left: -10,
- color: state.switched ? toggle.thumbOnColor : baseTheme.palette.textColor
- },
- toggleElement: {
- width: toggleTrackWidth
- },
- track: {
- transition: _transitions2.default.easeOut(),
- width: '100%',
- height: 14,
- borderRadius: 30,
- backgroundColor: toggle.trackOffColor
- },
- thumb: {
- transition: _transitions2.default.easeOut(),
- position: 'absolute',
- top: 1,
- left: 0,
- width: toggleSize,
- height: toggleSize,
- lineHeight: '24px',
- borderRadius: '50%',
- backgroundColor: toggle.thumbOffColor
- },
- trackWhenSwitched: {
- backgroundColor: toggle.trackOnColor
- },
- thumbWhenSwitched: {
- backgroundColor: toggle.thumbOnColor,
- left: '100%'
- },
- trackWhenDisabled: {
- backgroundColor: toggle.trackDisabledColor
- },
- thumbWhenDisabled: {
- backgroundColor: toggle.thumbDisabledColor
- },
- label: {
- color: disabled ? toggle.labelDisabledColor : toggle.labelColor,
- width: 'calc(100% - ' + (toggleTrackWidth + 10) + 'px)'
- }
- };
-
- (0, _simpleAssign2.default)(styles.track, trackStyle, state.switched && styles.trackWhenSwitched, state.switched && trackSwitchedStyle, disabled && styles.trackWhenDisabled);
-
- (0, _simpleAssign2.default)(styles.thumb, thumbStyle, state.switched && styles.thumbWhenSwitched, state.switched && thumbSwitchedStyle, disabled && styles.thumbWhenDisabled);
-
- if (state.switched) {
- styles.thumb.marginLeft = 0 - styles.thumb.width;
- }
-
- (0, _simpleAssign2.default)(styles.icon, iconStyle);
-
- (0, _simpleAssign2.default)(styles.ripple, rippleStyle);
-
- (0, _simpleAssign2.default)(styles.label, labelStyle);
-
- (0, _simpleAssign2.default)(styles.toggleElement, elementStyle);
-
- return styles;
- }
-
- var Toggle = function (_Component) {
- (0, _inherits3.default)(Toggle, _Component);
-
- function Toggle() {
- var _ref;
-
- var _temp, _this, _ret;
-
- (0, _classCallCheck3.default)(this, Toggle);
-
- for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
-
- return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Toggle.__proto__ || (0, _getPrototypeOf2.default)(Toggle)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
- switched: false
- }, _this.handleStateChange = function (newSwitched) {
- _this.setState({
- switched: newSwitched
- });
- }, _this.handleToggle = function (event, isInputChecked) {
- if (_this.props.onToggle) {
- _this.props.onToggle(event, isInputChecked);
- }
- }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
- }
-
- (0, _createClass3.default)(Toggle, [{
- key: 'componentWillMount',
- value: function componentWillMount() {
- var _props = this.props,
- toggled = _props.toggled,
- defaultToggled = _props.defaultToggled,
- valueLink = _props.valueLink;
-
-
- if (toggled || defaultToggled || valueLink && valueLink.value) {
- this.setState({
- switched: true
- });
- }
- }
- }, {
- key: 'isToggled',
- value: function isToggled() {
- return this.refs.enhancedSwitch.isSwitched();
- }
- }, {
- key: 'setToggled',
- value: function setToggled(newToggledValue) {
- this.refs.enhancedSwitch.setSwitched(newToggledValue);
- }
- }, {
- key: 'render',
- value: function render() {
- var _props2 = this.props,
- defaultToggled = _props2.defaultToggled,
- elementStyle = _props2.elementStyle,
- onToggle = _props2.onToggle,
- trackSwitchedStyle = _props2.trackSwitchedStyle,
- thumbSwitchedStyle = _props2.thumbSwitchedStyle,
- toggled = _props2.toggled,
- other = (0, _objectWithoutProperties3.default)(_props2, ['defaultToggled', 'elementStyle', 'onToggle', 'trackSwitchedStyle', 'thumbSwitchedStyle', 'toggled']);
- var prepareStyles = this.context.muiTheme.prepareStyles;
-
- var styles = getStyles(this.props, this.context, this.state);
-
- var toggleElement = _react2.default.createElement(
- 'div',
- { style: prepareStyles((0, _simpleAssign2.default)({}, styles.toggleElement)) },
- _react2.default.createElement('div', { style: prepareStyles((0, _simpleAssign2.default)({}, styles.track)) }),
- _react2.default.createElement(_Paper2.default, { style: styles.thumb, circle: true, zDepth: 1 })
- );
-
- var enhancedSwitchProps = {
- ref: 'enhancedSwitch',
- inputType: 'checkbox',
- switchElement: toggleElement,
- rippleStyle: styles.ripple,
- rippleColor: styles.ripple.color,
- iconStyle: styles.icon,
- trackStyle: styles.track,
- thumbStyle: styles.thumb,
- labelStyle: styles.label,
- switched: this.state.switched,
- onSwitch: this.handleToggle,
- onParentShouldUpdate: this.handleStateChange,
- labelPosition: this.props.labelPosition
- };
-
- if (this.props.hasOwnProperty('toggled')) {
- enhancedSwitchProps.checked = toggled;
- } else if (this.props.hasOwnProperty('defaultToggled')) {
- enhancedSwitchProps.defaultChecked = defaultToggled;
- }
-
- return _react2.default.createElement(_EnhancedSwitch2.default, (0, _extends3.default)({}, other, enhancedSwitchProps));
- }
- }]);
- return Toggle;
- }(_react.Component);
-
- Toggle.defaultProps = {
- defaultToggled: false,
- disabled: false,
- labelPosition: 'left'
- };
- Toggle.contextTypes = {
- muiTheme: _propTypes2.default.object.isRequired
- };
- Toggle.propTypes = process.env.NODE_ENV !== "production" ? {
- /**
- * Determines whether the Toggle is initially turned on.
- * **Warning:** This cannot be used in conjunction with `toggled`.
- * Decide between using a controlled or uncontrolled input element and remove one of these props.
- * More info: https://fb.me/react-controlled-components
- */
- defaultToggled: _propTypes2.default.bool,
- /**
- * Will disable the toggle if true.
- */
- disabled: _propTypes2.default.bool,
- /**
- * Overrides the inline-styles of the Toggle element.
- */
- elementStyle: _propTypes2.default.object,
- /**
- * Overrides the inline-styles of the Icon element.
- */
- iconStyle: _propTypes2.default.object,
- /**
- * Overrides the inline-styles of the input element.
- */
- inputStyle: _propTypes2.default.object,
- /**
- * Label for toggle.
- */
- label: _propTypes2.default.node,
- /**
- * Where the label will be placed next to the toggle.
- */
- labelPosition: _propTypes2.default.oneOf(['left', 'right']),
- /**
- * Overrides the inline-styles of the Toggle element label.
- */
- labelStyle: _propTypes2.default.object,
- /**
- * Callback function that is fired when the toggle switch is toggled.
- *
- * @param {object} event Change event targeting the toggle.
- * @param {bool} isInputChecked The new value of the toggle.
- */
- onToggle: _propTypes2.default.func,
- /**
- * Override style of ripple.
- */
- rippleStyle: _propTypes2.default.object,
- /**
- * Override the inline-styles of the root element.
- */
- style: _propTypes2.default.object,
- /**
- * Override style for thumb.
- */
- thumbStyle: _propTypes2.default.object,
- /**
- * Override the inline styles for thumb when the toggle switch is toggled on.
- */
- thumbSwitchedStyle: _propTypes2.default.object,
- /**
- * Toggled if set to true.
- */
- toggled: _propTypes2.default.bool,
- /**
- * Override style for track.
- */
- trackStyle: _propTypes2.default.object,
- /**
- * Override the inline styles for track when the toggle switch is toggled on.
- */
- trackSwitchedStyle: _propTypes2.default.object,
- /**
- * ValueLink prop for when using controlled toggle.
- */
- valueLink: _propTypes2.default.object
- } : {};
- exports.default = Toggle;
|