UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

232 lines (199 loc) 7.65 kB
import _extends from "@babel/runtime/helpers/extends"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } import React, { Children, Component } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import omit from 'lodash/omit'; import Icon from '../icon'; import Ripple from '../ripple'; import { Size } from '../_util/enum'; import { getPrefixCls } from '../configure'; import { ProgressType } from '../progress/enum'; import Progress from '../progress'; var Button = /*#__PURE__*/ function (_Component) { _inherits(Button, _Component); var _super = _createSuper(Button); function Button(props) { var _this; _classCallCheck(this, Button); _this = _super.call(this, props); _this.handleClick = function (e) { clearTimeout(_this.timeout); _this.timeout = window.setTimeout(function () { return _this.setState({ clicked: false }); }, 500); var onClick = _this.props.onClick; if (onClick) { onClick(e); } }; _this.state = { loading: props.loading, clicked: false }; return _this; } _createClass(Button, [{ key: "componentWillReceiveProps", value: function componentWillReceiveProps(nextProps) { var _this2 = this; var currentLoading = this.props.loading; var loading = nextProps.loading; if (currentLoading) { clearTimeout(this.delayTimeout); } if (typeof loading !== 'boolean' && loading && loading.delay) { this.delayTimeout = window.setTimeout(function () { return _this2.setState({ loading: loading }); }, loading.delay); } else { this.setState({ loading: loading }); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { if (this.timeout) { clearTimeout(this.timeout); } if (this.delayTimeout) { clearTimeout(this.delayTimeout); } } }, { key: "render", value: function render() { var _classNames; var _this$props = this.props, customizePrefixCls = _this$props.prefixCls, type = _this$props.type, shape = _this$props.shape, size = _this$props.size, className = _this$props.className, htmlType = _this$props.htmlType, children = _this$props.children, icon = _this$props.icon, ghost = _this$props.ghost, funcType = _this$props.funcType, onMouseEnter = _this$props.onMouseEnter, onMouseLeave = _this$props.onMouseLeave, disabled = _this$props.disabled, others = _objectWithoutProperties(_this$props, ["prefixCls", "type", "shape", "size", "className", "htmlType", "children", "icon", "ghost", "funcType", "onMouseEnter", "onMouseLeave", "disabled"]); var _this$state = this.state, loading = _this$state.loading, clicked = _this$state.clicked; var prefixCls = getPrefixCls('btn', customizePrefixCls); // large => lg // small => sm var sizeCls = ''; switch (size) { case Size.large: sizeCls = 'lg'; break; case Size.small: sizeCls = 'sm'; break; default: } var ComponentProp = others.href ? 'a' : 'button'; var classes = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(type), type), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(shape), shape), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(sizeCls), sizeCls), _defineProperty(_classNames, "".concat(prefixCls, "-icon-only"), !children && icon), _defineProperty(_classNames, "".concat(prefixCls, "-loading"), loading), _defineProperty(_classNames, "".concat(prefixCls, "-clicked"), clicked), _defineProperty(_classNames, "".concat(prefixCls, "-background-ghost"), ghost), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(funcType), funcType), _classNames)); var iconNode = icon ? React.createElement(Icon, { type: icon }) : null; iconNode = loading ? React.createElement(Progress, { key: "loading", type: ProgressType.loading, size: Size.small }) : iconNode; var kids = children || children === 0 ? Children.map(children, function (child) { if (typeof child === 'string') { return React.createElement("span", null, child); } return child; }) : null; var useWrapper = disabled && ComponentProp === 'button' && (onMouseEnter || onMouseLeave); var style = others.style, otherProps = _objectWithoutProperties(others, ["style"]); var button = React.createElement(Ripple, { disabled: disabled }, React.createElement(ComponentProp, _extends({ disabled: disabled, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave }, omit(otherProps, ['loading']), { // 如果没有href属性,则表示组件使用button标签,type为'submit' | 'reset' | 'button' type: others.href ? undefined : htmlType || 'button', style: useWrapper ? undefined : style, className: useWrapper ? undefined : classes, onClick: this.handleClick }), iconNode, kids)); return useWrapper ? React.createElement("span", { // @ts-ignore disabled: true, style: style, className: classNames(classes, "".concat(prefixCls, "-disabled-wrapper")), onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave }, button) : button; } }]); return Button; }(Component); export { Button as default }; Button.displayName = 'Button'; Button.__C7N_BUTTON = true; Button.defaultProps = { loading: false, ghost: false, funcType: 'flat' }; Button.propTypes = { type: PropTypes.string, shape: PropTypes.oneOf(['circle', 'circle-outline']), size: PropTypes.oneOf([Size.large, Size["default"], Size.small]), htmlType: PropTypes.oneOf(['submit', 'button', 'reset']), onClick: PropTypes.func, loading: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]), className: PropTypes.string, icon: PropTypes.string, ghost: PropTypes.bool, funcType: PropTypes.oneOf(['raised', 'flat']) }; //# sourceMappingURL=Button.js.map