forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbox.jsx
149 lines (129 loc) · 3.9 KB
/
checkbox.jsx
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
var React = require('react');
var EnhancedSwitch = require('./enhanced-switch');
var StylePropable = require('./mixins/style-propable');
var Transitions = require('./styles/transitions');
var CheckboxOutline = require('./svg-icons/toggle-check-box-outline-blank');
var CheckboxChecked = require('./svg-icons/toggle-check-box-checked');
var Checkbox = React.createClass({
mixins: [StylePropable],
contextTypes: {
muiTheme: React.PropTypes.object
},
propTypes: {
iconStyle: React.PropTypes.object,
onCheck: React.PropTypes.func
},
getInitialState: function() {
return {
switched:
this.props.checked ||
this.props.defaultChecked ||
(this.props.valueLink && this.props.valueLink.value) ||
false,
}
},
getTheme: function() {
return this.context.muiTheme.component.checkbox;
},
getStyles: function() {
var checkboxSize = 24;
var styles = {
icon: {
height: checkboxSize,
width: checkboxSize,
},
check: {
position: 'absolute',
opacity: 0,
transform: 'scale(0)',
transitionOrigin: '50% 50%',
transition: Transitions.easeOut('450ms', 'opacity', '0ms') + ', ' +
Transitions.easeOut('0ms', 'transform', '450ms'),
fill: this.getTheme().checkedColor
},
box: {
position: 'absolute',
opacity: 1,
fill: this.getTheme().boxColor,
transition: Transitions.easeOut('2s', null, '200ms')
},
checkWhenSwitched: {
opacity: 1,
transform: 'scale(1)',
transition: Transitions.easeOut('0ms', 'opacity', '0ms') + ', ' +
Transitions.easeOut('800ms', 'transform', '0ms')
},
boxWhenSwitched: {
transition: Transitions.easeOut('100ms', null, '0ms'),
fill: this.getTheme().checkedColor
},
checkWhenDisabled: {
fill: this.getTheme().disabledColor
},
boxWhenDisabled: {
fill: this.getTheme().disabledColor
}
};
return styles;
},
render: function() {
var {
onCheck,
...other
} = this.props;
var styles = this.getStyles();
var boxStyles =
this.mergeAndPrefix(
styles.box,
this.state.switched && styles.boxWhenSwitched,
this.props.iconStyle,
this.props.disabled && styles.boxWhenDisabled);
var checkStyles =
this.mergeAndPrefix(
styles.check,
this.state.switched && styles.checkWhenSwitched,
this.props.iconStyle,
this.props.disabled && styles.checkWhenDisabled);
var checkboxElement = (
<div>
<CheckboxOutline style={boxStyles} />
<CheckboxChecked style={checkStyles} />
</div>
);
var rippleColor = this.state.switched ? checkStyles.fill : boxStyles.fill;
var iconStyle = this.mergeAndPrefix(
styles.icon,
this.props.iconStyle
);
var enhancedSwitchProps = {
ref: "enhancedSwitch",
inputType: "checkbox",
switched: this.state.switched,
switchElement: checkboxElement,
rippleColor: rippleColor,
iconStyle: iconStyle,
onSwitch: this._handleCheck,
onParentShouldUpdate: this._handleStateChange,
defaultSwitched: this.props.defaultChecked,
labelPosition: (this.props.labelPosition) ? this.props.labelPosition : "right"
};
return (
<EnhancedSwitch
{...other}
{...enhancedSwitchProps}/>
);
},
isChecked: function() {
return this.refs.enhancedSwitch.isSwitched();
},
setChecked: function(newCheckedValue) {
this.refs.enhancedSwitch.setSwitched(newCheckedValue);
},
_handleCheck: function(e, isInputChecked) {
if (this.props.onCheck) this.props.onCheck(e, isInputChecked);
},
_handleStateChange: function(newSwitched) {
this.setState({switched: newSwitched});
}
});
module.exports = Checkbox;