@txdfe/at
Version:
一个设计体系组件库
279 lines (219 loc) • 11.3 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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
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 Checkbox from './checkbox';
var pickOthers = obj.pickOthers;
/** Checkbox.Group */
var CheckboxGroup = /*#__PURE__*/function (_Component) {
_inherits(CheckboxGroup, _Component);
var _super = _createSuper(CheckboxGroup);
function CheckboxGroup(props) {
var _this;
_classCallCheck(this, CheckboxGroup);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_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;
}
_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,
_classnames;
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((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "checkbox-group"), true), _defineProperty(_classnames, "".concat(prefix, "checkbox-group-").concat(_size), true), _defineProperty(_classnames, "".concat(prefix, "checkbox-group-").concat(itemDirection), true), _defineProperty(_classnames, className, !!className), _defineProperty(_classnames, "disabled", disabled), _classnames));
return /*#__PURE__*/React.createElement("span", _extends({
dir: rtl ? 'rtl' : undefined
}, others, {
className: cls,
style: style
}), children);
}
}]);
return CheckboxGroup;
}(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);