@txdfe/at
Version:
一个设计体系组件库
221 lines (219 loc) • 10.6 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 _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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 Checkbox from './checkbox';
var pickOthers = obj.pickOthers;
/** Checkbox.Group */
var CheckboxGroup = /*#__PURE__*/function (_Component) {
function CheckboxGroup(props) {
var _this;
_classCallCheck(this, CheckboxGroup);
_this = _callSuper(this, CheckboxGroup, [props]);
_defineProperty(_this, "onChange", function (currentValue, e) {
var value = _this.state.value;
var index = value.indexOf(currentValue);
var valTemp = _toConsumableArray(value);
if (index === -1) {
valTemp.push(currentValue);
} else {
valTemp.splice(index, 1);
}
if (!('value' in _this.props)) {
_this.setState({
value: valTemp
});
}
_this.props.onChange(valTemp, e);
});
var _value = [];
if ('value' in props) {
_value = props.value;
} else if ('defaultValue' in props) {
_value = props.defaultValue;
}
if (!Array.isArray(_value)) {
if (_value === null || _value === undefined) {
_value = [];
} else {
_value = [_value];
}
}
_this.state = {
value: _toConsumableArray(_value)
};
return _this;
}
_inherits(CheckboxGroup, _Component);
return _createClass(CheckboxGroup, [{
key: "getChildContext",
value: function getChildContext() {
return {
__group__: true,
onChange: this.onChange,
selectedValue: this.state.value,
disabled: this.props.disabled
};
}
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
var value = nextProps.value;
if (!Array.isArray(value)) {
if (value === null || value === undefined) {
value = [];
} else {
value = [value];
}
}
this.setState({
value: value
});
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props = this.props,
className = _this$props.className,
style = _this$props.style,
prefix = _this$props.prefix,
size = _this$props.size,
disabled = _this$props.disabled,
itemDirection = _this$props.itemDirection,
rtl = _this$props.rtl;
var others = pickOthers(CheckboxGroup.propTypes, this.props);
var _size = size;
if (_size !== 'large' && _size !== 'medium') {
_size = 'medium';
}
// 如果内嵌标签跟dataSource同时存在,以内嵌标签为主
var children;
if (this.props.children) {
children = React.Children.map(this.props.children, function (child) {
if (! /*#__PURE__*/React.isValidElement(child)) {
return child;
}
return /*#__PURE__*/React.cloneElement(child, child.props.rtl === undefined ? {
rtl: rtl
} : null);
});
} 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 && _this2.state.value.indexOf(option.value) > -1;
return /*#__PURE__*/React.createElement(Checkbox, {
size: _size,
key: index,
value: option.value,
checked: checked,
rtl: rtl,
disabled: disabled || option.disabled,
label: option.label
});
});
}
var cls = classnames(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "checkbox-group"), true), "".concat(prefix, "checkbox-group-").concat(_size), true), "".concat(prefix, "checkbox-group-").concat(itemDirection), true), className, !!className), "disabled", disabled));
return /*#__PURE__*/React.createElement("span", _extends({
dir: rtl ? 'rtl' : undefined
}, others, {
className: cls,
style: style
}), children);
}
}]);
}(Component);
_defineProperty(CheckboxGroup, "propTypes", {
prefix: PropTypes.string,
rtl: PropTypes.bool,
/**
* 自定义类名
*/
className: PropTypes.string,
/**
* 自定义内敛样式
*/
style: PropTypes.object,
/**
* 整体禁用
*/
disabled: PropTypes.bool,
/**
* 可选项列表, 数据项可为 String 或者 Object, 如 `['apple', 'pear', 'orange']` 或者 `[{value: 'apple', label: '苹果',}, {value: 'pear', label: '梨'}, {value: 'orange', label: '橙子'}]`
*/
dataSource: PropTypes.arrayOf(PropTypes.any),
/**
* 被选中的值列表
*/
value: PropTypes.oneOfType([PropTypes.array, PropTypes.string, PropTypes.number]),
/**
* 默认被选中的值列表
*/
defaultValue: PropTypes.oneOfType([PropTypes.array, PropTypes.string, PropTypes.number]),
/**
* checkbox的尺寸
* @enumdesc 放大版大小, 正常大小
*/
size: PropTypes.oneOf(['large', 'medium']),
/**
* 通过子元素方式设置内部 checkbox
*/
children: PropTypes.arrayOf(PropTypes.element),
/**
* 选中值改变时的事件
* @param {Array} value 选中项列表
* @param {Event} e Dom 事件对象
*/
onChange: PropTypes.func,
/**
* 子项目的排列方式
* - hoz: 水平排列 (default)
* - ver: 垂直排列
*/
itemDirection: PropTypes.oneOf(['hoz', 'ver'])
});
_defineProperty(CheckboxGroup, "defaultProps", {
dataSource: [],
onChange: function onChange() {},
prefix: 'next-',
itemDirection: 'hoz',
size: 'medium'
});
_defineProperty(CheckboxGroup, "childContextTypes", {
onChange: PropTypes.func,
__group__: PropTypes.bool,
selectedValue: PropTypes.array,
disabled: PropTypes.bool
});
export default ConfigProvider.config(CheckboxGroup);