【问题标题】:Disable overlay when clicking anywhere on page单击页面上的任意位置时禁用覆盖
【发布时间】:2016-06-22 17:32:11
【问题描述】:

我有一个工具提示,可以在点击时显示和隐藏。当用户点击页面上的anywhere时,我想隐藏工具提示。我怎样才能做到这一点?

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'));

【问题讨论】:

    标签: reactjs react-bootstrap


    【解决方案1】:

    您必须将两个 props 传递给您的 Overlay 组件才能实现这一点,rootClose={true}onHide={() =&gt; this.setState({show: false})}

    应该是这样的

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

    full working example

    【讨论】:

    • 您在我弄清楚并发布我自己的答案前几秒钟发布了 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 2018-08-24
    相关资源
    最近更新 更多