UNPKG

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