UNPKG

7 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = exports.styles = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
15
16var React = _interopRequireWildcard(require("react"));
17
18var _propTypes = _interopRequireDefault(require("prop-types"));
19
20var _clsx = _interopRequireDefault(require("clsx"));
21
22var _utils = require("@material-ui/utils");
23
24var _SwitchBase = _interopRequireDefault(require("../internal/SwitchBase"));
25
26var _RadioButtonIcon = _interopRequireDefault(require("./RadioButtonIcon"));
27
28var _colorManipulator = require("../styles/colorManipulator");
29
30var _capitalize = _interopRequireDefault(require("../utils/capitalize"));
31
32var _createChainedFunction = _interopRequireDefault(require("../utils/createChainedFunction"));
33
34var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
35
36var _useRadioGroup = _interopRequireDefault(require("../RadioGroup/useRadioGroup"));
37
38var styles = function styles(theme) {
39 return {
40 /* Styles applied to the root element. */
41 root: {
42 color: theme.palette.text.secondary
43 },
44
45 /* Pseudo-class applied to the root element if `checked={true}`. */
46 checked: {},
47
48 /* Pseudo-class applied to the root element if `disabled={true}`. */
49 disabled: {},
50
51 /* Styles applied to the root element if `color="primary"`. */
52 colorPrimary: {
53 '&$checked': {
54 color: theme.palette.primary.main,
55 '&:hover': {
56 backgroundColor: (0, _colorManipulator.alpha)(theme.palette.primary.main, theme.palette.action.hoverOpacity),
57 // Reset on touch devices, it doesn't add specificity
58 '@media (hover: none)': {
59 backgroundColor: 'transparent'
60 }
61 }
62 },
63 '&$disabled': {
64 color: theme.palette.action.disabled
65 }
66 },
67
68 /* Styles applied to the root element if `color="secondary"`. */
69 colorSecondary: {
70 '&$checked': {
71 color: theme.palette.secondary.main,
72 '&:hover': {
73 backgroundColor: (0, _colorManipulator.alpha)(theme.palette.secondary.main, theme.palette.action.hoverOpacity),
74 // Reset on touch devices, it doesn't add specificity
75 '@media (hover: none)': {
76 backgroundColor: 'transparent'
77 }
78 }
79 },
80 '&$disabled': {
81 color: theme.palette.action.disabled
82 }
83 }
84 };
85};
86
87exports.styles = styles;
88var defaultCheckedIcon = /*#__PURE__*/React.createElement(_RadioButtonIcon.default, {
89 checked: true
90});
91var defaultIcon = /*#__PURE__*/React.createElement(_RadioButtonIcon.default, null);
92var Radio = /*#__PURE__*/React.forwardRef(function Radio(props, ref) {
93 var checkedProp = props.checked,
94 classes = props.classes,
95 _props$color = props.color,
96 color = _props$color === void 0 ? 'secondary' : _props$color,
97 nameProp = props.name,
98 onChangeProp = props.onChange,
99 _props$size = props.size,
100 size = _props$size === void 0 ? 'medium' : _props$size,
101 other = (0, _objectWithoutProperties2.default)(props, ["checked", "classes", "color", "name", "onChange", "size"]);
102 var radioGroup = (0, _useRadioGroup.default)();
103 var checked = checkedProp;
104 var onChange = (0, _createChainedFunction.default)(onChangeProp, radioGroup && radioGroup.onChange);
105 var name = nameProp;
106
107 if (radioGroup) {
108 if (typeof checked === 'undefined') {
109 checked = radioGroup.value === props.value;
110 }
111
112 if (typeof name === 'undefined') {
113 name = radioGroup.name;
114 }
115 }
116
117 return /*#__PURE__*/React.createElement(_SwitchBase.default, (0, _extends2.default)({
118 color: color,
119 type: "radio",
120 icon: /*#__PURE__*/React.cloneElement(defaultIcon, {
121 fontSize: size === 'small' ? 'small' : 'medium'
122 }),
123 checkedIcon: /*#__PURE__*/React.cloneElement(defaultCheckedIcon, {
124 fontSize: size === 'small' ? 'small' : 'medium'
125 }),
126 classes: {
127 root: (0, _clsx.default)(classes.root, classes["color".concat((0, _capitalize.default)(color))]),
128 checked: classes.checked,
129 disabled: classes.disabled
130 },
131 name: name,
132 checked: checked,
133 onChange: onChange,
134 ref: ref
135 }, other));
136});
137process.env.NODE_ENV !== "production" ? Radio.propTypes = {
138 // ----------------------------- Warning --------------------------------
139 // | These PropTypes are generated from the TypeScript type definitions |
140 // | To update them edit the d.ts file and run "yarn proptypes" |
141 // ----------------------------------------------------------------------
142
143 /**
144 * If `true`, the component is checked.
145 */
146 checked: _propTypes.default.bool,
147
148 /**
149 * The icon to display when the component is checked.
150 */
151 checkedIcon: _propTypes.default.node,
152
153 /**
154 * Override or extend the styles applied to the component.
155 * See [CSS API](#css) below for more details.
156 */
157 classes: _propTypes.default.object,
158
159 /**
160 * The color of the component. It supports those theme colors that make sense for this component.
161 */
162 color: _propTypes.default.oneOf(['default', 'primary', 'secondary']),
163
164 /**
165 * If `true`, the radio will be disabled.
166 */
167 disabled: _propTypes.default.bool,
168
169 /**
170 * If `true`, the ripple effect will be disabled.
171 */
172 disableRipple: _propTypes.default.bool,
173
174 /**
175 * The icon to display when the component is unchecked.
176 */
177 icon: _propTypes.default.node,
178
179 /**
180 * The id of the `input` element.
181 */
182 id: _propTypes.default.string,
183
184 /**
185 * [Attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#Attributes) applied to the `input` element.
186 */
187 inputProps: _propTypes.default.object,
188
189 /**
190 * Pass a ref to the `input` element.
191 */
192 inputRef: _utils.refType,
193
194 /**
195 * Name attribute of the `input` element.
196 */
197 name: _propTypes.default.string,
198
199 /**
200 * Callback fired when the state is changed.
201 *
202 * @param {object} event The event source of the callback.
203 * You can pull out the new value by accessing `event.target.value` (string).
204 * You can pull out the new checked state by accessing `event.target.checked` (boolean).
205 */
206 onChange: _propTypes.default.func,
207
208 /**
209 * If `true`, the `input` element will be required.
210 */
211 required: _propTypes.default.bool,
212
213 /**
214 * The size of the radio.
215 * `small` is equivalent to the dense radio styling.
216 */
217 size: _propTypes.default.oneOf(['medium', 'small']),
218
219 /**
220 * The value of the component. The DOM API casts this to a string.
221 */
222 value: _propTypes.default.any
223} : void 0;
224
225var _default = (0, _withStyles.default)(styles, {
226 name: 'MuiRadio'
227})(Radio);
228
229exports.default = _default;
\No newline at end of file