UNPKG

3.77 kBJavaScriptView Raw
1import { __assign, __extends } from "tslib";
2import * as React from 'react';
3import { classNamesFunction, KeyCodes, getNativeProps, divProperties, warnDeprecations, initializeComponentRef, } from '../../Utilities';
4import { DocumentCardType, } from './DocumentCard.types';
5var getClassNames = classNamesFunction();
6var COMPONENT_NAME = 'DocumentCard';
7/**
8 * {@docCategory DocumentCard}
9 */
10var DocumentCardBase = /** @class */ (function (_super) {
11 __extends(DocumentCardBase, _super);
12 function DocumentCardBase(props) {
13 var _this = _super.call(this, props) || this;
14 _this._rootElement = React.createRef();
15 _this._onClick = function (ev) {
16 _this._onAction(ev);
17 };
18 _this._onKeyDown = function (ev) {
19 if (ev.which === KeyCodes.enter || ev.which === KeyCodes.space) {
20 _this._onAction(ev);
21 }
22 };
23 _this._onAction = function (ev) {
24 var _a = _this.props, onClick = _a.onClick, onClickHref = _a.onClickHref, onClickTarget = _a.onClickTarget;
25 if (onClick) {
26 onClick(ev);
27 }
28 else if (!onClick && onClickHref) {
29 // If no onClick Function was provided and we do have an onClickHref, redirect to the onClickHref
30 if (onClickTarget) {
31 window.open(onClickHref, onClickTarget, 'noreferrer noopener nofollow');
32 }
33 else {
34 window.location.href = onClickHref;
35 }
36 ev.preventDefault();
37 ev.stopPropagation();
38 }
39 };
40 initializeComponentRef(_this);
41 warnDeprecations(COMPONENT_NAME, props, {
42 accentColor: undefined,
43 });
44 return _this;
45 }
46 DocumentCardBase.prototype.render = function () {
47 // eslint-disable-next-line deprecation/deprecation
48 var _a = this.props, onClick = _a.onClick, onClickHref = _a.onClickHref, children = _a.children, type = _a.type, accentColor = _a.accentColor, styles = _a.styles, theme = _a.theme, className = _a.className;
49 var nativeProps = getNativeProps(this.props, divProperties, [
50 'className',
51 'onClick',
52 'type',
53 'role',
54 ]);
55 var actionable = onClick || onClickHref ? true : false;
56 this._classNames = getClassNames(styles, {
57 theme: theme,
58 className: className,
59 actionable: actionable,
60 compact: type === DocumentCardType.compact ? true : false,
61 });
62 // Override the border color if an accent color was provided (compact card only)
63 var style;
64 if (type === DocumentCardType.compact && accentColor) {
65 style = {
66 borderBottomColor: accentColor,
67 };
68 }
69 // if this element is actionable it should have an aria role
70 var role = this.props.role || (actionable ? (onClick ? 'button' : 'link') : undefined);
71 var tabIndex = actionable ? 0 : undefined;
72 return (React.createElement("div", __assign({ ref: this._rootElement, tabIndex: tabIndex, "data-is-focusable": actionable, role: role, className: this._classNames.root, onKeyDown: actionable ? this._onKeyDown : undefined, onClick: actionable ? this._onClick : undefined, style: style }, nativeProps), children));
73 };
74 DocumentCardBase.prototype.focus = function () {
75 if (this._rootElement.current) {
76 this._rootElement.current.focus();
77 }
78 };
79 DocumentCardBase.defaultProps = {
80 type: DocumentCardType.normal,
81 };
82 return DocumentCardBase;
83}(React.Component));
84export { DocumentCardBase };
85//# sourceMappingURL=DocumentCard.base.js.map
\No newline at end of file