React Bootstrap - How to manually close OverlayTrigger

后端 未结 4 627
醉梦人生
醉梦人生 2021-01-01 17:03

I have an OverlayTrigger wrapping a Popover that contains some form inputs and a Button to save the data and close.

         


        
相关标签:
4条回答
  • 2021-01-01 17:42

    I solved this problem with the following code:

    import React from "react";
    import ReactDOM from "react-dom";
    import {
      Container,
      Popover,
      OverlayTrigger,
      Button,
      Row,
      Col
    } from "react-bootstrap";
    
    // Styles
    import "bootstrap/dist/css/bootstrap.css";
    
    const PopoverCustom = () => {
      let ref = React.useRef(null);
    
      const popover = (
        <Popover id="popover-basic">
          {/* <Popover.Title as="h3">Popover right</Popover.Title> */}
          <Popover.Content>
            And here's some <strong>amazing</strong> content. It's very engaging.
            right?
            <div className="mt-3 mb-1">
              <Button
                onClick={() => ref.handleHide()}
                size="sm"
                variant="outline-dark"
                className="pt-0 pb-0"
              >
                Hide Popover
              </Button>
            </div>
          </Popover.Content>
        </Popover>
      );
    
      return (
        <OverlayTrigger
          ref={r => (ref = r)}
          container={ref.current}
          trigger="click"
          placement="auto"
          overlay={popover}
          rootClose
        >
          <Button variant="dark">Show popover</Button>
        </OverlayTrigger>
      );
    };
    
    function App() {
      return (
        <Container className="ml-5 mt-5">
          <Row>
            <Col className="offset-sm-2" sm={8}>
              <PopoverCustom />
            </Col>
          </Row>
        </Container>
      );
    }
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    
    

    0 讨论(0)
  • 2021-01-01 17:45

    Hide function is not a public function on OverlayTrigger. Set <OverlayTrigger rootClose={true}... and then on your onClick event trigger call document.body.click().

    0 讨论(0)
  • 2021-01-01 17:50

    If someone is searching for a solution without "close" button but rather -> close it on second click here it is:

        <OverlayTrigger trigger="focus" placement="top" overlay={popover}>
            <a tabindex="0">Some text</a>
        </OverlayTrigger>
    
    0 讨论(0)
  • 2021-01-01 17:57

    Add a ref to the <OverlayTrigger ref="overlay"... element and call the hide method once the element has been rendered. Haven't tested it but should work:

    this.refs.overlay.hide();
    
    0 讨论(0)
提交回复
热议问题