UNPKG

4.24 kBJavaScriptView Raw
1import { __assign, __extends, __rest } from "tslib";
2import * as React from 'react';
3import { classNamesFunction, initializeComponentRef } from '../../Utilities';
4import { KeytipData } from '../../KeytipData';
5var getClassNames = classNamesFunction();
6var LinkBase = /** @class */ (function (_super) {
7 __extends(LinkBase, _super);
8 function LinkBase(props) {
9 var _this = _super.call(this, props) || this;
10 _this._link = React.createRef();
11 _this._renderContent = function (keytipAttributes) {
12 if (keytipAttributes === void 0) { keytipAttributes = {}; }
13 var _a = _this.props, disabled = _a.disabled, children = _a.children, className = _a.className, href = _a.href, underline = _a.underline, theme = _a.theme, styles = _a.styles;
14 var classNames = getClassNames(styles, {
15 className: className,
16 isButton: !href,
17 isDisabled: disabled,
18 isUnderlined: underline,
19 theme: theme,
20 });
21 var RootType = _this._getRootType(_this.props);
22 return (React.createElement(RootType, __assign({}, keytipAttributes, _this._adjustPropsForRootType(RootType, _this.props), { className: classNames.root, onClick: _this._onClick, ref: _this._link, "aria-disabled": disabled }), children));
23 };
24 _this._onClick = function (ev) {
25 var _a = _this.props, onClick = _a.onClick, disabled = _a.disabled;
26 if (disabled) {
27 ev.preventDefault();
28 }
29 else if (onClick) {
30 onClick(ev);
31 }
32 };
33 initializeComponentRef(_this);
34 return _this;
35 }
36 LinkBase.prototype.render = function () {
37 var _this = this;
38 var _a = this.props, disabled = _a.disabled, keytipProps = _a.keytipProps;
39 if (keytipProps) {
40 return (React.createElement(KeytipData, { keytipProps: keytipProps, ariaDescribedBy: this.props['aria-describedby'], disabled: disabled }, function (keytipAttributes) { return _this._renderContent(keytipAttributes); }));
41 }
42 return this._renderContent();
43 };
44 LinkBase.prototype.focus = function () {
45 var current = this._link.current;
46 if (current && current.focus) {
47 current.focus();
48 }
49 };
50 LinkBase.prototype._adjustPropsForRootType = function (RootType, props) {
51 // Deconstruct the props so we remove props like `as`, `theme` and `styles`
52 // as those will always be removed. We also take some props that are optional
53 // based on the RootType.
54 var children = props.children, as = props.as, disabled = props.disabled, target = props.target, href = props.href, theme = props.theme, getStyles = props.getStyles, styles = props.styles, componentRef = props.componentRef, keytipProps = props.keytipProps, underline = props.underline, restProps = __rest(props, ["children", "as", "disabled", "target", "href", "theme", "getStyles", "styles", "componentRef", "keytipProps", "underline"]);
55 // RootType will be a string if we're dealing with an html component
56 if (typeof RootType === 'string') {
57 // Remove the disabled prop for anchor elements
58 if (RootType === 'a') {
59 return __assign({ target: target, href: disabled ? undefined : href }, restProps);
60 }
61 // Add the type='button' prop for button elements
62 if (RootType === 'button') {
63 return __assign({ type: 'button', disabled: disabled }, restProps);
64 }
65 // Remove the target and href props for all other non anchor elements
66 return __assign(__assign({}, restProps), { disabled: disabled });
67 }
68 // Retain all props except 'as' for ReactComponents
69 return __assign({ target: target, href: href, disabled: disabled }, restProps);
70 };
71 LinkBase.prototype._getRootType = function (props) {
72 if (props.as) {
73 return props.as;
74 }
75 if (props.href) {
76 return 'a';
77 }
78 return 'button';
79 };
80 return LinkBase;
81}(React.Component));
82export { LinkBase };
83//# sourceMappingURL=Link.base.js.map
\No newline at end of file