@txdfe/at
Version:
一个设计体系组件库
192 lines (188 loc) • 11.9 kB
JavaScript
;
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(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
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 _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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
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); }
function mapIconSize(size) {
return {
large: 'large',
medium: 'medium',
small: 'medium',
xs: 'medium'
}[size];
}
/** Button */
var Button = exports["default"] = /*#__PURE__*/function (_Component) {
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 = _callSuper(this, Button, [].concat(args));
_defineProperty(_this, "onMouseUp", function (e) {
_this.button.blur();
if (_this.props.onMouseUp) {
_this.props.onMouseUp(e);
}
});
_defineProperty(_this, "buttonRefHandler", function (button) {
_this.button = button;
});
return _this;
}
_inherits(Button, _Component);
return _createClass(Button, [{
key: "render",
value: function render() {
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"])(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "btn"), true), "".concat(prefix).concat(size), size), "".concat(prefix, "btn-").concat(type), type && !ghost), "".concat(prefix, "btn-text"), text), "".concat(prefix, "btn-warning"), warning), "".concat(prefix, "btn-loading"), loading), "".concat(prefix, "btn-ghost"), ghost), "".concat(prefix, "btn-").concat(ghostType), ghost), className, className));
var count = _react.Children.count(children);
var clonedChildren = _react.Children.map(children, function (child, index) {
if (child && child.type && child.type._typeMark === 'icon') {
var iconCls = (0, _classnames["default"])(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "".concat(prefix, "btn-icon"), !iconSize), "".concat(prefix, "icon-first"), count > 1 && index === 0), "".concat(prefix, "icon-last"), count > 1 && index === count - 1), "".concat(prefix, "icon-alone"), count === 1), child.props.className, !!child.props.className));
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);
}
}]);
}(_react.Component);
_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() {}
});