@txdfe/at
Version:
一个设计体系组件库
257 lines (190 loc) • 13.6 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); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _icon = _interopRequireDefault(require("../../icon"));
var _excluded = ["prefix", "className", "type", "size", "htmlType", "loading", "text", "warning", "ghost", "component", "iconSize", "children", "rtl"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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; }
function mapIconSize(size) {
return {
large: 'large',
medium: 'medium',
small: 'medium',
xs: 'medium'
}[size];
}
/** Button */
var Button = /*#__PURE__*/function (_Component) {
_inherits(Button, _Component);
var _super = _createSuper(Button);
function Button() {
var _this;
_classCallCheck(this, Button);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "onMouseUp", function (e) {
_this.button.blur();
if (_this.props.onMouseUp) {
_this.props.onMouseUp(e);
}
});
_defineProperty(_assertThisInitialized(_this), "buttonRefHandler", function (button) {
_this.button = button;
});
return _this;
}
_createClass(Button, [{
key: "render",
value: function render() {
var _classNames;
var _this$props = this.props,
prefix = _this$props.prefix,
className = _this$props.className,
type = _this$props.type,
size = _this$props.size,
htmlType = _this$props.htmlType,
loading = _this$props.loading,
text = _this$props.text,
warning = _this$props.warning,
ghost = _this$props.ghost,
component = _this$props.component,
iconSize = _this$props.iconSize,
children = _this$props.children,
rtl = _this$props.rtl,
others = _objectWithoutProperties(_this$props, _excluded);
var ghostType = ['light', 'dark'].indexOf(ghost) >= 0 ? ghost : 'dark';
var btnCls = (0, _classnames["default"])((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "btn"), true), _defineProperty(_classNames, "".concat(prefix).concat(size), size), _defineProperty(_classNames, "".concat(prefix, "btn-").concat(type), type && !ghost), _defineProperty(_classNames, "".concat(prefix, "btn-text"), text), _defineProperty(_classNames, "".concat(prefix, "btn-warning"), warning), _defineProperty(_classNames, "".concat(prefix, "btn-loading"), loading), _defineProperty(_classNames, "".concat(prefix, "btn-ghost"), ghost), _defineProperty(_classNames, "".concat(prefix, "btn-").concat(ghostType), ghost), _defineProperty(_classNames, className, className), _classNames));
var count = _react.Children.count(children);
var clonedChildren = _react.Children.map(children, function (child, index) {
if (child && child.type && child.type._typeMark === 'icon') {
var _classNames2;
var iconCls = (0, _classnames["default"])((_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefix, "btn-icon"), !iconSize), _defineProperty(_classNames2, "".concat(prefix, "icon-first"), count > 1 && index === 0), _defineProperty(_classNames2, "".concat(prefix, "icon-last"), count > 1 && index === count - 1), _defineProperty(_classNames2, "".concat(prefix, "icon-alone"), count === 1), _defineProperty(_classNames2, child.props.className, !!child.props.className), _classNames2));
return /*#__PURE__*/_react["default"].cloneElement(child, {
className: iconCls,
size: iconSize || mapIconSize(size)
});
} // 两个字的中文中间添加间距
// if (count === 1 && typeof child === 'string' && /^[\u4e00-\u9fa5]{2}$/.test(child)) {
// return <>{child[0]}<span className={`${prefix}btn-space`} />{child[1]}</>;
// }
return child;
});
var TagName = component;
var tagAttrs = _objectSpread(_objectSpread({}, others), {}, {
type: htmlType,
className: btnCls
});
if (TagName !== 'button') {
delete tagAttrs.type;
if (tagAttrs.disabled) {
delete tagAttrs.onClick; // a 标签的 onClick 浏览器默认不会禁用
tagAttrs.href && delete tagAttrs.href; // a 标签在禁用状态下无跳转
}
}
return /*#__PURE__*/_react["default"].createElement(TagName, _extends({}, tagAttrs, {
dir: rtl ? 'rtl' : undefined,
onMouseUp: this.onMouseUp,
ref: this.buttonRefHandler
}), /*#__PURE__*/_react["default"].createElement("span", {
className: "".concat(prefix, "btn-children")
}, clonedChildren), loading ? /*#__PURE__*/_react["default"].createElement("span", {
className: "".concat(prefix, "btn-loading-container")
}, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
type: "loading",
size: iconSize || mapIconSize(size)
})) : null);
}
}]);
return Button;
}(_react.Component);
exports["default"] = Button;
_defineProperty(Button, "propTypes", {
prefix: _propTypes["default"].string,
rtl: _propTypes["default"].bool,
/**
* 按钮的类型
*/
type: _propTypes["default"].oneOf(['primary', 'secondary', 'normal', 'tertiary']),
/**
* 按钮的尺寸
*/
size: _propTypes["default"].oneOf(['xs', 'small', 'medium', 'large']),
/**
* 按钮中 Icon 的尺寸,用于替代 Icon 的默认大小
*/
iconSize: _propTypes["default"].oneOf(['xs', 'small', 'medium', 'large', 'xl', 'xxl', 'xxxl']),
/**
* 当 component = 'button' 时,设置 button 标签的 type 值
*/
htmlType: _propTypes["default"].oneOf(['submit', 'reset', 'button']),
/**
* 设置标签类型
*/
component: _propTypes["default"].oneOf(['button', 'a', 'div', 'span']),
/**
* 设置按钮的载入状态
*/
loading: _propTypes["default"].bool,
/**
* 是否为幽灵按钮
*/
ghost: _propTypes["default"].oneOf([true, false, 'light', 'dark']),
/**
* 是否为文本按钮
*/
text: _propTypes["default"].bool,
/**
* 是否为警告按钮
*/
warning: _propTypes["default"].bool,
/**
* 是否禁用
*/
disabled: _propTypes["default"].bool,
/**
* 点击按钮的回调
* @param {Object} e Event Object
*/
onClick: _propTypes["default"].func,
className: _propTypes["default"].string,
onMouseUp: _propTypes["default"].func,
children: _propTypes["default"].node
});
_defineProperty(Button, "defaultProps", {
prefix: 'next-',
type: 'normal',
size: 'medium',
htmlType: 'button',
component: 'button',
loading: false,
ghost: false,
text: false,
warning: false,
disabled: false,
onClick: function onClick() {}
});