choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
156 lines (131 loc) • 4.41 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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, { Component } from 'react';
import classNames from 'classnames';
import AnchorContext from './AnchorContext';
var AnchorLink =
/*#__PURE__*/
function (_Component) {
_inherits(AnchorLink, _Component);
var _super = _createSuper(AnchorLink);
function AnchorLink() {
var _this;
_classCallCheck(this, AnchorLink);
_this = _super.apply(this, arguments);
_this.handleClick = function (e) {
var c7nAnchor = _this.context.c7nAnchor;
if (c7nAnchor) {
var scrollTo = c7nAnchor.scrollTo,
onClick = c7nAnchor.onClick;
var _this$props = _this.props,
href = _this$props.href,
title = _this$props.title;
if (onClick) {
onClick(e, {
title: title,
href: href
});
}
scrollTo(href);
}
};
return _this;
}
_createClass(AnchorLink, [{
key: "componentDidMount",
value: function componentDidMount() {
var c7nAnchor = this.context.c7nAnchor;
if (c7nAnchor) {
var href = this.props.href;
c7nAnchor.registerLink(href);
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(_ref) {
var prevHref = _ref.href;
var href = this.props.href;
if (prevHref !== href) {
var c7nAnchor = this.context.c7nAnchor;
if (c7nAnchor) {
c7nAnchor.unregisterLink(prevHref);
c7nAnchor.registerLink(href);
}
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
var c7nAnchor = this.context.c7nAnchor;
var href = this.props.href;
if (c7nAnchor) {
c7nAnchor.unregisterLink(href);
}
}
}, {
key: "render",
value: function render() {
var _this$context = this.context,
c7nAnchor = _this$context.c7nAnchor,
getPrefixCls = _this$context.getPrefixCls;
var _this$props2 = this.props,
customizePrefixCls = _this$props2.prefixCls,
href = _this$props2.href,
title = _this$props2.title,
children = _this$props2.children,
className = _this$props2.className;
var prefixCls = getPrefixCls('anchor', customizePrefixCls);
var active = c7nAnchor && c7nAnchor.activeLink === href;
var wrapperClassName = classNames(className, "".concat(prefixCls, "-link"), _defineProperty({}, "".concat(prefixCls, "-link-active"), active));
var titleClassName = classNames("".concat(prefixCls, "-link-title"), _defineProperty({}, "".concat(prefixCls, "-link-title-active"), active));
return React.createElement("div", {
className: wrapperClassName
}, React.createElement("a", {
className: titleClassName,
href: href,
title: typeof title === 'string' ? title : '',
onClick: this.handleClick
}, title), children);
}
}], [{
key: "contextType",
get: function get() {
return AnchorContext;
}
}]);
return AnchorLink;
}(Component);
export { AnchorLink as default };
AnchorLink.displayName = 'AnchorLink';
AnchorLink.defaultProps = {
href: '#'
};
//# sourceMappingURL=AnchorLink.js.map