@txdfe/at
Version:
一个设计体系组件库
285 lines (231 loc) • 10.6 kB
JavaScript
function _extends() { _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; }; return _extends.apply(this, arguments); }
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
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; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _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; }
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) {
_inherits(RadioGroup, _Component);
var _super = _createSuper(RadioGroup);
function RadioGroup(props) {
var _this;
_classCallCheck(this, RadioGroup);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_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;
}
_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,
_classnames;
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((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "radio-group"), true), _defineProperty(_classnames, "".concat(prefix, "radio-group-").concat(itemDirection), !isButtonShape), _defineProperty(_classnames, "".concat(prefix, "radio-button"), isButtonShape), _defineProperty(_classnames, "".concat(prefix, "radio-button-").concat(size), isButtonShape), _defineProperty(_classnames, className, !!className), _defineProperty(_classnames, "disabled", disabled), _classnames));
var TagName = component;
return /*#__PURE__*/React.createElement(TagName, _extends({}, others, {
"aria-disabled": disabled,
role: "radiogroup",
className: cls,
style: style
}), children);
}
}]);
return RadioGroup;
}(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);