@fpxfd/next
Version:
A configurable component library for web built on React.
128 lines (108 loc) • 4.83 kB
JavaScript
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var _class, _temp2;
import classNames from 'classnames';
import PropTypes from 'prop-types';
import * as React from 'react';
import { polyfill } from 'react-lifecycles-compat';
import ConfigProvider from '../config-provider';
import AnchorContext from './context';
export var ConfigContext = React.createContext({
// We provide a default function for Context without provider
});
export var ConfigConsumer = ConfigContext.Consumer;
var AnchorLink = (_temp2 = _class = function (_React$Component) {
_inherits(AnchorLink, _React$Component);
function AnchorLink() {
var _temp, _this, _ret;
_classCallCheck(this, AnchorLink);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.handleClick = function (e) {
var _this$context = _this.context,
scrollTo = _this$context.scrollTo,
onClick = _this$context.onClick;
var _this$props = _this.props,
href = _this$props.href,
title = _this$props.title;
onClick && onClick(e, { title: title, href: href });
scrollTo(href);
}, _this.renderAnchorLink = function () {
var _classNames, _classNames2;
var _this$props2 = _this.props,
href = _this$props2.href,
title = _this$props2.title,
className = _this$props2.className,
target = _this$props2.target,
allowFold = _this$props2.allowFold,
isOpen = _this$props2.isOpen,
isSub = _this$props2.isSub,
style = _this$props2.style;
var prefixCls = 'next-anchor';
var active = _this.context.activeLink === href;
var wrapperClassName = classNames(prefixCls + '-link ' + (allowFold ? prefixCls + '-link-allowFold' : ''), (_classNames = {}, _classNames[prefixCls + '-link-active'] = active, _classNames), prefixCls + '-link-allowFold-' + (isOpen ? 'open' : 'close'), '' + (isSub ? prefixCls + '-link-sub' : ''), className);
var titleClassName = classNames(prefixCls + '-link-title', (_classNames2 = {}, _classNames2[prefixCls + '-link-title-active'] = active, _classNames2));
return React.createElement(
'div',
{ onClick: _this.handleClick, className: wrapperClassName, style: style || {} },
React.createElement(
'a',
{
className: titleClassName,
href: href,
title: typeof title === 'string' ? title : '',
target: target,
style: style || {}
},
isOpen ? title : ''
)
);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
AnchorLink.prototype.componentDidMount = function componentDidMount() {
if (!this.props.isParent) {
this.context.registerLink(this.props.href);
}
};
AnchorLink.prototype.componentDidUpdate = function componentDidUpdate(_ref) {
var prevHref = _ref.href;
var href = this.props.href;
if (prevHref !== href) {
this.context.unregisterLink(prevHref);
if (!this.props.isParent) {
this.context.registerLink(href);
}
this.context.registerLink(href);
}
};
AnchorLink.prototype.componentWillUnmount = function componentWillUnmount() {
this.context.unregisterLink(this.props.href);
};
AnchorLink.prototype.render = function render() {
return React.createElement(
ConfigConsumer,
null,
this.renderAnchorLink
);
};
return AnchorLink;
}(React.Component), _class.propTypes = {
prefixCls: PropTypes.string,
href: PropTypes.string,
target: PropTypes.string,
title: PropTypes.string,
className: PropTypes.string,
allowFold: PropTypes.bool,
isOpen: PropTypes.bool,
isSub: PropTypes.bool,
isParent: PropTypes.bool,
style: PropTypes.any
}, _class.defaultProps = {
prefixCls: 'next-',
href: '#',
allowFold: false
}, _class.contextType = AnchorContext, _temp2);
AnchorLink.displayName = 'AnchorLink';
export default ConfigProvider.config(polyfill(AnchorLink));