-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Expand file tree
/
Copy pathenhanced-switch.jsx
More file actions
351 lines (302 loc) · 9.85 KB
/
enhanced-switch.jsx
File metadata and controls
351 lines (302 loc) · 9.85 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
var React = require('react');
var KeyCode = require('./utils/key-code');
var DomIdable = require('./mixins/dom-idable');
var StylePropable = require('./mixins/style-propable');
var Transitions = require('./styles/mixins/transitions');
var WindowListenable = require('./mixins/window-listenable');
var CustomVariables = require('./styles/variables/custom-variables');
var FocusRipple = require('./ripples/focus-ripple');
var TouchRipple = require('./ripples/touch-ripple');
var Paper = require('./paper');
var Theme = require('./styles/theme').get();
var EnhancedSwitch = React.createClass({
mixins: [DomIdable, WindowListenable, StylePropable],
propTypes: {
id: React.PropTypes.string,
inputType: React.PropTypes.string.isRequired,
switchElement: React.PropTypes.element.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,
onSwitch: React.PropTypes.func,
required: React.PropTypes.bool,
disabled: React.PropTypes.bool,
defaultSwitched: React.PropTypes.bool,
labelPosition: React.PropTypes.oneOf(['left', 'right']),
disableFocusRipple: React.PropTypes.bool,
disableTouchRipple: React.PropTypes.bool,
},
windowListeners: {
'keydown': '_handleWindowKeydown',
'keyup': '_handleWindowKeyup'
},
getInitialState: function() {
return {
isKeyboardFocused: false
}
},
getEvenWidth: function(){
return (
parseInt(window
.getComputedStyle(this.getDOMNode())
.getPropertyValue('width'), 10)
);
},
componentDidMount: function() {
var inputNode = this.refs.checkbox.getDOMNode();
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) {
var hasCheckedLinkProp = nextProps.hasOwnProperty('checkedLink');
var hasCheckedProp = nextProps.hasOwnProperty('checked');
var hasToggledProp = nextProps.hasOwnProperty('toggled');
var hasNewDefaultProp =
(nextProps.hasOwnProperty('defaultSwitched') &&
(nextProps.defaultSwitched != this.props.defaultSwitched));
var newState = {};
if (hasCheckedProp) {
newState.switched = nextProps.checked;
} else if (hasToggledProp) {
newState.switched = nextProps.toggled;
} else if (hasCheckedLinkProp) {
newState.switched = nextProps.checkedLink.value;
}
if (newState.switched != undefined && (newState.switched != this.props.switched)) this.props.onParentShouldUpdate(newState.switched);
},
render: function() {
var {
type,
name,
value,
label,
onSwitch,
defaultSwitched,
onBlur,
onFocus,
onMouseUp,
onMouseDown,
onMouseOut,
onTouchStart,
onTouchEnd,
disableTouchRipple,
disableFocusRipple,
...other
} = this.props;
var switchWidth = 60 - CustomVariables.spacing.desktopGutterLess;
var labelWidth = this.state.parentWidth - switchWidth - 30;
var styles = this.mergeStyles({
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.mergeStyles({
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 ? (
<label style={labelStyles} htmlFor={inputId}>
{this.props.label}
</label>
) : null;
var inputProps = {
ref: "checkbox",
type: this.props.inputType,
name: this.props.name,
value: this.props.value,
defaultChecked: this.props.defaultSwitched,
onBlur: this._handleBlur,
onFocus: this._handleFocus,
onMouseUp: this._handleMouseUp,
onMouseDown: this._handleMouseDown,
onMouseOut: this._handleMouseOut,
onTouchStart: this._handleTouchStart,
onTouchEnd: this._handleTouchEnd
};
if (!this.props.hasOwnProperty('checkedLink')) {
inputProps.onChange = this._handleChange;
}
var inputElement = (
<input
{...other}
{...inputProps}
style={inputStyles}/>
);
var rippleStyle = this.mergeStyles({
height: '200%',
width: '200%',
top: '-12',
left: '-12'
}, this.props.rippleStyle);
var touchRipple = (
<TouchRipple
ref="touchRipple"
key="touchRipple"
style={rippleStyle}
color={this.props.switched ? Theme.primary1Color : Theme.textColor}
centerRipple={true} />
);
var focusRipple = (
<FocusRipple
key="focusRipple"
innerStyle={rippleStyle}
color={this.props.switched ? Theme.primary1Color : Theme.textColor}
show={this.state.isKeyboardFocused} />
);
var ripples = [
this.props.disabled || disableTouchRipple ? null : touchRipple,
this.props.disabled || disableFocusRipple ? null : focusRipple
];
// If toggle component (indicated by whether the style includes thumb) manually lay out
// elements in order to nest ripple elements
var switchElement = !this.props.thumbStyle ? (
<div style={wrapStyles}>
{this.props.switchElement}
{ripples}
</div>
) : (
<div style={wrapStyles}>
<div style={this.props.trackStyle}/>
<Paper style={this.props.thumbStyle} zDepth={1} circle={true}> {ripples} </Paper>
</div>
);
var labelPositionExist = this.props.labelPosition;
// Position is left if not defined or invalid.
var elementsInOrder = (labelPositionExist &&
(this.props.labelPosition.toUpperCase() === "RIGHT")) ? (
<div>
{switchElement}
{labelElement}
</div>
) : (
<div>
{labelElement}
{switchElement}
</div>
);
return (
<div style={styles}>
{inputElement}
{elementsInOrder}
</div>
);
},
isSwitched: function() {
return this.refs.checkbox.getDOMNode().checked;
},
// no callback here because there is no event
setSwitched: function(newSwitchedValue) {
if (!this.props.hasOwnProperty('checked') || this.props.checked == false) {
this.props.onParentShouldUpdate(newSwitchedValue);
this.refs.checkbox.getDOMNode().checked = newSwitchedValue;
} else if (process.NODE_ENV !== 'production') {
var message = 'Cannot call set method while checked is defined as a property.';
console.error(message);
}
},
getValue: function() {
return this.refs.checkbox.getDOMNode().value;
},
isKeyboardFocused: function() {
return this.state.isKeyboardFocused;
},
_handleChange: function(e) {
this._tabPressed = false;
this.setState({
isKeyboardFocused: false
});
var isInputChecked = this.refs.checkbox.getDOMNode().checked;
if (!this.props.hasOwnProperty('checked')) this.props.onParentShouldUpdate(isInputChecked);
if (this.props.onSwitch) this.props.onSwitch(e, isInputChecked);
},
/**
* Because both the ripples and the checkbox input cannot share pointer
* events, the checkbox input takes control of pointer events and calls
* ripple animations manually.
*/
// Checkbox inputs only use SPACE to change their state. Using ENTER will
// update the ui but not the input.
_handleWindowKeydown: function(e) {
if (e.keyCode == KeyCode.TAB) this._tabPressed = true;
if (e.keyCode == KeyCode.SPACE && this.state.isKeyboardFocused) {
this._handleChange(e);
}
},
_handleWindowKeyup: function(e) {
if (e.keyCode == KeyCode.SPACE && this.state.isKeyboardFocused) {
this._handleChange(e);
}
},
_handleMouseDown: function(e) {
//only listen to left clicks
if (e.button === 0) this.refs.touchRipple.start(e);
},
_handleMouseUp: function(e) {
this.refs.touchRipple.end();
},
_handleMouseOut: function(e) {
this.refs.touchRipple.end();
},
_handleTouchStart: function(e) {
this.refs.touchRipple.start(e);
},
_handleTouchEnd: function(e) {
this.refs.touchRipple.end();
},
_handleBlur: function(e) {
this.setState({
isKeyboardFocused: false
});
if (this.props.onBlur) this.props.onBlur(e);
},
_handleFocus: function(e) {
//setTimeout is needed becuase the focus event fires first
//Wait so that we can capture if this was a keyboard focus
//or touch focus
setTimeout(function() {
if (this._tabPressed) {
this.setState({
isKeyboardFocused: true
});
}
}.bind(this), 150);
if (this.props.onFocus) this.props.onFocus(e);
}
});
module.exports = EnhancedSwitch;