forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbox.js
159 lines (132 loc) · 5.33 KB
/
checkbox.js
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
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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');
var Checkbox = React.createClass({
displayName: 'Checkbox',
mixins: [StylePropable],
contextTypes: {
muiTheme: React.PropTypes.object
},
propTypes: {
checked: React.PropTypes.bool,
checkedIcon: React.PropTypes.element,
defaultChecked: React.PropTypes.bool,
iconStyle: React.PropTypes.object,
labelStyle: React.PropTypes.object,
onCheck: React.PropTypes.func,
unCheckedIcon: React.PropTypes.element
},
getInitialState: function getInitialState() {
return {
switched: this.props.checked || this.props.defaultChecked || this.props.valueLink && this.props.valueLink.value || false
};
},
getTheme: function getTheme() {
return this.context.muiTheme.component.checkbox;
},
getStyles: function getStyles() {
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
},
label: {
color: this.props.disabled ? this.getTheme().labelDisabledColor : this.getTheme().labelColor
}
};
return styles;
},
render: function render() {
var _props = this.props;
var iconStyle = _props.iconStyle;
var onCheck = _props.onCheck;
var checkedIcon = _props.checkedIcon;
var unCheckedIcon = _props.unCheckedIcon;
var other = _objectWithoutProperties(_props, ['iconStyle', 'onCheck', 'checkedIcon', 'unCheckedIcon']);
var styles = this.getStyles();
var boxStyles = this.mergeAndPrefix(styles.box, this.state.switched && styles.boxWhenSwitched, iconStyle, this.props.disabled && styles.boxWhenDisabled);
var checkStyles = this.mergeAndPrefix(styles.check, this.state.switched && styles.checkWhenSwitched, iconStyle, this.props.disabled && styles.checkWhenDisabled);
var checkedElement = checkedIcon ? React.cloneElement(checkedIcon, {
style: this.mergeAndPrefix(checkStyles, checkedIcon.props.style)
}) : React.createElement(CheckboxChecked, {
style: checkStyles
});
var unCheckedElement = unCheckedIcon ? React.cloneElement(unCheckedIcon, {
style: this.mergeAndPrefix(boxStyles, unCheckedIcon.props.style)
}) : React.createElement(CheckboxOutline, {
style: boxStyles
});
var checkboxElement = React.createElement(
'div',
null,
unCheckedElement,
checkedElement
);
var rippleColor = this.state.switched ? checkStyles.fill : boxStyles.fill;
var mergedIconStyle = this.mergeAndPrefix(styles.icon, iconStyle);
var labelStyle = this.mergeAndPrefix(styles.label, this.props.labelStyle);
var enhancedSwitchProps = {
ref: "enhancedSwitch",
inputType: "checkbox",
switched: this.state.switched,
switchElement: checkboxElement,
rippleColor: rippleColor,
iconStyle: mergedIconStyle,
onSwitch: this._handleCheck,
labelStyle: labelStyle,
onParentShouldUpdate: this._handleStateChange,
defaultSwitched: this.props.defaultChecked,
labelPosition: this.props.labelPosition ? this.props.labelPosition : "right"
};
return React.createElement(EnhancedSwitch, _extends({}, other, enhancedSwitchProps));
},
isChecked: function isChecked() {
return this.refs.enhancedSwitch.isSwitched();
},
setChecked: function setChecked(newCheckedValue) {
this.refs.enhancedSwitch.setSwitched(newCheckedValue);
},
_handleCheck: function _handleCheck(e, isInputChecked) {
if (this.props.onCheck) this.props.onCheck(e, isInputChecked);
},
_handleStateChange: function _handleStateChange(newSwitched) {
this.setState({ switched: newSwitched });
}
});
module.exports = Checkbox;