单击页面上的任何位置时禁用叠加

时间:2016-06-22 17:32:12

标签: reactjs react-bootstrap

我有一个工具提示,我在点击时显示和隐藏。当用户点击页面上的任何地方时,我想隐藏工具提示。我怎么能做到这一点?

fiddle

var Alert = ReactBootstrap.Alert;
var Overlay = ReactBootstrap.Overlay;
var Tooltip = ReactBootstrap.Tooltip;
var Button = ReactBootstrap.Button;

const Example = React.createClass({
  getInitialState() {
    return { show: true };
  },

  toggle() {
    this.setState({ show: !this.state.show });
  },

  render() {
    const tooltip = <Tooltip>Tooltip overload!</Tooltip>;

    const sharedProps = {
      show: this.state.show,
      container: this,
      target: () => this.refs.target.getDOMNode()
    };

    return (
      <div style={{ height: 100, paddingLeft: 150, position: 'relative' }}>
        <Button ref="target" onClick={this.toggle}>
          Click me!
        </Button>

        <div>
                Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
        </div>

        <Overlay {...sharedProps} placement="left">
          { tooltip }
        </Overlay>
      </div>
    );
  }
});


React.render(<Example/>, document.getElementById('container'));

1 个答案:

答案 0 :(得分:1)

您必须将两个道具传递给您的叠加组件才能实现这一目标,rootClose={true}onHide={() => this.setState({show: false})}

看起来应该是这样的

<Overlay rootClose={true} onHide={() => this.setState({show: false})} {...sharedProps} placement="left">
          { tooltip }
        </Overlay>

full working example