@txdfe/at
Version:
一个设计体系组件库
334 lines (271 loc) • 13.5 kB
JavaScript
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); }
var _excluded = ["id", "className", "children", "style", "label", "size", "onMouseEnter", "onMouseLeave", "rtl"];
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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 from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import UIState from '../mixin-ui-state';
import ConfigProvider from '../config-provider';
import Icon from '../icon';
import { obj, func } from '../util';
var noop = func.noop;
function isChecked(selectedValue, value) {
return selectedValue.indexOf(value) > -1;
}
var checkedicon =
/*#__PURE__*/
// 选中图标的svg
React.createElement("svg", {
viewBox: "0 0 1024 1024"
}, /*#__PURE__*/React.createElement("path", {
d: "M275.93,503.71A55.33,55.33,0,1,0,195,579.23L361,757.08a55.33,55.33,0,0,0,78.2,2.7q1.4-1.3,2.7-2.7l387.32-415a55.33,55.33,0,0,0-80.9-75.51L401.51,638.22Z"
}));
var indeteicon =
/*#__PURE__*/
// 中间状态的svg
React.createElement("svg", {
viewBox: "0 0 1024 1024"
}, /*#__PURE__*/React.createElement("path", {
d: "M235.24,567.35H788.76a55.35,55.35,0,0,0,0-110.7H235.24a55.35,55.35,0,0,0,0,110.7Z"
}));
/**
* Checkbox
* @order 1
*/
var Checkbox = /*#__PURE__*/function (_UIState) {
_inherits(Checkbox, _UIState);
var _super = _createSuper(Checkbox);
function Checkbox(props, context) {
var _this;
_classCallCheck(this, Checkbox);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "onChange", function (e) {
var checked = e.target.checked;
var value = _this.props.value;
if (_this.disabled) {
return;
}
if (_this.context.__group__) {
_this.context.onChange(value, e);
} else {
if (!('checked' in _this.props)) {
_this.setState({
checked: checked
});
}
if (!('indeterminate' in _this.props)) {
_this.setState({
indeterminate: false
});
}
_this.props.onChange(checked, e);
}
});
var _checked;
var indeterminate;
if ('checked' in props) {
_checked = props.checked;
} else {
_checked = props.defaultChecked;
}
if ('indeterminate' in props) {
indeterminate = props.indeterminate;
} else {
indeterminate = props.defaultIndeterminate;
}
if (context.__group__) {
_checked = isChecked(context.selectedValue, props.value);
}
_this.state = {
checked: _checked,
indeterminate: indeterminate
};
_this.disabled = props.disabled || context.__group__ && 'disabled' in context && context.disabled;
return _this;
}
_createClass(Checkbox, [{
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps, nextContext) {
if (nextContext.__group__) {
if ('selectedValue' in nextContext) {
this.setState({
checked: isChecked(nextContext.selectedValue, nextProps.value)
});
}
this.disabled = nextProps.disabled || 'disabled' in nextContext && nextContext.disabled;
} else {
if ('checked' in nextProps) {
this.setState({
checked: nextProps.checked
});
}
if ('indeterminate' in nextProps) {
this.setState({
indeterminate: nextProps.indeterminate
});
}
this.disabled = nextProps.disabled;
}
}
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps, nextState, nextContext) {
var shallowEqual = obj.shallowEqual;
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState) || !shallowEqual(this.context, nextContext);
}
}, {
key: "render",
value: function render() {
var _classnames, _classnames2, _classnames3;
/* eslint-disable no-unused-vars */
var _this$props = this.props,
id = _this$props.id,
className = _this$props.className,
children = _this$props.children,
style = _this$props.style,
label = _this$props.label,
size = _this$props.size,
onMouseEnter = _this$props.onMouseEnter,
onMouseLeave = _this$props.onMouseLeave,
rtl = _this$props.rtl,
otherProps = _objectWithoutProperties(_this$props, _excluded);
var checked = !!this.state.checked;
var disabled = this.disabled;
var indeterminate = !!this.state.indeterminate;
var prefix = this.context.prefix || this.props.prefix;
var others = obj.pickOthers(Checkbox.propTypes, otherProps);
var othersData = obj.pickAttrsWith(others, 'data-');
var childInput = /*#__PURE__*/React.createElement("input", _extends({}, obj.pickOthers(Checkbox.propTypes, otherProps), {
id: id,
disabled: disabled,
checked: checked,
type: "checkbox",
onChange: this.onChange,
"aria-checked": indeterminate ? 'mixed' : checked,
className: "".concat(prefix, "checkbox-input")
})); // disable 无状态操作
if (!disabled) {
childInput = this.getStateElement(childInput);
}
var _size = size;
if (_size !== 'large' && _size !== 'medium') {
_size = 'medium';
}
var cls = classnames((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "checkbox-wrapper"), true), _defineProperty(_classnames, "".concat(prefix, "checkbox-wrapper-").concat(_size), true), _defineProperty(_classnames, className, !!className), _defineProperty(_classnames, "checked", checked), _defineProperty(_classnames, "disabled", disabled), _defineProperty(_classnames, "indeterminate", indeterminate), _classnames));
var labelCls = classnames((_classnames2 = {}, _defineProperty(_classnames2, "".concat(prefix, "checkbox-label"), true), _defineProperty(_classnames2, "".concat(prefix, "checkbox-label-").concat(_size), true), _classnames2));
var iconCls = classnames((_classnames3 = {}, _defineProperty(_classnames3, "".concat(prefix, "icon"), true), _defineProperty(_classnames3, "".concat(prefix, "xs"), _size === 'medium'), _defineProperty(_classnames3, "".concat(prefix, "small"), _size === 'large'), _classnames3));
var type = indeterminate ? 'horizontal' : 'tick-bold';
return /*#__PURE__*/React.createElement("label", _extends({}, othersData, {
className: cls,
style: style,
dir: rtl ? 'rtl' : undefined,
onMouseEnter: onMouseEnter,
onMouseLeave: onMouseLeave
}), /*#__PURE__*/React.createElement("span", {
className: "".concat(prefix, "checkbox")
}, /*#__PURE__*/React.createElement("span", {
className: "".concat(prefix, "checkbox-inner")
}, /*#__PURE__*/React.createElement("i", {
className: iconCls
}, indeterminate ? indeteicon : checkedicon)), childInput), [label, children].map(function (item, i) {
return [undefined, null].indexOf(item) === -1 ? /*#__PURE__*/React.createElement("span", {
key: i,
className: labelCls
}, item) : null;
}));
}
}]);
return Checkbox;
}(UIState);
_defineProperty(Checkbox, "displayName", 'Checkbox');
_defineProperty(Checkbox, "propTypes", {
prefix: PropTypes.string,
rtl: PropTypes.bool,
/**
* 自定义类名
*/
className: PropTypes.string,
/**
* checkbox id, 挂载在input上
*/
id: PropTypes.string,
/**
* 自定义内敛样式
*/
style: PropTypes.object,
/**
* 选中状态
*/
checked: PropTypes.bool,
/**
* 默认选中状态
*/
defaultChecked: PropTypes.bool,
/**
* 禁用
*/
disabled: PropTypes.bool,
/**
* checkbox的尺寸
* @enumdesc 放大版大小, 正常大小
*/
size: PropTypes.oneOf(['large', 'medium']),
/**
* 通过属性配置label,
*/
label: PropTypes.node,
/**
* Checkbox 的中间状态,只会影响到 Checkbox 的样式,并不影响其 checked 属性
*/
indeterminate: PropTypes.bool,
/**
* Checkbox 的默认中间态,只会影响到 Checkbox 的样式,并不影响其 checked 属性
*/
defaultIndeterminate: PropTypes.bool,
/**
* 状态变化时触发的事件
* @param {Boolean} checked 是否选中
* @param {Event} e Dom 事件对象
*/
onChange: PropTypes.func,
/**
* 鼠标进入enter事件
* @param {Event} e Dom 事件对象
*/
onMouseEnter: PropTypes.func,
/**
* 鼠标离开Leave事件
* @param {Event} e Dom 事件对象
*/
onMouseLeave: PropTypes.func
});
_defineProperty(Checkbox, "defaultProps", {
defaultChecked: false,
defaultIndeterminate: false,
onChange: noop,
onMouseEnter: noop,
onMouseLeave: noop,
prefix: 'next-',
size: 'medium'
});
_defineProperty(Checkbox, "contextTypes", {
onChange: PropTypes.func,
__group__: PropTypes.bool,
selectedValue: PropTypes.array,
disabled: PropTypes.bool,
prefix: PropTypes.string
});
export default ConfigProvider.config(Checkbox);