1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
|
3 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
4 | import _createClass from 'babel-runtime/helpers/createClass';
|
5 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
6 | import _inherits from 'babel-runtime/helpers/inherits';
|
7 |
|
8 | var _class, _temp;
|
9 |
|
10 | import React from 'react';
|
11 | import PropTypes from 'prop-types';
|
12 | import classnames from 'classnames';
|
13 | import { polyfill } from 'react-lifecycles-compat';
|
14 | import UIState from '../mixin-ui-state';
|
15 | import ConfigProvider from '../config-provider';
|
16 | import withContext from './with-context';
|
17 | import { obj, func } from '../util';
|
18 |
|
19 | var makeChain = func.makeChain,
|
20 | noop = func.noop;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var Radio = (_temp = _class = function (_UIState) {
|
27 | _inherits(Radio, _UIState);
|
28 |
|
29 | function Radio(props) {
|
30 | _classCallCheck(this, Radio);
|
31 |
|
32 | var _this = _possibleConstructorReturn(this, _UIState.call(this, props));
|
33 |
|
34 | var context = props.context;
|
35 |
|
36 | var checked = void 0;
|
37 | if (context.__group__) {
|
38 | checked = context.selectedValue === props.value;
|
39 | } else if ('checked' in props) {
|
40 | checked = props.checked;
|
41 | } else {
|
42 | checked = props.defaultChecked;
|
43 | }
|
44 |
|
45 | _this.state = { checked: checked };
|
46 |
|
47 | _this.onChange = _this.onChange.bind(_this);
|
48 | return _this;
|
49 | }
|
50 |
|
51 | Radio.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps) {
|
52 | var nextContext = nextProps.context;
|
53 |
|
54 |
|
55 | if (nextContext.__group__ && 'selectedValue' in nextContext) {
|
56 | return {
|
57 | checked: nextContext.selectedValue === nextProps.value
|
58 | };
|
59 | } else if ('checked' in nextProps) {
|
60 | return {
|
61 | checked: nextProps.checked
|
62 | };
|
63 | }
|
64 |
|
65 | return null;
|
66 | };
|
67 |
|
68 | Radio.prototype.shouldComponentUpdate = function shouldComponentUpdate(nextProps, nextState, nextContext) {
|
69 | var shallowEqual = obj.shallowEqual;
|
70 |
|
71 | return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState) || !shallowEqual(this.context, nextContext);
|
72 | };
|
73 |
|
74 | Radio.prototype.componentDidUpdate = function componentDidUpdate() {
|
75 |
|
76 | if (this.disabled) {
|
77 |
|
78 | this.resetUIState();
|
79 | }
|
80 | };
|
81 |
|
82 | Radio.prototype.onChange = function onChange(e) {
|
83 | var checked = e.target.checked;
|
84 | var _props = this.props,
|
85 | context = _props.context,
|
86 | value = _props.value;
|
87 |
|
88 |
|
89 | if (context.__group__) {
|
90 | context.onChange(value, e);
|
91 | } else if (this.state.checked !== checked) {
|
92 | if (!('checked' in this.props)) {
|
93 | this.setState({
|
94 | checked: checked
|
95 | });
|
96 | }
|
97 | this.props.onChange(checked, e);
|
98 | }
|
99 | };
|
100 |
|
101 | Radio.prototype.render = function render() {
|
102 | var _classnames, _classnames2, _classnames3;
|
103 |
|
104 |
|
105 | var _props2 = this.props,
|
106 | id = _props2.id,
|
107 | className = _props2.className,
|
108 | children = _props2.children,
|
109 | style = _props2.style,
|
110 | label = _props2.label,
|
111 | onMouseEnter = _props2.onMouseEnter,
|
112 | onMouseLeave = _props2.onMouseLeave,
|
113 | tabIndex = _props2.tabIndex,
|
114 | rtl = _props2.rtl,
|
115 | name = _props2.name,
|
116 | isPreview = _props2.isPreview,
|
117 | renderPreview = _props2.renderPreview,
|
118 | value = _props2.value,
|
119 | context = _props2.context,
|
120 | otherProps = _objectWithoutProperties(_props2, ['id', 'className', 'children', 'style', 'label', 'onMouseEnter', 'onMouseLeave', 'tabIndex', 'rtl', 'name', 'isPreview', 'renderPreview', 'value', 'context']);
|
121 |
|
122 | var checked = !!this.state.checked;
|
123 | var disabled = this.disabled;
|
124 | var isButton = context.isButton;
|
125 | var prefix = context.prefix || this.props.prefix;
|
126 |
|
127 | var others = obj.pickOthers(Radio.propTypes, otherProps);
|
128 | var othersData = obj.pickAttrsWith(others, 'data-');
|
129 |
|
130 | if (isPreview) {
|
131 | var previewCls = classnames(className, prefix + 'form-preview');
|
132 |
|
133 | if ('renderPreview' in this.props) {
|
134 | return React.createElement(
|
135 | 'div',
|
136 | _extends({ id: id, dir: rtl ? 'rtl' : 'ltr' }, others, { className: previewCls }),
|
137 | renderPreview(checked, this.props)
|
138 | );
|
139 | }
|
140 |
|
141 | return React.createElement(
|
142 | 'p',
|
143 | _extends({ id: id, dir: rtl ? 'rtl' : 'ltr' }, others, { className: previewCls }),
|
144 | checked && (children || label || value)
|
145 | );
|
146 | }
|
147 |
|
148 | var input = React.createElement('input', _extends({}, obj.pickOthers(othersData, others), {
|
149 | name: name,
|
150 | id: id,
|
151 | tabIndex: tabIndex,
|
152 | disabled: disabled,
|
153 | checked: checked,
|
154 | type: 'radio',
|
155 | onChange: this.onChange,
|
156 | 'aria-checked': checked,
|
157 | className: prefix + 'radio-input'
|
158 | }));
|
159 |
|
160 |
|
161 | if (!disabled) {
|
162 | input = this.getStateElement(input);
|
163 | }
|
164 |
|
165 | var cls = classnames((_classnames = {}, _classnames[prefix + 'radio'] = true, _classnames.checked = checked, _classnames.disabled = disabled, _classnames[this.getStateClassName()] = true, _classnames));
|
166 | var clsInner = classnames((_classnames2 = {}, _classnames2[prefix + 'radio-inner'] = true, _classnames2.press = checked, _classnames2.unpress = !checked, _classnames2));
|
167 | var clsWrapper = classnames((_classnames3 = {}, _classnames3[prefix + 'radio-wrapper'] = true, _classnames3[className] = !!className, _classnames3.checked = checked, _classnames3.disabled = disabled, _classnames3[this.getStateClassName()] = true, _classnames3));
|
168 | var childrenCls = prefix + 'radio-label';
|
169 |
|
170 | var radioComp = !isButton ? React.createElement(
|
171 | 'span',
|
172 | { className: cls },
|
173 | React.createElement('span', { className: clsInner }),
|
174 | input
|
175 | ) : React.createElement(
|
176 | 'span',
|
177 | { className: prefix + 'radio-single-input' },
|
178 | input
|
179 | );
|
180 |
|
181 | return React.createElement(
|
182 | 'label',
|
183 | _extends({}, othersData, {
|
184 | dir: rtl ? 'rtl' : 'ltr',
|
185 | style: style,
|
186 | 'aria-checked': checked,
|
187 | 'aria-disabled': disabled,
|
188 | className: clsWrapper,
|
189 | onMouseEnter: disabled ? onMouseEnter : makeChain(this._onUIMouseEnter, onMouseEnter),
|
190 | onMouseLeave: disabled ? onMouseLeave : makeChain(this._onUIMouseLeave, onMouseLeave)
|
191 | }),
|
192 | radioComp,
|
193 | [children, label].map(function (d, i) {
|
194 | return d !== undefined ? React.createElement(
|
195 | 'span',
|
196 | { key: i, className: childrenCls },
|
197 | d
|
198 | ) : null;
|
199 | })
|
200 | );
|
201 | };
|
202 |
|
203 | _createClass(Radio, [{
|
204 | key: 'disabled',
|
205 | get: function get() {
|
206 | var props = this.props;
|
207 | var context = props.context;
|
208 |
|
209 |
|
210 | var disabled = props.disabled || context.__group__ && 'disabled' in context && context.disabled;
|
211 |
|
212 | return disabled;
|
213 | }
|
214 | }]);
|
215 |
|
216 | return Radio;
|
217 | }(UIState), _class.displayName = 'Radio', _class.propTypes = _extends({}, ConfigProvider.propTypes, {
|
218 | |
219 |
|
220 |
|
221 | className: PropTypes.string,
|
222 | |
223 |
|
224 |
|
225 | id: PropTypes.string,
|
226 | |
227 |
|
228 |
|
229 | style: PropTypes.object,
|
230 | |
231 |
|
232 |
|
233 | checked: PropTypes.bool,
|
234 | |
235 |
|
236 |
|
237 | defaultChecked: PropTypes.bool,
|
238 | |
239 |
|
240 |
|
241 | label: PropTypes.node,
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 | onChange: PropTypes.func,
|
248 | |
249 |
|
250 |
|
251 |
|
252 | onMouseEnter: PropTypes.func,
|
253 | |
254 |
|
255 |
|
256 |
|
257 | onMouseLeave: PropTypes.func,
|
258 | |
259 |
|
260 |
|
261 | disabled: PropTypes.bool,
|
262 | |
263 |
|
264 |
|
265 | value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
|
266 | |
267 |
|
268 |
|
269 | name: PropTypes.string,
|
270 | |
271 |
|
272 |
|
273 | isPreview: PropTypes.bool,
|
274 | |
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 | renderPreview: PropTypes.func
|
281 | }), _class.defaultProps = {
|
282 | onChange: noop,
|
283 | onMouseLeave: noop,
|
284 | onMouseEnter: noop,
|
285 | tabIndex: 0,
|
286 | prefix: 'next-',
|
287 | isPreview: false
|
288 | }, _class.contextTypes = {
|
289 | onChange: PropTypes.func,
|
290 | __group__: PropTypes.bool,
|
291 | isButton: PropTypes.bool,
|
292 | selectedValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
|
293 | disabled: PropTypes.bool
|
294 | }, _temp);
|
295 |
|
296 |
|
297 | export default ConfigProvider.config(withContext(polyfill(Radio))); |
\ | No newline at end of file |