-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathlink.jsx
98 lines (85 loc) · 2.44 KB
/
link.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */
/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */
// # App Launcher Link Component
// ## Dependencies
// ### React
import React from 'react';
import PropTypes from 'prop-types';
// ### classNames
// [github.com/JedWatson/classnames](https://github.com/JedWatson/classnames)
// A simple javascript utility for conditionally joining classNames together.
import classNames from 'classnames';
// ### Children
import Highlighter from '../utilities/highlighter';
// ## Constants
import { APP_LAUNCHER_LINK } from '../../utilities/constants';
/**
* App Launcher Link component creates simple links to be used in "All Items" sections
*/
class AppLauncherLink extends React.Component {
// ### Display Name
// Always use the canonical component name as the React display name.
static displayName = APP_LAUNCHER_LINK;
// ### Prop Types
static propTypes = {
/**
* Contents of the link
*/
children: PropTypes.node,
/**
* Classes to be applied to the link
*/
className: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
PropTypes.string,
]),
/**
* The `href` attribute of the link. If the `onClick` callback is specified this URL will be prevented from changing the browser's location.
*/
href: PropTypes.string,
/**
* Callback for when the link is clicked. Passes back event and data object with href prop. Prevents click from changing browser's location if set.
*/
onClick: PropTypes.func,
/**
* Text used to highlight content in link
*/
search: PropTypes.string,
/**
* The title for the link. If not provided it will attempt to use child content if that content is a string.
*/
title: PropTypes.string,
};
// ### Default Props
static defaultProps = {
href: '#',
};
render() {
let { title } = this.props;
if (!title && typeof this.props.children === 'string') {
title = this.props.children;
}
return (
<a
href={this.props.href}
className={classNames('slds-truncate', this.props.className)}
onClick={(event) => {
if (this.props.href === '#') {
event.preventDefault();
}
if (this.props.onClick) {
event.preventDefault();
this.props.onClick(event, { href: this.props.href });
}
}}
title={title}
>
<Highlighter search={this.props.search}>
{this.props.children}
</Highlighter>
</a>
);
}
}
export default AppLauncherLink;