UNPKG

10.3 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends2 = require('babel-runtime/helpers/extends');
8
9var _extends3 = _interopRequireDefault(_extends2);
10
11var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
12
13var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
14
15var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
16
17var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
18
19var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
20
21var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
22
23var _createClass2 = require('babel-runtime/helpers/createClass');
24
25var _createClass3 = _interopRequireDefault(_createClass2);
26
27var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
28
29var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
30
31var _inherits2 = require('babel-runtime/helpers/inherits');
32
33var _inherits3 = _interopRequireDefault(_inherits2);
34
35var _simpleAssign = require('simple-assign');
36
37var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
38
39var _react = require('react');
40
41var _react2 = _interopRequireDefault(_react);
42
43var _propTypes = require('prop-types');
44
45var _propTypes2 = _interopRequireDefault(_propTypes);
46
47var _transitions = require('../styles/transitions');
48
49var _transitions2 = _interopRequireDefault(_transitions);
50
51var _Paper = require('../Paper');
52
53var _Paper2 = _interopRequireDefault(_Paper);
54
55var _EnhancedSwitch = require('../internal/EnhancedSwitch');
56
57var _EnhancedSwitch2 = _interopRequireDefault(_EnhancedSwitch);
58
59function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
60
61function getStyles(props, context, state) {
62 var disabled = props.disabled,
63 elementStyle = props.elementStyle,
64 trackSwitchedStyle = props.trackSwitchedStyle,
65 thumbSwitchedStyle = props.thumbSwitchedStyle,
66 trackStyle = props.trackStyle,
67 thumbStyle = props.thumbStyle,
68 iconStyle = props.iconStyle,
69 rippleStyle = props.rippleStyle,
70 labelStyle = props.labelStyle;
71 var _context$muiTheme = context.muiTheme,
72 baseTheme = _context$muiTheme.baseTheme,
73 toggle = _context$muiTheme.toggle;
74
75
76 var toggleSize = 20;
77 var toggleTrackWidth = 36;
78 var styles = {
79 icon: {
80 width: 36,
81 padding: '4px 0px 6px 2px'
82 },
83 ripple: {
84 top: -10,
85 left: -10,
86 color: state.switched ? toggle.thumbOnColor : baseTheme.palette.textColor
87 },
88 toggleElement: {
89 width: toggleTrackWidth
90 },
91 track: {
92 transition: _transitions2.default.easeOut(),
93 width: '100%',
94 height: 14,
95 borderRadius: 30,
96 backgroundColor: toggle.trackOffColor
97 },
98 thumb: {
99 transition: _transitions2.default.easeOut(),
100 position: 'absolute',
101 top: 1,
102 left: 0,
103 width: toggleSize,
104 height: toggleSize,
105 lineHeight: '24px',
106 borderRadius: '50%',
107 backgroundColor: toggle.thumbOffColor
108 },
109 trackWhenSwitched: {
110 backgroundColor: toggle.trackOnColor
111 },
112 thumbWhenSwitched: {
113 backgroundColor: toggle.thumbOnColor,
114 left: '100%'
115 },
116 trackWhenDisabled: {
117 backgroundColor: toggle.trackDisabledColor
118 },
119 thumbWhenDisabled: {
120 backgroundColor: toggle.thumbDisabledColor
121 },
122 label: {
123 color: disabled ? toggle.labelDisabledColor : toggle.labelColor,
124 width: 'calc(100% - ' + (toggleTrackWidth + 10) + 'px)'
125 }
126 };
127
128 (0, _simpleAssign2.default)(styles.track, trackStyle, state.switched && styles.trackWhenSwitched, state.switched && trackSwitchedStyle, disabled && styles.trackWhenDisabled);
129
130 (0, _simpleAssign2.default)(styles.thumb, thumbStyle, state.switched && styles.thumbWhenSwitched, state.switched && thumbSwitchedStyle, disabled && styles.thumbWhenDisabled);
131
132 if (state.switched) {
133 styles.thumb.marginLeft = 0 - styles.thumb.width;
134 }
135
136 (0, _simpleAssign2.default)(styles.icon, iconStyle);
137
138 (0, _simpleAssign2.default)(styles.ripple, rippleStyle);
139
140 (0, _simpleAssign2.default)(styles.label, labelStyle);
141
142 (0, _simpleAssign2.default)(styles.toggleElement, elementStyle);
143
144 return styles;
145}
146
147var Toggle = function (_Component) {
148 (0, _inherits3.default)(Toggle, _Component);
149
150 function Toggle() {
151 var _ref;
152
153 var _temp, _this, _ret;
154
155 (0, _classCallCheck3.default)(this, Toggle);
156
157 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
158 args[_key] = arguments[_key];
159 }
160
161 return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = Toggle.__proto__ || (0, _getPrototypeOf2.default)(Toggle)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
162 switched: false
163 }, _this.handleStateChange = function (newSwitched) {
164 _this.setState({
165 switched: newSwitched
166 });
167 }, _this.handleToggle = function (event, isInputChecked) {
168 if (_this.props.onToggle) {
169 _this.props.onToggle(event, isInputChecked);
170 }
171 }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
172 }
173
174 (0, _createClass3.default)(Toggle, [{
175 key: 'componentWillMount',
176 value: function componentWillMount() {
177 var _props = this.props,
178 toggled = _props.toggled,
179 defaultToggled = _props.defaultToggled,
180 valueLink = _props.valueLink;
181
182
183 if (toggled || defaultToggled || valueLink && valueLink.value) {
184 this.setState({
185 switched: true
186 });
187 }
188 }
189 }, {
190 key: 'isToggled',
191 value: function isToggled() {
192 return this.refs.enhancedSwitch.isSwitched();
193 }
194 }, {
195 key: 'setToggled',
196 value: function setToggled(newToggledValue) {
197 this.refs.enhancedSwitch.setSwitched(newToggledValue);
198 }
199 }, {
200 key: 'render',
201 value: function render() {
202 var _props2 = this.props,
203 defaultToggled = _props2.defaultToggled,
204 elementStyle = _props2.elementStyle,
205 onToggle = _props2.onToggle,
206 trackSwitchedStyle = _props2.trackSwitchedStyle,
207 thumbSwitchedStyle = _props2.thumbSwitchedStyle,
208 toggled = _props2.toggled,
209 other = (0, _objectWithoutProperties3.default)(_props2, ['defaultToggled', 'elementStyle', 'onToggle', 'trackSwitchedStyle', 'thumbSwitchedStyle', 'toggled']);
210 var prepareStyles = this.context.muiTheme.prepareStyles;
211
212 var styles = getStyles(this.props, this.context, this.state);
213
214 var toggleElement = _react2.default.createElement(
215 'div',
216 { style: prepareStyles((0, _simpleAssign2.default)({}, styles.toggleElement)) },
217 _react2.default.createElement('div', { style: prepareStyles((0, _simpleAssign2.default)({}, styles.track)) }),
218 _react2.default.createElement(_Paper2.default, { style: styles.thumb, circle: true, zDepth: 1 })
219 );
220
221 var enhancedSwitchProps = {
222 ref: 'enhancedSwitch',
223 inputType: 'checkbox',
224 switchElement: toggleElement,
225 rippleStyle: styles.ripple,
226 rippleColor: styles.ripple.color,
227 iconStyle: styles.icon,
228 trackStyle: styles.track,
229 thumbStyle: styles.thumb,
230 labelStyle: styles.label,
231 switched: this.state.switched,
232 onSwitch: this.handleToggle,
233 onParentShouldUpdate: this.handleStateChange,
234 labelPosition: this.props.labelPosition
235 };
236
237 if (this.props.hasOwnProperty('toggled')) {
238 enhancedSwitchProps.checked = toggled;
239 } else if (this.props.hasOwnProperty('defaultToggled')) {
240 enhancedSwitchProps.defaultChecked = defaultToggled;
241 }
242
243 return _react2.default.createElement(_EnhancedSwitch2.default, (0, _extends3.default)({}, other, enhancedSwitchProps));
244 }
245 }]);
246 return Toggle;
247}(_react.Component);
248
249Toggle.defaultProps = {
250 defaultToggled: false,
251 disabled: false,
252 labelPosition: 'left'
253};
254Toggle.contextTypes = {
255 muiTheme: _propTypes2.default.object.isRequired
256};
257Toggle.propTypes = process.env.NODE_ENV !== "production" ? {
258 /**
259 * Determines whether the Toggle is initially turned on.
260 * **Warning:** This cannot be used in conjunction with `toggled`.
261 * Decide between using a controlled or uncontrolled input element and remove one of these props.
262 * More info: https://fb.me/react-controlled-components
263 */
264 defaultToggled: _propTypes2.default.bool,
265 /**
266 * Will disable the toggle if true.
267 */
268 disabled: _propTypes2.default.bool,
269 /**
270 * Overrides the inline-styles of the Toggle element.
271 */
272 elementStyle: _propTypes2.default.object,
273 /**
274 * Overrides the inline-styles of the Icon element.
275 */
276 iconStyle: _propTypes2.default.object,
277 /**
278 * Overrides the inline-styles of the input element.
279 */
280 inputStyle: _propTypes2.default.object,
281 /**
282 * Label for toggle.
283 */
284 label: _propTypes2.default.node,
285 /**
286 * Where the label will be placed next to the toggle.
287 */
288 labelPosition: _propTypes2.default.oneOf(['left', 'right']),
289 /**
290 * Overrides the inline-styles of the Toggle element label.
291 */
292 labelStyle: _propTypes2.default.object,
293 /**
294 * Callback function that is fired when the toggle switch is toggled.
295 *
296 * @param {object} event Change event targeting the toggle.
297 * @param {bool} isInputChecked The new value of the toggle.
298 */
299 onToggle: _propTypes2.default.func,
300 /**
301 * Override style of ripple.
302 */
303 rippleStyle: _propTypes2.default.object,
304 /**
305 * Override the inline-styles of the root element.
306 */
307 style: _propTypes2.default.object,
308 /**
309 * Override style for thumb.
310 */
311 thumbStyle: _propTypes2.default.object,
312 /**
313 * Override the inline styles for thumb when the toggle switch is toggled on.
314 */
315 thumbSwitchedStyle: _propTypes2.default.object,
316 /**
317 * Toggled if set to true.
318 */
319 toggled: _propTypes2.default.bool,
320 /**
321 * Override style for track.
322 */
323 trackStyle: _propTypes2.default.object,
324 /**
325 * Override the inline styles for track when the toggle switch is toggled on.
326 */
327 trackSwitchedStyle: _propTypes2.default.object,
328 /**
329 * ValueLink prop for when using controlled toggle.
330 */
331 valueLink: _propTypes2.default.object
332} : {};
333exports.default = Toggle;
\No newline at end of file