UNPKG

choerodon-ui

Version:

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

122 lines (104 loc) 3.56 kB
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 _createSuper from "@babel/runtime/helpers/createSuper"; 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 /*#__PURE__*/React.createElement("div", { className: wrapperClassName }, /*#__PURE__*/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