UNPKG

@jdcfe/yep-react

Version:

一套移动端的React组件库

102 lines (86 loc) 4.16 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } 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 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import * as React from 'react'; import classNames from 'classnames'; import BaseCheckbox from '../checkbox/BaseCheckbox'; var Radio = /*#__PURE__*/function (_React$PureComponent) { _inherits(Radio, _React$PureComponent); var _super = _createSuper(Radio); function Radio(props) { var _this; _classCallCheck(this, Radio); _this = _super.call(this, props); _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this)); _this.state = { // @ts-ignore value: props.checked || props.options[0].value || props.options[0].label || props.options[0] }; return _this; } _createClass(Radio, [{ key: "handleChange", value: function handleChange(value) { var onChange = this.props.onChange; this.setState({ value: value }); onChange && onChange(value); } }, { key: "render", value: function render() { var _classNames, _this2 = this; var _a = this.props, className = _a.className, style = _a.style, options = _a.options, horizontal = _a.horizontal, onChange = _a.onChange, restProps = __rest(_a, ["className", "style", "options", "horizontal", "onChange"]); var prefixCls = restProps.prefixCls; var wrapCls = classNames("".concat(prefixCls, "-wrapper"), className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-horizontal"), horizontal), _defineProperty(_classNames, "".concat(prefixCls, "-vertical"), !horizontal), _classNames)); return /*#__PURE__*/React.createElement("div", { className: wrapCls, style: style }, options.map(function (option, index) { return /*#__PURE__*/React.createElement(BaseCheckbox, _extends({ key: index, type: "radio", label: option.label || option, disabled: option.disabled, checked: _this2.state.value === (option.value || option.label || option), onChange: function onChange() { return _this2.handleChange(option.value || option.label || option); }, value: option.value || option.label || option }, restProps)); })); } }]); return Radio; }(React.PureComponent); export { Radio as default }; Radio.defaultProps = { prefixCls: 'Yep-radio', options: [], horizontal: false };