@txdfe/at
Version:
一个设计体系组件库
282 lines (229 loc) • 12.1 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", "onMouseEnter", "onMouseLeave", "tabIndex", "rtl", "name"];
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 { obj, func } from '../util';
var makeChain = func.makeChain,
noop = func.noop;
/**
* Radio
* @order 1
*/
var Radio = /*#__PURE__*/function (_UIState) {
_inherits(Radio, _UIState);
var _super = _createSuper(Radio);
function Radio(props, context) {
var _this;
_classCallCheck(this, Radio);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "onChange", function (e) {
var checked = e.target.checked;
var value = _this.props.value;
if (_this.context.__group__) {
_this.context.onChange(value, e);
} else if (_this.state.checked !== checked) {
if (!('checked' in _this.props)) {
_this.setState({
checked: checked
});
}
_this.props.onChange(checked, e);
}
});
var _checked;
if (context.__group__) {
_checked = context.selectedValue === props.value;
} else if ('checked' in props) {
_checked = props.checked;
} else {
_checked = props.defaultChecked;
}
_this.state = {
checked: _checked
};
_this.disabled = props.disabled || context.__group__ && 'disabled' in context && context.disabled;
return _this;
}
_createClass(Radio, [{
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps, nextContext) {
if (nextContext.__group__) {
var selectedValue = nextContext.selectedValue;
if ('selectedValue' in nextContext) {
this.setState({
checked: selectedValue === nextProps.value
});
}
} else if ('checked' in nextProps) {
this.setState({
checked: nextProps.checked
});
}
this.disabled = nextProps.disabled || nextContext.__group__ && 'disabled' in nextContext && nextContext.disabled; // when disabled, reset UIState
if (this.disabled) {
// only class has an impact, no effect on visual
this.resetUIState();
}
}
}, {
key: "shouldComponentUpdate",
value: function shouldComponentUpdate(nextProps, nextState, nextContext) {
var shallowEqual = obj.shallowEqual;
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState) || !shallowEqual(this.nextContext, 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,
onMouseEnter = _this$props.onMouseEnter,
onMouseLeave = _this$props.onMouseLeave,
tabIndex = _this$props.tabIndex,
rtl = _this$props.rtl,
name = _this$props.name,
otherProps = _objectWithoutProperties(_this$props, _excluded);
var checked = !!this.state.checked;
var disabled = this.disabled;
var isButton = this.context.isButton;
var prefix = this.context.prefix || this.props.prefix;
var others = obj.pickOthers(Radio.propTypes, otherProps);
var othersData = obj.pickAttrsWith(others, 'data-');
var input = /*#__PURE__*/React.createElement("input", _extends({}, obj.pickOthers(othersData, others), {
name: name,
id: id,
role: "radio",
tabIndex: tabIndex,
disabled: disabled,
checked: checked,
type: "radio",
onChange: this.onChange,
"aria-checked": checked,
className: "".concat(prefix, "radio-input")
})); // disabled do not hove focus state
if (!disabled) {
input = this.getStateElement(input);
}
var cls = classnames((_classnames = {}, _defineProperty(_classnames, "".concat(prefix, "radio"), true), _defineProperty(_classnames, "checked", checked), _defineProperty(_classnames, "disabled", disabled), _classnames));
var clsInner = classnames((_classnames2 = {}, _defineProperty(_classnames2, "".concat(prefix, "radio-inner"), true), _defineProperty(_classnames2, "press", checked), _defineProperty(_classnames2, "unpress", !checked), _classnames2));
var clsWrapper = classnames((_classnames3 = {}, _defineProperty(_classnames3, "".concat(prefix, "radio-wrapper"), true), _defineProperty(_classnames3, className, !!className), _defineProperty(_classnames3, "checked", checked), _defineProperty(_classnames3, "disabled", disabled), _classnames3));
var childrenCls = "".concat(prefix, "radio-label");
var radioComp = !isButton ? /*#__PURE__*/React.createElement("span", {
className: cls
}, /*#__PURE__*/React.createElement("span", {
className: clsInner
}), input) : /*#__PURE__*/React.createElement("span", {
className: "".concat(prefix, "radio-single-input")
}, input);
return /*#__PURE__*/React.createElement("label", _extends({}, othersData, {
dir: rtl ? 'rtl' : 'ltr',
style: style,
"aria-checked": checked,
"aria-disabled": disabled,
className: clsWrapper,
onMouseEnter: disabled ? onMouseEnter : makeChain(this._onUIMouseEnter, onMouseEnter),
onMouseLeave: disabled ? onMouseLeave : makeChain(this._onUIMouseLeave, onMouseLeave)
}), radioComp, [children, label].map(function (d, i) {
return d !== undefined ? /*#__PURE__*/React.createElement("span", {
key: i,
className: childrenCls
}, d) : null;
}));
}
}]);
return Radio;
}(UIState);
_defineProperty(Radio, "displayName", 'Radio');
_defineProperty(Radio, "propTypes", {
prefix: PropTypes.string,
rtl: PropTypes.bool,
/**
* 自定义类名
*/
className: PropTypes.string,
/**
* 组件input的id
*/
id: PropTypes.string,
/**
* 自定义内敛样式
*/
style: PropTypes.object,
/**
* 设置radio是否选中
*/
checked: PropTypes.bool,
/**
* 设置radio是否默认选中
*/
defaultChecked: PropTypes.bool,
/**
* 通过属性配置label
*/
label: PropTypes.node,
/**
* 状态变化时触发的事件
* @param {Boolean} checked 是否选中
* @param {Event} e Dom 事件对象
*/
onChange: PropTypes.func,
/**
* 鼠标进入enter事件
* @param {Event} e Dom 事件对象
*/
onMouseEnter: PropTypes.func,
/**
* 鼠标离开事件
* @param {Event} e Dom 事件对象
*/
onMouseLeave: PropTypes.func,
/**
* radio是否被禁用
*/
disabled: PropTypes.bool,
/**
* radio 的value
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
/**
* name
*/
name: PropTypes.string
});
_defineProperty(Radio, "defaultProps", {
onChange: noop,
onMouseLeave: noop,
onMouseEnter: noop,
tabIndex: 0,
prefix: 'next-'
});
_defineProperty(Radio, "contextTypes", {
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(Radio);