Tooltips

Tooltips are built with https://popper.js.org/ via https://github.com/souporserious/react-popper.

Tooltips - 图1

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Tooltip } from 'reactstrap';
  4. import { mapToCssModules } from '../../../src/utils'
  5. import classNames from 'classnames'
  6. export default class Example extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.toggle = this.toggle.bind(this);
  10. this.state = {
  11. tooltipOpen: false
  12. };
  13. }
  14. toggle() {
  15. this.setState({
  16. tooltipOpen: !this.state.tooltipOpen
  17. });
  18. }
  19. render() {
  20. const classes = 'tooltip-inner'
  21. return (
  22. <div>
  23. <p>Somewhere in here is a <span style={{textDecoration: "underline", color:"blue"}} href="#" id="TooltipExample">tooltip</span>.</p>
  24. <Tooltip placement="right" isOpen={this.state.tooltipOpen} target="TooltipExample" toggle={this.toggle}>
  25. Hello world!
  26. </Tooltip>
  27. </div>
  28. );
  29. }
  30. }

Properties

  1. Tooltip.propTypes = {
  2. // space separated list of triggers (e.g. "click hover focus")
  3. trigger: PropTypes.string,
  4. // boundaries for popper, can be scrollParent, window, viewport, or any DOM element
  5. boundariesElement: PropTypes.oneOfType([PropTypes.string, DOMElement]),
  6. // boolean to control the state of the tooltip
  7. isOpen: PropTypes.bool,
  8. hideArrow: PropTypes.bool,
  9. // callback for toggling isOpen in the controlling component. It will receive an object with info about the event that triggered it
  10. toggle: PropTypes.func,
  11. // target element or element ID, popover is attached to this element
  12. target: PropTypes.oneOfType([
  13. PropTypes.string,
  14. PropTypes.func,
  15. DOMElement, // instanceof Element (https://developer.mozilla.org/en-US/docs/Web/API/Element)
  16. ]).isRequired,
  17. // Where to inject the popper DOM node, default to body
  18. container: PropTypes.oneOfType([PropTypes.string, PropTypes.func, DOMElement]),
  19. // optionally override show/hide delays - default { show: 0, hide: 250 }
  20. delay: PropTypes.oneOfType([
  21. PropTypes.shape({ show: PropTypes.number, hide: PropTypes.number }),
  22. PropTypes.number
  23. ]),
  24. className: PropTypes.string,
  25. // Apply class to the inner-tooltip
  26. innerClassName: PropTypes.string,
  27. // Apply class to the arrow-tooltip ('arrow' by default)
  28. arrowClassName: PropTypes.string,
  29. // optionally hide tooltip when hovering over tooltip content - default true
  30. autohide: PropTypes.bool,
  31. // convenience attachments for popover
  32. placement: PropTypes.oneOf([
  33. 'auto',
  34. 'auto-start',
  35. 'auto-end',
  36. 'top',
  37. 'top-start',
  38. 'top-end',
  39. 'right',
  40. 'right-start',
  41. 'right-end',
  42. 'bottom',
  43. 'bottom-start',
  44. 'bottom-end',
  45. 'left',
  46. 'left-start',
  47. 'left-end',
  48. ]),
  49. // Custom modifiers that are passed to Popper.js, see https://popper.js.org/popper-documentation.html#modifiers
  50. modifiers: PropTypes.object,
  51. offset: PropTypes.oneOfType([
  52. PropTypes.string,
  53. PropTypes.number
  54. ]),
  55. // Custom ref handler that will be assigned to the "ref" of the <div> wrapping the tooltip elements
  56. innerRef: PropTypes.oneOfType([
  57. PropTypes.func,
  58. PropTypes.string,
  59. PropTypes.object
  60. ]),
  61. // Whether to show/hide the popover with a fade effect
  62. // (default: true)
  63. fade: PropTypes.bool,
  64. // Whether to flip the direction of the popover if too close to
  65. // the container edge
  66. // (default: true)
  67. flip: PropTypes.bool,
  68. }

Tooltip Disable Autohide

Tooltips - 图2

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Tooltip } from 'reactstrap';
  4. export default class Example extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.toggle = this.toggle.bind(this);
  8. this.state = {
  9. tooltipOpen: false
  10. };
  11. }
  12. toggle() {
  13. this.setState({
  14. tooltipOpen: !this.state.tooltipOpen
  15. });
  16. }
  17. render() {
  18. return (
  19. <div>
  20. <p>Sometimes you need to allow users to select text within a <span style={{textDecoration: "underline", color:"blue"}} href="#" id="DisabledAutoHideExample">tooltip</span>.</p>
  21. <Tooltip placement="top" isOpen={this.state.tooltipOpen} autohide={false} target="DisabledAutoHideExample" toggle={this.toggle}>
  22. Try to select this text!
  23. </Tooltip>
  24. </div>
  25. );
  26. }
  27. }

Tooltips List

Tooltips - 图3

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { Button, Tooltip } from 'reactstrap';
  4. class TooltipItem extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.toggle = this.toggle.bind(this);
  8. this.state = {
  9. tooltipOpen: false
  10. };
  11. }
  12. toggle() {
  13. this.setState({
  14. tooltipOpen: !this.state.tooltipOpen
  15. });
  16. }
  17. render() {
  18. return (
  19. <span>
  20. <Button className="mr-1" color="secondary" id={'Tooltip-' + this.props.id}>
  21. {this.props.item.text}
  22. </Button>
  23. <Tooltip placement={this.props.item.placement} isOpen={this.state.tooltipOpen} target={'Tooltip-' + this.props.id} toggle={this.toggle}>
  24. Tooltip Content!
  25. </Tooltip>
  26. </span>
  27. );
  28. }
  29. }
  30. class TooltipExampleMulti extends React.Component {
  31. constructor(props) {
  32. super(props);
  33. this.state = {
  34. tooltips: [
  35. {
  36. placement: 'top',
  37. text: 'Top'
  38. },
  39. {
  40. placement: 'bottom',
  41. text: 'Bottom'
  42. },
  43. {
  44. placement: 'left',
  45. text: 'Left'
  46. },
  47. {
  48. placement: 'right',
  49. text: 'Right'
  50. }
  51. ]
  52. };
  53. }
  54. render() {
  55. return (
  56. <div>
  57. { this.state.tooltips.map((tooltip, i) => {
  58. return <TooltipItem key={i} item={tooltip} id={i} />;
  59. })}
  60. </div>
  61. );
  62. }
  63. }
  64. export default TooltipExampleMulti;

Uncontrolled Tooltip

For the most basic use-case an uncontrolled component can provide the functionality wanted without the need to manage/control the state of the component. UncontrolledTooltip does not require isOpen nor toggle props to work.

Tooltips - 图4

  1. /* eslint react/no-multi-comp: 0, react/prop-types: 0 */
  2. import React from 'react';
  3. import { UncontrolledTooltip } from 'reactstrap';
  4. export default function Example() {
  5. return (
  6. <div>
  7. <p>Somewhere in here is a <span style={{textDecoration: "underline", color:"blue"}} href="#" id="UncontrolledTooltipExample">tooltip</span>.</p>
  8. <UncontrolledTooltip placement="right" target="UncontrolledTooltipExample">
  9. Hello world!
  10. </UncontrolledTooltip>
  11. </div>
  12. );
  13. }