UNPKG

yantd

Version:

React component library

72 lines (71 loc) 3.37 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import * as React from 'react'; import classNames from 'classnames'; import { ConfigConsumer } from '../config-provider'; import AnchorContext from './context'; var AnchorLink = /** @class */ (function (_super) { __extends(AnchorLink, _super); function AnchorLink() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.handleClick = function (e) { var _a = _this.context, scrollTo = _a.scrollTo, onClick = _a.onClick; var _b = _this.props, href = _b.href, title = _b.title; onClick === null || onClick === void 0 ? void 0 : onClick(e, { title: title, href: href }); scrollTo(href); }; _this.renderAnchorLink = function (_a) { var _b, _c; var getPrefixCls = _a.getPrefixCls; var _d = _this.props, customizePrefixCls = _d.prefixCls, href = _d.href, title = _d.title, children = _d.children, className = _d.className, target = _d.target; var prefixCls = getPrefixCls('anchor', customizePrefixCls); var active = _this.context.activeLink === href; var wrapperClassName = classNames(prefixCls + "-link", (_b = {}, _b[prefixCls + "-link-active"] = active, _b), className); var titleClassName = classNames(prefixCls + "-link-title", (_c = {}, _c[prefixCls + "-link-title-active"] = active, _c)); return (React.createElement("div", { className: wrapperClassName }, React.createElement("a", { className: titleClassName, href: href, title: typeof title === 'string' ? title : '', target: target, onClick: _this.handleClick }, title), children)); }; return _this; } AnchorLink.prototype.componentDidMount = function () { this.context.registerLink(this.props.href); }; AnchorLink.prototype.componentDidUpdate = function (_a) { var prevHref = _a.href; var href = this.props.href; if (prevHref !== href) { this.context.unregisterLink(prevHref); this.context.registerLink(href); } }; AnchorLink.prototype.componentWillUnmount = function () { this.context.unregisterLink(this.props.href); }; AnchorLink.prototype.render = function () { return React.createElement(ConfigConsumer, null, this.renderAnchorLink); }; AnchorLink.defaultProps = { href: '#', }; AnchorLink.contextType = AnchorContext; return AnchorLink; }(React.Component)); export default AnchorLink;