forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnhancedSwitch.js
More file actions
387 lines (346 loc) · 10.3 KB
/
EnhancedSwitch.js
File metadata and controls
387 lines (346 loc) · 10.3 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import React, {Component, PropTypes} from 'react';
import EventListener from 'react-event-listener';
import keycode from 'keycode';
import transitions from '../styles/transitions';
import FocusRipple from './FocusRipple';
import TouchRipple from './TouchRipple';
import Paper from './../Paper';
import warning from 'warning';
function getStyles(props, context) {
const {baseTheme} = context.muiTheme;
return {
root: {
position: 'relative',
cursor: props.disabled ? 'default' : 'pointer',
overflow: 'visible',
display: 'table',
height: 'auto',
width: '100%',
},
input: {
position: 'absolute',
cursor: props.disabled ? 'default' : 'pointer',
pointerEvents: 'all',
opacity: 0,
width: '100%',
height: '100%',
zIndex: 2,
left: 0,
boxSizing: 'border-box',
padding: 0,
margin: 0,
},
controls: {
display: 'flex',
width: '100%',
height: '100%',
},
label: {
float: 'left',
position: 'relative',
display: 'block',
width: 'calc(100% - 60px)',
lineHeight: '24px',
color: baseTheme.palette.textColor,
fontFamily: baseTheme.fontFamily,
},
wrap: {
transition: transitions.easeOut(),
float: 'left',
position: 'relative',
display: 'block',
flexShrink: 0,
width: 60 - baseTheme.spacing.desktopGutterLess,
marginRight: (props.labelPosition === 'right') ?
baseTheme.spacing.desktopGutterLess : 0,
marginLeft: (props.labelPosition === 'left') ?
baseTheme.spacing.desktopGutterLess : 0,
},
ripple: {
color: props.rippleColor || baseTheme.palette.primary1Color,
height: '200%',
width: '200%',
top: -12,
left: -12,
},
};
}
class EnhancedSwitch extends Component {
static propTypes = {
checked: PropTypes.bool,
className: PropTypes.string,
defaultChecked: PropTypes.bool,
disableFocusRipple: PropTypes.bool,
disableTouchRipple: PropTypes.bool,
disabled: PropTypes.bool,
iconStyle: PropTypes.object,
inputStyle: PropTypes.object,
inputType: PropTypes.string.isRequired,
label: PropTypes.node,
labelPosition: PropTypes.oneOf(['left', 'right']),
labelStyle: PropTypes.object,
name: PropTypes.string,
onBlur: PropTypes.func,
onFocus: PropTypes.func,
onMouseDown: PropTypes.func,
onMouseLeave: PropTypes.func,
onMouseUp: PropTypes.func,
onParentShouldUpdate: PropTypes.func,
onSwitch: PropTypes.func,
onTouchEnd: PropTypes.func,
onTouchStart: PropTypes.func,
rippleColor: PropTypes.string,
rippleStyle: PropTypes.object,
style: PropTypes.object,
switchElement: PropTypes.element.isRequired,
switched: PropTypes.bool.isRequired,
thumbStyle: PropTypes.object,
trackStyle: PropTypes.object,
value: PropTypes.any,
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
state = {
isKeyboardFocused: false,
};
componentDidMount() {
const inputNode = this.refs.checkbox;
if ((!this.props.switched || inputNode.checked !== this.props.switched) &&
this.props.onParentShouldUpdate) {
this.props.onParentShouldUpdate(inputNode.checked);
}
}
componentWillReceiveProps(nextProps) {
const hasCheckedProp = nextProps.hasOwnProperty('checked');
const hasToggledProp = nextProps.hasOwnProperty('toggled');
const hasNewDefaultProp =
(nextProps.hasOwnProperty('defaultChecked') &&
(nextProps.defaultChecked !== this.props.defaultChecked));
if (hasCheckedProp || hasToggledProp || hasNewDefaultProp) {
const switched = nextProps.checked || nextProps.toggled || nextProps.defaultChecked || false;
this.setState({
switched: switched,
});
if (this.props.onParentShouldUpdate && switched !== this.props.switched) {
this.props.onParentShouldUpdate(switched);
}
}
}
isSwitched() {
return this.refs.checkbox.checked;
}
// no callback here because there is no event
setSwitched(newSwitchedValue) {
if (!this.props.hasOwnProperty('checked') || this.props.checked === false) {
if (this.props.onParentShouldUpdate) {
this.props.onParentShouldUpdate(newSwitchedValue);
}
this.refs.checkbox.checked = newSwitchedValue;
} else {
warning(false, 'Cannot call set method while checked is defined as a property.');
}
}
getValue() {
return this.refs.checkbox.value;
}
handleChange = (event) => {
this.tabPressed = false;
this.setState({
isKeyboardFocused: false,
});
const isInputChecked = this.refs.checkbox.checked;
if (!this.props.hasOwnProperty('checked') && this.props.onParentShouldUpdate) {
this.props.onParentShouldUpdate(isInputChecked);
}
if (this.props.onSwitch) {
this.props.onSwitch(event, isInputChecked);
}
};
// Checkbox inputs only use SPACE to change their state. Using ENTER will
// update the ui but not the input.
handleKeyDown = (event) => {
const code = keycode(event);
if (code === 'tab') {
this.tabPressed = true;
}
if (this.state.isKeyboardFocused && code === 'space') {
this.handleChange(event);
}
};
handleKeyUp = (event) => {
if (this.state.isKeyboardFocused && keycode(event) === 'space') {
this.handleChange(event);
}
};
/**
* 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.
*/
handleMouseDown = (event) => {
// only listen to left clicks
if (event.button === 0) {
this.refs.touchRipple.start(event);
}
};
handleMouseUp = () => {
this.refs.touchRipple.end();
};
handleMouseLeave = () => {
this.refs.touchRipple.end();
};
handleTouchStart = (event) => {
this.refs.touchRipple.start(event);
};
handleTouchEnd = () => {
this.refs.touchRipple.end();
};
handleBlur = (event) => {
this.setState({
isKeyboardFocused: false,
});
if (this.props.onBlur) {
this.props.onBlur(event);
}
};
handleFocus = (event) => {
// 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(() => {
if (this.tabPressed) {
this.setState({
isKeyboardFocused: true,
});
}
}, 150);
if (this.props.onFocus) {
this.props.onFocus(event);
}
};
render() {
const {
name,
value,
iconStyle,
inputStyle,
inputType,
label,
labelStyle,
labelPosition,
onSwitch, // eslint-disable-line no-unused-vars
onBlur, // eslint-disable-line no-unused-vars
onFocus, // eslint-disable-line no-unused-vars
onMouseUp, // eslint-disable-line no-unused-vars
onMouseDown, // eslint-disable-line no-unused-vars
onMouseLeave, // eslint-disable-line no-unused-vars
onTouchStart, // eslint-disable-line no-unused-vars
onTouchEnd, // eslint-disable-line no-unused-vars
disabled,
disableTouchRipple,
disableFocusRipple,
className,
rippleStyle,
style,
switched, // eslint-disable-line no-unused-vars
switchElement,
thumbStyle,
trackStyle,
...other,
} = this.props;
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context);
const wrapStyles = Object.assign(styles.wrap, iconStyle);
const mergedRippleStyle = Object.assign(styles.ripple, rippleStyle);
if (thumbStyle) {
wrapStyles.marginLeft /= 2;
wrapStyles.marginRight /= 2;
}
const labelElement = label && (
<label style={prepareStyles(Object.assign(styles.label, labelStyle))}>
{label}
</label>
);
const showTouchRipple = !disabled && !disableTouchRipple;
const showFocusRipple = !disabled && !disableFocusRipple;
const touchRipple = (
<TouchRipple
ref="touchRipple"
key="touchRipple"
style={mergedRippleStyle}
color={mergedRippleStyle.color}
muiTheme={this.context.muiTheme}
centerRipple={true}
/>
);
const focusRipple = (
<FocusRipple
key="focusRipple"
innerStyle={mergedRippleStyle}
color={mergedRippleStyle.color}
muiTheme={this.context.muiTheme}
show={this.state.isKeyboardFocused}
/>
);
const ripples = [
showTouchRipple ? touchRipple : null,
showFocusRipple ? focusRipple : null,
];
const inputElement = (
<input
{...other}
ref="checkbox"
type={inputType}
style={prepareStyles(Object.assign(styles.input, inputStyle))}
name={name}
value={value}
disabled={disabled}
onBlur={this.handleBlur}
onFocus={this.handleFocus}
onChange={this.handleChange}
onMouseUp={showTouchRipple && this.handleMouseUp}
onMouseDown={showTouchRipple && this.handleMouseDown}
onMouseLeave={showTouchRipple && this.handleMouseLeave}
onTouchStart={showTouchRipple && this.handleTouchStart}
onTouchEnd={showTouchRipple && this.handleTouchEnd}
/>
);
// If toggle component (indicated by whether the style includes thumb) manually lay out
// elements in order to nest ripple elements
const switchOrThumbElement = !thumbStyle ? (
<div style={prepareStyles(wrapStyles)}>
{switchElement}
{ripples}
</div>
) : (
<div style={prepareStyles(wrapStyles)}>
<div style={prepareStyles(Object.assign({}, trackStyle))} />
<Paper style={thumbStyle} zDepth={1} circle={true}> {ripples} </Paper>
</div>
);
const elementsInOrder = labelPosition === 'right' ? (
<div style={styles.controls}>
{switchOrThumbElement}
{labelElement}
</div>
) : (
<div style={styles.controls}>
{labelElement}
{switchOrThumbElement}
</div>
);
return (
<div ref="root" className={className} style={prepareStyles(Object.assign(styles.root, style))}>
<EventListener
target="window"
onKeyDown={this.handleKeyDown}
onKeyUp={this.handleKeyUp}
/>
{inputElement}
{elementsInOrder}
</div>
);
}
}
export default EnhancedSwitch;