TooltipWrapper.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import React, { Component } from 'react';
  2. import { Tooltip } from 'reactstrap';
  3. // track id generation
  4. let idCounter = 0;
  5. // return unique id number
  6. const UUID = () => idCounter++;
  7. // reset to sync client/server rendering
  8. export const resetUUID = () => idCounter = 0;
  9. /**
  10. * Wrap an element and assign automatically an ID,
  11. * creates a handler to show/hide tooltips without
  12. * the hassle of creating new states and class methods.
  13. * Support only one child and simple text content.
  14. */
  15. class TooltipWrapper extends Component {
  16. // static propTypes { content: PropTypes.string }
  17. state = {
  18. _id: 'id4tooltip_' + UUID(),
  19. tooltipOpen: false
  20. };
  21. toggle = e => {
  22. this.setState({ tooltipOpen: !this.state.tooltipOpen });
  23. };
  24. render() {
  25. return [
  26. <Tooltip
  27. {...this.props}
  28. isOpen={this.state.tooltipOpen}
  29. toggle={this.toggle}
  30. target={this.state._id}
  31. placement={this.props.placement}
  32. key="1"
  33. >
  34. {this.props.content}
  35. </Tooltip>,
  36. React.cloneElement(React.Children.only(this.props.children), {
  37. id: this.state._id,
  38. key: '2'
  39. })
  40. ];
  41. }
  42. }
  43. export default TooltipWrapper;