UNPKG

@alifd/meet-react

Version:

Fusion Mobile React UI System Component

123 lines 7.34 kB
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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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); } import { __rest } from "tslib"; import React, { createElement, forwardRef, Fragment, useCallback, useMemo, Children, cloneElement, isValidElement } from "react"; import classNames from 'classnames'; import { Text } from "@alifd/meet-react-component-one"; import View from '../view'; import Icon from '../icon'; import { isBoolean, isFunction, isNil } from '../utils'; var createChildren = function createChildren(children, classes, prefix, size) { var count = Children.count(children); return Children.map(children, function (child, index) { if (isNil(child) || isBoolean(child)) { return null; } var key = child.key || "mt-button-".concat(index); if (isValidElement(child)) { if (child.type === Text) { return cloneElement(child, { key: key, className: classNames(classes.textCls, child.props.className), style: child.props.style || {} }); } else if (child.type === Icon) { return cloneElement(child, { key: key, className: classNames(classes.iconCls, child.props.className, _defineProperty(_defineProperty({}, "".concat(prefix, "button-icon--").concat(size, "-first"), index === 0), "".concat(prefix, "button-icon--").concat(size, "-last"), index === count - 1)), style: child.props.style || {} }); } else { return cloneElement(child, { key: key }); } } else { return /*#__PURE__*/React.createElement(Text, { key: key, className: classes.textCls }, child); } }); }; var Button = function Button(props, ref) { var _props$prefix = props.prefix, prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix, _props$type = props.type, typeProp = _props$type === void 0 ? 'normal' : _props$type, _props$size = props.size, size = _props$size === void 0 ? 'medium' : _props$size, _props$fullWidth = props.fullWidth, fullWidth = _props$fullWidth === void 0 ? false : _props$fullWidth, _props$disabled = props.disabled, disabled = _props$disabled === void 0 ? false : _props$disabled, _props$loading = props.loading, loading = _props$loading === void 0 ? false : _props$loading, _props$text = props.text, text = _props$text === void 0 ? false : _props$text, _props$warning = props.warning, warning = _props$warning === void 0 ? false : _props$warning, children = props.children, _props$model = props.model, modelProp = _props$model === void 0 ? 'solid' : _props$model, className = props.className, _props$component = props.component, component = _props$component === void 0 ? View : _props$component, _props$onClick = props.onClick, onClick = _props$onClick === void 0 ? function () {} : _props$onClick, onDisabledClick = props.onDisabledClick, others = __rest(props, ["prefix", "type", "size", "fullWidth", "disabled", "loading", "text", "warning", "children", "model", "className", "component", "onClick", "onDisabledClick"]); var _useMemo = useMemo(function () { var nType = typeProp; var nModel = modelProp; if (typeProp === 'secondary') { nType = 'primary'; nModel = 'outline'; } if (text) { nModel = 'text'; } if (warning) { nType = 'warning'; } return [nType, nModel]; }, [typeProp, modelProp, warning, text]), _useMemo2 = _slicedToArray(_useMemo, 2), type = _useMemo2[0], model = _useMemo2[1]; var textCls = "".concat(prefix, "button-font--").concat(size); var iconCls = "".concat(prefix, "button-font--").concat(size); var classes = { textCls: textCls, iconCls: iconCls }; var handleClick = useCallback(function (e) { if (disabled) { if (isFunction(onDisabledClick)) { onDisabledClick(e); } return; } onClick(e); }, [disabled, onDisabledClick, onClick]); return /*#__PURE__*/createElement(component, Object.assign(Object.assign({ ref: ref }, others), { className: classNames(className, "".concat(prefix, "button"), "".concat(prefix, "button--").concat(size), "".concat(prefix, "button--").concat(type, "-").concat(model), _defineProperty(_defineProperty({}, "".concat(prefix, "button--fullwidth"), fullWidth), "".concat(prefix, "button--").concat(type, "-").concat(model, "-disabled"), disabled)), onClick: handleClick }), /*#__PURE__*/React.createElement(Fragment, null, loading ? /*#__PURE__*/React.createElement(Icon, { key: "prefix-icon", className: classNames(iconCls, "".concat(prefix, "button-icon--").concat(size, "-first")), name: "loading" }) : null, createChildren(children, classes, prefix, size))); }; Button.displayName = 'Button'; export default /*#__PURE__*/forwardRef(Button);