antd
Version:
An enterprise-class UI design language and React-based implementation
98 lines (86 loc) • 3.81 kB
JavaScript
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var __rest = this && this.__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]];
}return t;
};
import * as React from 'react';
import * as PropTypes from 'prop-types';
import RcCheckbox from 'rc-checkbox';
import classNames from 'classnames';
import shallowEqual from 'shallowequal';
var Radio = function (_React$Component) {
_inherits(Radio, _React$Component);
function Radio() {
_classCallCheck(this, Radio);
var _this = _possibleConstructorReturn(this, (Radio.__proto__ || Object.getPrototypeOf(Radio)).apply(this, arguments));
_this.saveCheckbox = function (node) {
_this.rcCheckbox = node;
};
return _this;
}
_createClass(Radio, [{
key: 'shouldComponentUpdate',
value: function shouldComponentUpdate(nextProps, nextState, nextContext) {
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState) || !shallowEqual(this.context.radioGroup, nextContext.radioGroup);
}
}, {
key: 'focus',
value: function focus() {
this.rcCheckbox.focus();
}
}, {
key: 'blur',
value: function blur() {
this.rcCheckbox.blur();
}
}, {
key: 'render',
value: function render() {
var _classNames;
var props = this.props,
context = this.context;
var prefixCls = props.prefixCls,
className = props.className,
children = props.children,
style = props.style,
restProps = __rest(props, ["prefixCls", "className", "children", "style"]);
var radioGroup = context.radioGroup;
var radioProps = _extends({}, restProps);
if (radioGroup) {
radioProps.name = radioGroup.name;
radioProps.onChange = radioGroup.onChange;
radioProps.checked = props.value === radioGroup.value;
radioProps.disabled = props.disabled || radioGroup.disabled;
}
var wrapperClassString = classNames(className, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-wrapper', true), _defineProperty(_classNames, prefixCls + '-wrapper-checked', radioProps.checked), _defineProperty(_classNames, prefixCls + '-wrapper-disabled', radioProps.disabled), _classNames));
return React.createElement(
'label',
{ className: wrapperClassString, style: style, onMouseEnter: props.onMouseEnter, onMouseLeave: props.onMouseLeave },
React.createElement(RcCheckbox, _extends({}, radioProps, { prefixCls: prefixCls, ref: this.saveCheckbox })),
children !== undefined ? React.createElement(
'span',
null,
children
) : null
);
}
}]);
return Radio;
}(React.Component);
export default Radio;
Radio.defaultProps = {
prefixCls: 'ant-radio',
type: 'radio'
};
Radio.contextTypes = {
radioGroup: PropTypes.any
};