choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
186 lines (155 loc) • 5.48 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
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 _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
var _excluded = ["prefixCls", "closable", "color", "className", "children", "style"];
import React, { Component } from 'react';
import { findDOMNode } from 'react-dom';
import classNames from 'classnames';
import omit from 'lodash/omit';
import Icon from '../icon';
import CheckableTag from './CheckableTag';
import Animate from '../animate';
import { isPresetColor as isPresetColorUtil } from '../_util/colors';
import ConfigContext from '../config-provider/ConfigContext';
var Tag = /*#__PURE__*/function (_Component) {
_inherits(Tag, _Component);
var _super = _createSuper(Tag);
function Tag() {
var _this;
_classCallCheck(this, Tag);
_this = _super.apply(this, arguments);
_this.state = {
closing: false,
closed: false,
visible: true
};
_this.handleIconClick = function (e) {
var onClose = _this.props.onClose;
if (onClose) {
onClose(e);
}
if (e.defaultPrevented || 'visible' in _this.props) {
return;
}
_this.setState({
visible: false
});
};
_this.close = function () {
var _this$state = _this.state,
closing = _this$state.closing,
closed = _this$state.closed;
if (closing || closed) {
return;
}
var dom = findDOMNode(_assertThisInitialized(_this));
dom.style.width = "".concat(dom.getBoundingClientRect().width, "px"); // It's Magic Code, don't know why
dom.style.width = "".concat(dom.getBoundingClientRect().width, "px");
_this.setState({
closing: true
});
};
_this.show = function () {
_this.setState({
closed: false
});
};
_this.animationEnd = function (_, existed) {
var closed = _this.state.closed;
if (!existed && !closed) {
_this.setState({
closed: true,
closing: false
});
var afterClose = _this.props.afterClose;
if (afterClose) {
afterClose();
}
} else {
_this.setState({
closed: false
});
}
};
return _this;
}
_createClass(Tag, [{
key: "componentDidUpdate",
value: function componentDidUpdate(_prevProps, prevState) {
var visible = this.state.visible;
if (prevState.visible && !visible) {
this.close();
} else if (!prevState.visible && visible) {
this.show();
}
}
}, {
key: "render",
value: function render() {
var _classNames;
var _this$props = this.props,
customizePrefixCls = _this$props.prefixCls,
closable = _this$props.closable,
color = _this$props.color,
className = _this$props.className,
children = _this$props.children,
style = _this$props.style,
otherProps = _objectWithoutProperties(_this$props, _excluded);
var getPrefixCls = this.context.getPrefixCls;
var prefixCls = getPrefixCls('tag', customizePrefixCls);
var _this$state2 = this.state,
closing = _this$state2.closing,
closed = _this$state2.closed;
var closeIcon = closable ? /*#__PURE__*/React.createElement(Icon, {
type: "close",
onClick: this.handleIconClick
}) : '';
var isPresetColor = isPresetColorUtil(color);
var classString = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-").concat(color), isPresetColor), _defineProperty(_classNames, "".concat(prefixCls, "-has-color"), color && !isPresetColor), _defineProperty(_classNames, "".concat(prefixCls, "-close"), closing), _classNames), className); // fix https://fb.me/react-unknown-prop
var divProps = omit(otherProps, ['onClose', 'afterClose', 'visible']);
var tagStyle = _objectSpread({}, style);
if (color && !isPresetColor) {
tagStyle.backgroundColor = color;
}
var tag = closed ? null : /*#__PURE__*/React.createElement("div", _extends({
hidden: closing
}, divProps, {
className: classString,
style: tagStyle
}), children, closeIcon);
return /*#__PURE__*/React.createElement(Animate, {
component: "",
hiddenProp: "hidden",
transitionName: "".concat(prefixCls, "-zoom"),
transitionAppear: true,
onEnd: this.animationEnd
}, tag);
}
}], [{
key: "contextType",
get: function get() {
return ConfigContext;
}
}, {
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps) {
return 'visible' in nextProps ? {
visible: nextProps.visible
} : null;
}
}]);
return Tag;
}(Component);
export { Tag as default };
Tag.displayName = 'Tag';
Tag.CheckableTag = CheckableTag;
Tag.defaultProps = {
closable: false
};
//# sourceMappingURL=index.js.map