Repository navigation
Expand file tree
/
Copy pathSwitch.web.js
More file actions
139 lines (123 loc) · 3.56 KB
/
Copy pathSwitch.web.js
File metadata and controls
139 lines (123 loc) · 3.56 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
/**
* Copyright (c) 2015-present, Alibaba Group Holding Limited.
* All rights reserved.
*
* @providesModule ReactSwitch
*/
'use strict';
import React, { Component, PropTypes } from 'react';
import { Mixin as NativeMethodsMixin } from 'NativeMethodsMixin';
import StyleSheet from 'ReactStyleSheet';
import mixin from 'react-mixin';
import autobind from 'autobind-decorator';
class Switch extends Component {
static propTypes = {
value: PropTypes.bool,
disabled: PropTypes.bool,
onValueChange: PropTypes.func,
onTintColor: PropTypes.string,
thumbTintColor: PropTypes.string,
tintColor: PropTypes.string
}
static defaultProps = {
onTintColor: '#00e158',
thumbTintColor: '#fff',
tintColor: '#fff'
}
state = {
value: this.props.value,
disabled: this.props.disabled
}
componentWillReceiveProps(nextProps) {
this.setState({
value: nextProps.value,
disabled: nextProps.disabled
});
}
getStyles() {
return StyleSheet.create({
span: {
position: 'relative',
display: 'inline-block',
margin: 2,
height: 30,
width: 50,
cursor: 'default', // pointer will cause a grey background color on chrome
verticalAlign: 'middle',
borderRadius: 20,
borderColor: '#dfdfdf',
borderWidth: 1,
borderStyle: 'solid',
WebkitUserSelect: 'none',
WebkitBoxSizing: 'content-box',
WebkitBackfaceVisibility: 'hidden'
},
checkedSpan: {
borderColor: this.props.onTintColor,
backgroundColor: this.props.onTintColor,
boxShadow: this.props.onTintColor + ' 0 0 0 16px inset',
WebkitTransition: 'border 0.2s, box-shadow 0.2s, background-color 1s'
},
uncheckedSpan: {
borderColor: '#dfdfdf',
backgroundColor: this.props.tintColor,
boxShadow: '#dfdfdf 0 0 0 0 inset',
WebkitTransition: 'border 0.2s, box-shadow 0.2s'
},
disabledSpan: {
opacity: 0.5,
cursor: 'not-allowed',
boxShadow: 'none'
},
small: {
position: 'absolute',
top: 0,
width: 30,
height: 30,
backgroundColor: this.props.thumbTintColor,
borderRadius: '100%',
boxShadow: '0 1px 3px rgba(0,0,0,0.4)',
WebkitTransition: '-webkit-transform 0.2s ease-in'
},
checkedSmall: {
WebkitTransform: 'translateX(20px)'
},
uncheckedSmall: {
WebkitTransform: 'translateX(0)'
}
});
}
handleClick(e) {
if (this.state.disabled) {
return null;
}
var newVal = !this.state.value;
this.props.onValueChange && this.props.onValueChange.call(this, newVal);
this.setState({
value: newVal
});
var oldValue = this.props.value;
setTimeout(function() {
if (this.props.value == oldValue) {
this.setState({
value: this.props.value
});
}
}.bind(this), 200);
}
render() {
var styles = this.getStyles();
var spancss = this.state.value ? {...styles.span, ...styles.checkedSpan} : {...styles.span, ...styles.uncheckedSpan};
var smallcss = this.state.value ? {...styles.small, ...styles.checkedSmall} : {...styles.small, ...styles.uncheckedSmall};
spancss = this.state.disabled ? {...spancss, ...styles.disabledSpan} : spancss;
return (
<span onClick={this.handleClick} style={spancss}>
<small style={smallcss}/>
</span>
);
}
};
mixin.onClass(Switch, NativeMethodsMixin);
autobind(Switch);
Switch.isReactNativeComponent = true;
export default Switch;