Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Tooltip): Tooltip target element option #356

Merged
merged 7 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules
/dist
/build
/lib
.idea
npm-debug.log
7 changes: 5 additions & 2 deletions docs/lib/Components/TooltipsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ export default class TooltipsPage extends React.Component {
// boolean to control the state of the tooltip
toggle: PropTypes.func,
// callback for toggling isOpen in the controlling component
target: PropTypes.string.isRequired,
// target div ID, popover is attached to this element
target: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object
]).isRequired,
// target element or element ID, popover is attached to this element
tether: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
// optionally overide tether config http://tether.io/#options
tetherRef: PropType.function,
Expand Down
18 changes: 15 additions & 3 deletions src/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { getTetherAttachments, tetherAttachements, mapToCssModules } from './uti
const { PropTypes } = React;
const propTypes = {
placement: React.PropTypes.oneOf(tetherAttachements),
target: PropTypes.string.isRequired,
target: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object
]).isRequired,
isOpen: PropTypes.bool,
disabled: PropTypes.bool,
tether: PropTypes.object,
Expand Down Expand Up @@ -52,6 +55,7 @@ class Tooltip extends React.Component {
super(props);

this.addTargetEvents = this.addTargetEvents.bind(this);
this.getTarget = this.getTarget.bind(this);
this.getTetherConfig = this.getTetherConfig.bind(this);
this.handleDocumentClick = this.handleDocumentClick.bind(this);
this.removeTargetEvents = this.removeTargetEvents.bind(this);
Expand All @@ -65,7 +69,7 @@ class Tooltip extends React.Component {
}

componentDidMount() {
this._target = document.getElementById(this.props.target);
this._target = this.getTarget();
this.addTargetEvents();
}

Expand Down Expand Up @@ -114,12 +118,20 @@ class Tooltip extends React.Component {
return delay;
}

getTarget() {
const { target } = this.props;
if (typeof target === 'object') {
return target;
}
return document.getElementById(target);
}

getTetherConfig() {
const attachments = getTetherAttachments(this.props.placement);
return {
...defaultTetherConfig,
...attachments,
target: '#' + this.props.target,
target: this.getTarget,
...this.props.tether
};
}
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/Tooltip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ describe('Tooltip', () => {
wrapper.detach();
});

it('should render with target object', () => {
isOpen = true;
const wrapper = mount(
<Tooltip target={document.getElementById('target')} isOpen={isOpen} toggle={toggle}>
Tooltip Content
</Tooltip>,
{ attachTo: container }
);
const instance = wrapper.instance();
const tooltips = document.getElementsByClassName('tooltip');

expect(ReactDOM.findDOMNode(instance)).toBe(null);
expect(document.body.querySelectorAll('.tooltip.show').length).toBe(1);
expect(target.className.indexOf('bs-tether-target') > -1).toBe(true);
expect(tooltips.length).toBe(1);
expect(tooltips[0].textContent).toBe('Tooltip Content');
wrapper.detach();
});

it('should toggle isOpen', () => {
const wrapper = mount(
<Tooltip target="target" isOpen={isOpen} toggle={toggle}>
Expand Down