forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadioButton.js
More file actions
208 lines (189 loc) · 5.48 KB
/
RadioButton.js
File metadata and controls
208 lines (189 loc) · 5.48 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
import React, {Component, PropTypes} from 'react';
import transitions from '../styles/transitions';
import EnhancedSwitch from '../internal/EnhancedSwitch';
import RadioButtonOff from '../svg-icons/toggle/radio-button-unchecked';
import RadioButtonOn from '../svg-icons/toggle/radio-button-checked';
function getStyles(props, context) {
const {radioButton} = context.muiTheme;
return {
icon: {
height: radioButton.size,
width: radioButton.size,
},
target: {
transition: transitions.easeOut(),
position: 'absolute',
opacity: 1,
transform: 'scale(1)',
fill: radioButton.borderColor,
},
fill: {
position: 'absolute',
opacity: 1,
transform: 'scale(0)',
transformOrigin: '50% 50%',
transition: transitions.easeOut(),
fill: radioButton.checkedColor,
},
targetWhenChecked: {
opacity: 0,
transform: 'scale(0)',
},
fillWhenChecked: {
opacity: 1,
transform: 'scale(1)',
},
targetWhenDisabled: {
fill: radioButton.disabledColor,
},
fillWhenDisabled: {
fill: radioButton.disabledColor,
},
label: {
color: props.disabled ? radioButton.labelDisabledColor : radioButton.labelColor,
},
ripple: {
color: props.checked ? radioButton.checkedColor : radioButton.borderColor,
},
};
}
class RadioButton extends Component {
static propTypes = {
/**
* @ignore
* checked if true
* Used internally by `RadioButtonGroup`.
*/
checked: PropTypes.bool,
/**
* The icon element to show when the radio button is checked.
*/
checkedIcon: PropTypes.element,
/**
* If true, the radio button is disabled.
*/
disabled: PropTypes.bool,
/**
* Override the inline-styles of the icon element.
*/
iconStyle: PropTypes.object,
/**
* Override the inline-styles of the input element.
*/
inputStyle: PropTypes.object,
/**
* @ignore
* Used internally by `RadioButtonGroup`. Use the `labelPosition` property of `RadioButtonGroup` instead.
* Where the label will be placed next to the radio button.
*/
labelPosition: PropTypes.oneOf(['left', 'right']),
/**
* Override the inline-styles of the label element.
*/
labelStyle: PropTypes.object,
/**
* @ignore
* Callback function fired when the radio button is checked. Note that this
* function will not be called if the radio button is part of a
* radio button group: in this case, use the `onChange` property of
* `RadioButtonGroup`.
*
* @param {object} event `change` event targeting the element.
* @param {string} value The element's `value`.
*/
onCheck: PropTypes.func,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* The icon element to show when the radio button is unchecked.
*/
uncheckedIcon: PropTypes.element,
/**
* The value of the radio button.
*/
value: PropTypes.any,
};
static defaultProps = {
checked: false,
disabled: false,
labelPosition: 'right',
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
handleStateChange = () => {
};
// Only called when selected, not when unselected.
handleSwitch = (event) => {
if (this.props.onCheck) this.props.onCheck(event, this.props.value);
};
isChecked() {
return this.refs.enhancedSwitch.isSwitched();
}
// Use RadioButtonGroup.setSelectedValue(newSelectionValue) to set a
// RadioButton's checked value.
setChecked(newCheckedValue) {
this.refs.enhancedSwitch.setSwitched(newCheckedValue);
}
getValue() {
return this.refs.enhancedSwitch.getValue();
}
render() {
const {
checkedIcon,
checked,
iconStyle,
labelStyle,
labelPosition,
onCheck, // eslint-disable-line no-unused-vars
uncheckedIcon,
disabled,
...other,
} = this.props;
const styles = getStyles(this.props, this.context);
const uncheckedStyles = Object.assign(
styles.target,
checked && styles.targetWhenChecked,
iconStyle,
disabled && styles.targetWhenDisabled
);
const checkedStyles = Object.assign(
styles.fill,
checked && styles.fillWhenChecked,
iconStyle,
disabled && styles.fillWhenDisabled
);
const uncheckedElement = React.isValidElement(uncheckedIcon) ?
React.cloneElement(uncheckedIcon, {
style: Object.assign(uncheckedStyles, uncheckedIcon.props.style),
}) :
<RadioButtonOff style={uncheckedStyles} />;
const checkedElement = React.isValidElement(checkedIcon) ?
React.cloneElement(checkedIcon, {
style: Object.assign(checkedStyles, checkedIcon.props.style),
}) :
<RadioButtonOn style={checkedStyles} />;
const mergedIconStyle = Object.assign(styles.icon, iconStyle);
const mergedLabelStyle = Object.assign(styles.label, labelStyle);
return (
<EnhancedSwitch
{...other}
ref="enhancedSwitch"
inputType="radio"
checked={checked}
switched={checked}
disabled={disabled}
rippleColor={styles.ripple.color}
iconStyle={mergedIconStyle}
labelStyle={mergedLabelStyle}
labelPosition={labelPosition}
onParentShouldUpdate={this.handleStateChange}
onSwitch={this.handleSwitch}
switchElement={<div>{uncheckedElement}{checkedElement}</div>}
/>
);
}
}
export default RadioButton;