@txdfe/at
Version:
一个设计体系组件库
231 lines (230 loc) • 10 kB
JavaScript
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import ConfigProvider from '../config-provider';
import { obj } from '../util';
import Radio from './radio';
var pickOthers = obj.pickOthers;
/**
* Radio.Group
* @order 2
*/
var RadioGroup = /*#__PURE__*/function (_Component) {
function RadioGroup(props) {
var _this;
_classCallCheck(this, RadioGroup);
_this = _callSuper(this, RadioGroup, [props]);
_defineProperty(_this, "onChange", function (currentValue, e) {
if (!('value' in _this.props)) {
_this.setState({
value: currentValue
});
}
if (currentValue !== _this.state.value) {
_this.props.onChange(currentValue, e);
}
});
var value = '';
if ('value' in props) {
value = props.value;
} else if ('defaultValue' in props) {
value = props.defaultValue;
}
_this.state = {
value: value
};
return _this;
}
_inherits(RadioGroup, _Component);
return _createClass(RadioGroup, [{
key: "getChildContext",
value: function getChildContext() {
return {
__group__: true,
isButton: this.props.shape === 'button',
onChange: this.onChange,
selectedValue: this.state.value,
disabled: this.props.disabled
};
}
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
var value = nextProps.value;
if ('value' in nextProps) {
if (value === undefined) {
value = '';
}
this.setState({
value: value
});
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props = this.props,
rtl = _this$props.rtl,
className = _this$props.className,
disabled = _this$props.disabled,
shape = _this$props.shape,
size = _this$props.size,
style = _this$props.style,
prefix = _this$props.prefix,
itemDirection = _this$props.itemDirection,
component = _this$props.component;
var others = pickOthers(Object.keys(RadioGroup.propTypes), this.props);
if (rtl) {
others.dir = 'rtl';
}
var children;
if (this.props.children) {
children = React.Children.map(this.props.children, function (child, index) {
if (! /*#__PURE__*/React.isValidElement(child)) {
return child;
}
var checked = _this2.state.value === child.props.value;
var tabIndex = index === 0 && !_this2.state.value || checked ? 0 : -1;
var childrtl = child.props.rtl === undefined ? rtl : child.props.rtl;
if (child.type && child.type.displayName === 'Config(Radio)') {
return /*#__PURE__*/React.cloneElement(child, {
checked: checked,
tabIndex: tabIndex,
rtl: childrtl
});
}
return /*#__PURE__*/React.cloneElement(child, {
checked: checked,
rtl: childrtl
});
});
} else {
children = this.props.dataSource.map(function (item, index) {
var option = item;
if (_typeof(item) !== 'object') {
option = {
label: item,
value: item,
disabled: disabled
};
}
var checked = _this2.state.value === option.value;
return /*#__PURE__*/React.createElement(Radio, {
key: index,
tabIndex: index === 0 && !_this2.state.value || checked ? 0 : -1,
value: option.value,
checked: checked,
label: option.label,
disabled: disabled || option.disabled
});
});
}
var isButtonShape = shape === 'button';
var cls = classnames(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "radio-group"), true), "".concat(prefix, "radio-group-").concat(itemDirection), !isButtonShape), "".concat(prefix, "radio-button"), isButtonShape), "".concat(prefix, "radio-button-").concat(size), isButtonShape), className, !!className), "disabled", disabled));
var TagName = component;
return /*#__PURE__*/React.createElement(TagName, _extends({}, others, {
"aria-disabled": disabled,
role: "radiogroup",
className: cls,
style: style
}), children);
}
}]);
}(Component);
_defineProperty(RadioGroup, "propTypes", {
/**
* 样式类名的品牌前缀
*/
prefix: PropTypes.string,
rtl: PropTypes.bool,
/**
* 自定义类名
*/
className: PropTypes.string,
/**
* 自定义内敛样式
*/
style: PropTypes.object,
/**
* name
*/
name: PropTypes.string,
/**
* radio group的选中项的值
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
/**
* radio group的默认值
*/
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
/**
* 设置标签类型
*/
component: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
/**
* 选中值改变时的事件
* @param {String/Number} value 选中项的值
* @param {Event} e Dom 事件对象
*/
onChange: PropTypes.func,
/**
* 表示radio被禁用
*/
disabled: PropTypes.bool,
/**
* 可以设置成 button 展示形状
* @enumdesc 按钮状
*/
shape: PropTypes.oneOf(['button']),
/**
* 与 `shape` 属性配套使用,shape设为button时有效
* @enumdesc 大, 中, 小
*/
size: PropTypes.oneOf(['large', 'medium', 'small']),
/**
* 可选项列表, 数据项可为 String 或者 Object, 如 `['apple', 'pear', 'orange']`
*/
dataSource: PropTypes.arrayOf(PropTypes.any),
/**
* 通过子元素方式设置内部radio
*/
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.element), PropTypes.element]),
/**
* 子项目的排列方式
* - hoz: 水平排列 (default)
* - ver: 垂直排列
*/
itemDirection: PropTypes.oneOf(['hoz', 'ver'])
});
_defineProperty(RadioGroup, "defaultProps", {
dataSource: [],
size: 'medium',
onChange: function onChange() {},
prefix: 'next-',
component: 'div',
itemDirection: 'hoz'
});
_defineProperty(RadioGroup, "childContextTypes", {
onChange: PropTypes.func,
__group__: PropTypes.bool,
isButton: PropTypes.bool,
selectedValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
disabled: PropTypes.bool
});
export default ConfigProvider.config(RadioGroup);