Skip to content

Commit 988b35f

Browse files
committed
Added tooltip component
1 parent 8a0f528 commit 988b35f

File tree

6 files changed

+131
-1
lines changed

6 files changed

+131
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
export const defaultCode = `
3+
class Demo extends React.Component {
4+
constructor(props) {
5+
super(props);
6+
}
7+
8+
render() {
9+
return (
10+
<div>
11+
{/* Preview Block 1 */}
12+
<PreviewBlock header="Default Tooltip">
13+
<Tooltip tooltipText='Normal Tooltip'>Look down</Tooltip>
14+
</PreviewBlock>
15+
{/* Preview Block 2 */}
16+
<PreviewBlock header="Tooltip at top">
17+
<Tooltip tooltipText='Top Tooltip' top>Look up</Tooltip>
18+
</PreviewBlock>
19+
</div>
20+
)
21+
}
22+
}
23+
`;

docs/client/components/common/DefaultCode/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export { defaultCode as RadioButtonGroupDefaultCode } from './RadioButtonGroup';
77
export { defaultCode as TextInputDefaultCode } from './TextInput';
88
export { defaultCode as SnackbarDefaultCode } from './Snackbar';
99
export { defaultCode as AvatarDefaultCode } from './Avatar';
10+
export { defaultCode as TooltipDefaultCode } from './Tooltip';

docs/client/components/common/componentList.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Snackbar from '../../../../src/snackbar';
1515
import SnackbarReadme from '../../../../src/snackbar/readMe.md';
1616
import Avatar from '../../../../src/avatar';
1717
import AvatarReadme from '../../../../src/avatar/readMe.md';
18+
import Tooltip from '../../../../src/avatar';
1819

1920
import {
2021
CardDefaultCode,
@@ -26,6 +27,7 @@ import {
2627
TextInputDefaultCode,
2728
SnackbarDefaultCode,
2829
AvatarDefaultCode,
30+
TooltipDefaultCode,
2931
} from './DefaultCode';
3032

3133
export const componentList = [
@@ -82,5 +84,11 @@ export const componentList = [
8284
docs: AvatarReadme,
8385
component: Avatar,
8486
defaultCode: AvatarDefaultCode,
87+
},
88+
{
89+
name: 'Tooltip',
90+
docs: AvatarReadme,
91+
component: Tooltip,
92+
defaultCode: TooltipDefaultCode,
8593
}
8694
];

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import RadioButtonGroup from './radioButtonGroup';
99
import TextInput from './textInput';
1010
import Snackbar from './snackbar';
1111
import Avatar from './avatar';
12+
import Tooltip from './tooltip';
1213

1314
export default {
1415
Button,
@@ -20,6 +21,7 @@ export default {
2021
TextInput,
2122
Snackbar,
2223
Avatar,
24+
Tooltip,
2325
};
2426

25-
// ReactDOM.render(<Avatar image="Https://placeimg.com/80/80/animals"/>, document.getElementById('index'));
27+
// ReactDOM.render(<Tooltip tooltipText='Tooltip' top>Here it is</Tooltip>, document.getElementById('index'));

src/tooltip/index.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { themr } from 'react-css-themr';
4+
import classnames from 'classnames';
5+
import defaultTheme from './theme.scss';
6+
7+
class Tooltip extends React.Component {
8+
state = { tooltipActive: false };
9+
10+
enableTooltip = () => {
11+
this.setState({
12+
tooltipActive: true,
13+
});
14+
}
15+
16+
disableTooltip = () => {
17+
this.setState({
18+
tooltipActive: false,
19+
});
20+
}
21+
22+
render() {
23+
const {
24+
children,
25+
className,
26+
theme,
27+
tooltipText,
28+
top,
29+
...other
30+
} = this.props;
31+
const classes = classnames(theme.tooltip, className, theme.baseContent);
32+
return (
33+
<div className={classes} {...other} onMouseEnter={this.enableTooltip} onMouseLeave={this.disableTooltip}>
34+
<div className={theme.contentWrapper}>
35+
{children}
36+
</div>
37+
{
38+
this.state.tooltipActive && (
39+
<div className={theme.tip}>
40+
<div className={classnames(theme.tooltipContent, { top })}>
41+
{tooltipText}
42+
</div>
43+
</div>
44+
)
45+
}
46+
</div>
47+
);
48+
}
49+
}
50+
51+
Tooltip.propTypes = {
52+
children: PropTypes.node,
53+
className: PropTypes.string,
54+
theme: PropTypes.object,
55+
image: PropTypes.string,
56+
alt: PropTypes.string,
57+
title: PropTypes.string,
58+
tooltipText: PropTypes.string.isRequired,
59+
top: PropTypes.bool,
60+
61+
};
62+
63+
Tooltip.defaultProps = {
64+
children: null,
65+
className: '',
66+
theme: defaultTheme,
67+
image: null,
68+
alt: '',
69+
title: '',
70+
top: undefined,
71+
};
72+
73+
export default themr('CBTooltip', defaultTheme)(Tooltip);

src/tooltip/theme.scss

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@import '../globals/theme';
2+
3+
:local(.baseContent) {
4+
cursor: pointer;
5+
}
6+
7+
:local(.tip) {
8+
position: relative;
9+
cursor: pointer;
10+
11+
}
12+
13+
:local(.tooltipContent) {
14+
background: $secondary-grey;
15+
font-size: 14px;
16+
position: absolute;
17+
top: 20px;
18+
padding: 3px 5px;
19+
color: $original-white;
20+
&.top {
21+
top: -60px;
22+
}
23+
}

0 commit comments

Comments
 (0)