1 | import _extends from "@babel/runtime-corejs2/helpers/esm/extends";
|
2 | import _objectWithoutPropertiesLoose from "@babel/runtime-corejs2/helpers/esm/objectWithoutPropertiesLoose";
|
3 | import _inheritsLoose from "@babel/runtime-corejs2/helpers/esm/inheritsLoose";
|
4 | import _assertThisInitialized from "@babel/runtime-corejs2/helpers/esm/assertThisInitialized";
|
5 | import React from 'react';
|
6 | import PropTypes from 'prop-types';
|
7 | import elementType from 'prop-types-extra/lib/elementType';
|
8 | import createChainedFunction from './utils/createChainedFunction';
|
9 | var propTypes = {
|
10 | href: PropTypes.string,
|
11 | onClick: PropTypes.func,
|
12 | onKeyDown: PropTypes.func,
|
13 | disabled: PropTypes.bool,
|
14 | role: PropTypes.string,
|
15 | tabIndex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
16 |
|
17 | |
18 |
|
19 |
|
20 | componentClass: elementType
|
21 | };
|
22 | var defaultProps = {
|
23 | componentClass: 'a'
|
24 | };
|
25 |
|
26 | function isTrivialHref(href) {
|
27 | return !href || href.trim() === '#';
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var SafeAnchor =
|
39 |
|
40 | function (_React$Component) {
|
41 | _inheritsLoose(SafeAnchor, _React$Component);
|
42 |
|
43 | function SafeAnchor(props, context) {
|
44 | var _this;
|
45 |
|
46 | _this = _React$Component.call(this, props, context) || this;
|
47 | _this.handleClick = _this.handleClick.bind(_assertThisInitialized(_assertThisInitialized(_this)));
|
48 | _this.handleKeyDown = _this.handleKeyDown.bind(_assertThisInitialized(_assertThisInitialized(_this)));
|
49 | return _this;
|
50 | }
|
51 |
|
52 | var _proto = SafeAnchor.prototype;
|
53 |
|
54 | _proto.handleClick = function handleClick(event) {
|
55 | var _this$props = this.props,
|
56 | disabled = _this$props.disabled,
|
57 | href = _this$props.href,
|
58 | onClick = _this$props.onClick;
|
59 |
|
60 | if (disabled || isTrivialHref(href)) {
|
61 | event.preventDefault();
|
62 | }
|
63 |
|
64 | if (disabled) {
|
65 | event.stopPropagation();
|
66 | return;
|
67 | }
|
68 |
|
69 | if (onClick) {
|
70 | onClick(event);
|
71 | }
|
72 | };
|
73 |
|
74 | _proto.handleKeyDown = function handleKeyDown(event) {
|
75 | if (event.key === ' ') {
|
76 | event.preventDefault();
|
77 | this.handleClick(event);
|
78 | }
|
79 | };
|
80 |
|
81 | _proto.render = function render() {
|
82 | var _this$props2 = this.props,
|
83 | Component = _this$props2.componentClass,
|
84 | disabled = _this$props2.disabled,
|
85 | onKeyDown = _this$props2.onKeyDown,
|
86 | props = _objectWithoutPropertiesLoose(_this$props2, ["componentClass", "disabled", "onKeyDown"]);
|
87 |
|
88 | if (isTrivialHref(props.href)) {
|
89 | props.role = props.role || 'button';
|
90 |
|
91 |
|
92 | props.href = props.href || '#';
|
93 | }
|
94 |
|
95 | if (disabled) {
|
96 | props.tabIndex = -1;
|
97 | props.style = _extends({
|
98 | pointerEvents: 'none'
|
99 | }, props.style);
|
100 | }
|
101 |
|
102 | return React.createElement(Component, _extends({}, props, {
|
103 | onClick: this.handleClick,
|
104 | onKeyDown: createChainedFunction(this.handleKeyDown, onKeyDown)
|
105 | }));
|
106 | };
|
107 |
|
108 | return SafeAnchor;
|
109 | }(React.Component);
|
110 |
|
111 | SafeAnchor.propTypes = propTypes;
|
112 | SafeAnchor.defaultProps = defaultProps;
|
113 | export default SafeAnchor; |
\ | No newline at end of file |