UNPKG

acud

Version:

acg react 组件库

124 lines • 4.94 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn"; import _isNativeReflectConstruct from "@babel/runtime/helpers/esm/isNativeReflectConstruct"; import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/esm/inherits"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } import * as React from 'react'; import classNames from 'classnames'; import { ConfigConsumer } from '../config-provider'; import AnchorContext from './context'; var AnchorLink = /*#__PURE__*/function (_React$Component) { function AnchorLink() { var _this; _classCallCheck(this, AnchorLink); _this = _callSuper(this, AnchorLink, arguments); _this.handleClick = function (e) { var _this$props = _this.props, href = _this$props.href, title = _this$props.title, disabled = _this$props.disabled; if (disabled) { e.preventDefault(); return; } var _this$context = _this.context, scrollTo = _this$context.scrollTo, onClick = _this$context.onClick; if (onClick) { onClick(e, { title: title, href: href }); } scrollTo(href); }; _this.handleMousehover = function (active, isEnter) { if (active) { var inkRef = _this.context.inkRef; var cla = inkRef.current.getAttribute('class'); inkRef.current.setAttribute('class', isEnter ? "".concat(cla, " hover") : cla.replace('hover', '').trim()); } }; _this.handleMouseClick = function (active, isDown) { if (active) { var inkRef = _this.context.inkRef; var cla = inkRef.current.getAttribute('class'); inkRef.current.setAttribute('class', isDown ? "".concat(cla, " click") : cla.replace('click', '').trim()); } }; _this.renderAnchorLink = function (_ref) { var getPrefixCls = _ref.getPrefixCls; var _this$props2 = _this.props, customizePrefixCls = _this$props2.prefixCls, href = _this$props2.href, title = _this$props2.title, className = _this$props2.className, style = _this$props2.style, target = _this$props2.target, disabled = _this$props2.disabled, children = _this$props2.children; var prefixCls = getPrefixCls('anchor', customizePrefixCls); var active = _this.context.activeLink === href; var wrapperClassName = classNames("".concat(prefixCls, "-link"), _defineProperty({}, "".concat(prefixCls, "-link-active"), active), className); var titleClassName = classNames("".concat(prefixCls, "-link-title"), _defineProperty(_defineProperty({}, "".concat(prefixCls, "-link-title-active"), active), "".concat(prefixCls, "-link-title-disabled"), disabled)); return /*#__PURE__*/React.createElement("div", { className: wrapperClassName, style: style }, /*#__PURE__*/React.createElement("a", { className: titleClassName, href: href, title: typeof title === 'string' ? title : '', target: target, onMouseEnter: function onMouseEnter() { return _this.handleMousehover(active, true); }, onMouseLeave: function onMouseLeave() { return _this.handleMousehover(active, false); }, onMouseDown: function onMouseDown() { return _this.handleMouseClick(active, true); }, onMouseUp: function onMouseUp() { return _this.handleMouseClick(active, false); }, onClick: _this.handleClick }, title), children); }; return _this; } _inherits(AnchorLink, _React$Component); return _createClass(AnchorLink, [{ key: "componentDidMount", value: function componentDidMount() { this.context.registerLink(this.props.href); } }, { key: "componentDidUpdate", value: function componentDidUpdate(_ref2) { var prevHref = _ref2.href; var href = this.props.href; if (prevHref !== href) { this.context.unregisterLink(prevHref); this.context.registerLink(href); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.context.unregisterLink(this.props.href); } }, { key: "render", value: function render() { return /*#__PURE__*/React.createElement(ConfigConsumer, null, this.renderAnchorLink); } }]); }(React.Component); AnchorLink.defaultProps = { href: '#' }; AnchorLink.contextType = AnchorContext; export default AnchorLink;