Skip to content

Instantly share code, notes, and snippets.

@bernardodiasc
Created March 31, 2017 16:55
Show Gist options
  • Save bernardodiasc/15874f7fa1c02163bcd0776bbbfd0694 to your computer and use it in GitHub Desktop.
Save bernardodiasc/15874f7fa1c02163bcd0776bbbfd0694 to your computer and use it in GitHub Desktop.
Tooltip component
import { compose, withState, onlyUpdateForKeys } from 'recompose';
import { themr } from 'react-css-themr';
import Tooltip from './Tooltip';
import style from './Tooltip.css';
export default compose(
themr('Tooltip', style),
withState('hover', 'onHover', false),
onlyUpdateForKeys(['hover']),
)(Tooltip);
import React, { PropTypes } from 'react';
import classNames from 'classnames';
const Tooltip = ({ children, text, position, hover, onHover, theme }) => {
const popup = classNames(theme.popup, theme[position]);
return (
<span
className={theme.tooltip}
onMouseEnter={() => onHover(true)}
onMouseLeave={() => onHover(false)}
>
{hover && <span className={popup}>{text}</span>}
{children || ''}
</span>
);
};
Tooltip.propTypes = {
children: PropTypes.node,
text: PropTypes.string,
position: PropTypes.oneOf([
'left',
'top',
'right',
'bottom',
]),
hover: PropTypes.bool,
onHover: PropTypes.func,
theme: PropTypes.shape({
tooltip: PropTypes.string,
popup: PropTypes.string,
left: PropTypes.string,
top: PropTypes.string,
right: PropTypes.string,
bottom: PropTypes.string,
}),
};
Tooltip.defaultProps = {
theme: {},
text: '',
position: 'left',
};
export default Tooltip;
import React from 'react';
import { shallow } from 'enzyme';
import Tooltip from './Tooltip.js';
const wrapper = shallow(<Tooltip />);
describe('(Component) Tooltip', () => {
it('renders without exploding', () => {
expect(wrapper).toHaveLength(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment