Skip to content

Commit 2023036

Browse files
nlroweeddywashere
authored andcommitted
feat(Tooltip): enable target element option (#356)
* Ignore intellij and log files. * Add object type to tooltip target. Changed the tooltip target property to also allow for DOM elements. * Fix indents. * Change tether config to use target retrieved for event listeners. * Pass getTarget function instead of this._target Pass getTarget function instead of the target retrieved in componentDidMount as it is not always available at render.
1 parent 8ad5379 commit 2023036

4 files changed

Lines changed: 41 additions & 5 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules
33
/dist
44
/build
55
/lib
6+
.idea
7+
npm-debug.log

docs/lib/Components/TooltipsPage.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ export default class TooltipsPage extends React.Component {
3434
// boolean to control the state of the tooltip
3535
toggle: PropTypes.func,
3636
// callback for toggling isOpen in the controlling component
37-
target: PropTypes.string.isRequired,
38-
// target div ID, popover is attached to this element
37+
target: PropTypes.oneOfType([
38+
PropTypes.string,
39+
PropTypes.object
40+
]).isRequired,
41+
// target element or element ID, popover is attached to this element
3942
tether: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),
4043
// optionally overide tether config http://tether.io/#options
4144
tetherRef: PropType.function,

src/Tooltip.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { getTetherAttachments, tetherAttachements, mapToCssModules } from './uti
77
const { PropTypes } = React;
88
const propTypes = {
99
placement: React.PropTypes.oneOf(tetherAttachements),
10-
target: PropTypes.string.isRequired,
10+
target: PropTypes.oneOfType([
11+
PropTypes.string,
12+
PropTypes.object
13+
]).isRequired,
1114
isOpen: PropTypes.bool,
1215
disabled: PropTypes.bool,
1316
tether: PropTypes.object,
@@ -52,6 +55,7 @@ class Tooltip extends React.Component {
5255
super(props);
5356

5457
this.addTargetEvents = this.addTargetEvents.bind(this);
58+
this.getTarget = this.getTarget.bind(this);
5559
this.getTetherConfig = this.getTetherConfig.bind(this);
5660
this.handleDocumentClick = this.handleDocumentClick.bind(this);
5761
this.removeTargetEvents = this.removeTargetEvents.bind(this);
@@ -65,7 +69,7 @@ class Tooltip extends React.Component {
6569
}
6670

6771
componentDidMount() {
68-
this._target = document.getElementById(this.props.target);
72+
this._target = this.getTarget();
6973
this.addTargetEvents();
7074
}
7175

@@ -114,12 +118,20 @@ class Tooltip extends React.Component {
114118
return delay;
115119
}
116120

121+
getTarget() {
122+
const { target } = this.props;
123+
if (typeof target === 'object') {
124+
return target;
125+
}
126+
return document.getElementById(target);
127+
}
128+
117129
getTetherConfig() {
118130
const attachments = getTetherAttachments(this.props.placement);
119131
return {
120132
...defaultTetherConfig,
121133
...attachments,
122-
target: '#' + this.props.target,
134+
target: this.getTarget,
123135
...this.props.tether
124136
};
125137
}

src/__tests__/Tooltip.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ describe('Tooltip', () => {
7272
wrapper.detach();
7373
});
7474

75+
it('should render with target object', () => {
76+
isOpen = true;
77+
const wrapper = mount(
78+
<Tooltip target={document.getElementById('target')} isOpen={isOpen} toggle={toggle}>
79+
Tooltip Content
80+
</Tooltip>,
81+
{ attachTo: container }
82+
);
83+
const instance = wrapper.instance();
84+
const tooltips = document.getElementsByClassName('tooltip');
85+
86+
expect(ReactDOM.findDOMNode(instance)).toBe(null);
87+
expect(document.body.querySelectorAll('.tooltip.show').length).toBe(1);
88+
expect(target.className.indexOf('bs-tether-target') > -1).toBe(true);
89+
expect(tooltips.length).toBe(1);
90+
expect(tooltips[0].textContent).toBe('Tooltip Content');
91+
wrapper.detach();
92+
});
93+
7594
it('should toggle isOpen', () => {
7695
const wrapper = mount(
7796
<Tooltip target="target" isOpen={isOpen} toggle={toggle}>

0 commit comments

Comments
 (0)