UNPKG

7.52 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; };
8
9var _classnames = require('classnames');
10
11var _classnames2 = _interopRequireDefault(_classnames);
12
13var _react = require('react');
14
15var _react2 = _interopRequireDefault(_react);
16
17var _propTypes = require('prop-types');
18
19var _propTypes2 = _interopRequireDefault(_propTypes);
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
22
23function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
24
25function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
26
27function _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; }
28
29function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
30
31function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
32
33function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
34
35var propTypes = {
36 /**
37 * radio 颜色 样式
38 */
39 colors: _propTypes2["default"].oneOf(['', 'dark', 'success', 'info', 'warning', 'danger', 'primary']),
40 /**
41 * radio 大小
42 */
43 size: _propTypes2["default"].oneOf(['lg', 'sm']),
44 /**
45 * radio 是否可用
46 */
47 disabled: _propTypes2["default"].bool,
48 /**
49 * radio 样式 是否使用红色填充
50 */
51 inverse: _propTypes2["default"].bool,
52 checked: _propTypes2["default"].bool,
53 onChange: _propTypes2["default"].func
54};
55
56var defaultProps = {
57 inverse: false,
58 disabled: false,
59 clsPrefix: 'u-radio'
60};
61
62/**
63 * 建立与RadioGroup通信
64 */
65var contextTypes = {
66 radioGroup: _propTypes2["default"].object
67};
68
69var Radio = function (_React$Component) {
70 _inherits(Radio, _React$Component);
71
72 function Radio(props, context) {
73 _classCallCheck(this, Radio);
74
75 var _this = _possibleConstructorReturn(this, _React$Component.call(this, props, context));
76
77 _this.handleClick = function (event) {
78 if (_this.props.disabled) {
79 return;
80 }
81 if (_this.context.radioGroup && _this.context.radioGroup.onChange) {
82 _this.context.radioGroup.onChange(_this.props.value);
83 } else {
84 if (!('checked' in _this.props)) {
85 _this.setState({
86 checked: !_this.state.checked
87 });
88 }
89 event.target.checked = !_this.state.checked;
90 _this.props.onChange && _this.props.onChange(event, !_this.state.checked);
91 }
92 };
93
94 _this.handleFocus = function (e) {
95 if (e.target && e.target.type == 'radio') {
96 _this.setState({
97 focused: true
98 });
99 }
100 };
101
102 _this.handleBlur = function (e) {
103 if (e.target && e.target.type == 'radio') {
104 _this.setState({
105 focused: false
106 });
107 }
108 };
109
110 var initChecked = typeof props.checked !== 'undefined' ? props.checked : props.defaultChecked;
111 _this.state = {
112 checked: initChecked,
113 focused: false
114 };
115 return _this;
116 }
117
118 Radio.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
119 if ('checked' in nextProps) {
120 this.setState({
121 checked: nextProps.checked
122 });
123 }
124 };
125
126 Radio.prototype.render = function render() {
127 var _classes;
128
129 var state = this.state,
130 props = this.props,
131 context = this.context;
132 var checked = state.checked;
133 /**
134 * 自身的属性
135 */
136
137 var inverse = props.inverse,
138 disabled = props.disabled,
139 colors = props.colors,
140 className = props.className,
141 children = props.children,
142 clsPrefix = props.clsPrefix,
143 style = props.style,
144 onChange = props.onChange,
145 others = _objectWithoutProperties(props, ['inverse', 'disabled', 'colors', 'className', 'children', 'clsPrefix', 'style', 'onChange']);
146
147 var radioGroup = context.radioGroup;
148
149 var radioProps = _extends({}, others);
150 // 包裹 radioGroup
151 if (radioGroup) {
152 radioProps.name = radioGroup.name;
153 radioProps.selectedValue = radioGroup.selectedValue;
154 radioProps.size = radioGroup.size;
155 radioProps.focusvalue = radioGroup.focusvalue;
156 }
157 var name = radioProps.name,
158 selectedValue = radioProps.selectedValue,
159 size = radioProps.size,
160 focusvalue = radioProps.focusvalue;
161
162
163 var optional = {};
164 /**
165 * 若父级selectedValue与本身的value值相同,则改radio被选中
166 */
167 if (selectedValue !== undefined) {
168 optional.checked = this.props.value === selectedValue;
169 }
170
171 var classes = (_classes = {}, _defineProperty(_classes, clsPrefix + '-focused', this.state.focused), _defineProperty(_classes, 'is-checked', typeof optional.checked !== 'undefined' ? optional.checked : checked), _defineProperty(_classes, 'disabled', disabled), _classes);
172
173 if (colors) {
174 classes[clsPrefix + '-' + colors] = true;
175 }
176 if (size) {
177 classes[clsPrefix + '-' + size] = true;
178 }
179 if (inverse) {
180 classes[clsPrefix + '-inverse'] = true;
181 }
182 if (children == null) {
183 classes[clsPrefix + '-noContent'] = true;
184 }
185 var classNames = (0, _classnames2["default"])(clsPrefix, classes);
186 var tabIndex = optional.checked ? 0 : -1;
187 if (focusvalue && focusvalue == this.props.value) {
188 tabIndex = 0;
189 }
190 var input = _react2["default"].createElement('input', _extends({}, radioProps, {
191 type: 'radio',
192 name: name,
193 disabled: this.props.disabled,
194 tabIndex: tabIndex,
195 onFocus: this.handleFocus,
196 onBlur: this.handleBlur
197 }));
198 return _react2["default"].createElement(
199 'label',
200 { style: style, onClick: this.handleClick, className: (0, _classnames2["default"])(className, classNames) },
201 input,
202 _react2["default"].createElement(
203 'label',
204 { className: clsPrefix + '-label' },
205 children
206 )
207 );
208 };
209
210 return Radio;
211}(_react2["default"].Component);
212
213Radio.contextTypes = contextTypes;
214Radio.propTypes = propTypes;
215Radio.defaultProps = defaultProps;
216
217exports["default"] = Radio;
218module.exports = exports['default'];
\No newline at end of file