`popover'(作为react组件)用`

发布于 2025-02-12 00:10:27 字数 1581 浏览 3 评论 0 原文

我正在尝试将丰富的React组件作为弹出内容创建。 如果我将示例与简单 const popover )一切正常。

问题,

但是自定义反应组件无法定位。它出现在屏幕的左上角


const MyPopover = React.forwardRef((props, ref) => {
    return <Popover ref={ref} placement={"bottom"}>
        <Popover.Header as="h3">
            <Form.Control></Form.Control>
        </Popover.Header>
        <Popover.Body>
            <strong>Holy guacamole!</strong> Check this info.
        </Popover.Body>
    </Popover>
})

const PopoverChooser = ({children,  container}) => {
    const _refTarget = useRef(null)

    return <>
          <OverlayTrigger
              trigger="click"
              placement="bottom"
              overlay={<MyPopover ref={_refTarget}/>}
              target={_refTarget.current}
          >
              {children} 
          </OverlayTrigger>
    </>
}
export default PopoverChooser;

如您所见,我尝试使用 ref 's,但这无济于事。

问题

  1. 如何将弹出键链链接到目标按钮(如下拉按钮中的图像中,在代码中为 {children {children} )。
  2. 还是我应该手动定位 mypopover (通过检查按钮ID并分配位置等。等等?)
  3. 完全不同的方法来挖掘..?

I'm trying to create rich React component as popover content.
If I use example with simple const popover (https://react-bootstrap.netlify.app/components/overlays/#examples-1) everything works fine.

Problem

But custom react component fails to position itself. It appears on top left of the screen
enter image description here


const MyPopover = React.forwardRef((props, ref) => {
    return <Popover ref={ref} placement={"bottom"}>
        <Popover.Header as="h3">
            <Form.Control></Form.Control>
        </Popover.Header>
        <Popover.Body>
            <strong>Holy guacamole!</strong> Check this info.
        </Popover.Body>
    </Popover>
})

const PopoverChooser = ({children,  container}) => {
    const _refTarget = useRef(null)

    return <>
          <OverlayTrigger
              trigger="click"
              placement="bottom"
              overlay={<MyPopover ref={_refTarget}/>}
              target={_refTarget.current}
          >
              {children} 
          </OverlayTrigger>
    </>
}
export default PopoverChooser;

As you can see, I'v tried to use ref's, but it's doesn't help.

Question

  1. How can it link popover to target button (in image as dropdown button and in code as {children}).
  2. Or should I position MyPopover manually (by checking button ID and assigning position.. etc.?)
  3. Completely different approach to dig in..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇到 2025-02-19 00:10:27

您转发裁判的方法是正确的。您实际忘记的是注入道具。根据文档:

组件和组件不会定位自己。
相反(或)组件,注入ref和
样式道具

httpps://react-bootstrap.netlify.app/components/components/poverlays/poverlays/poverlays/poverlays/poverview <<<<<<<<<<<<<<<< /a>

因此,您需要做的是传播这样的道具:

const MyPopover = React.forwardRef((props, ref) => {
  return (
    <Popover ref={ref} {...props}>

https:// codesandbox box .io/s/s/trusting-sid-0050g9

Your approach to forward the ref was right. What you actually forgot is to also inject props. According to the documentation:

The and components do not position themselves.
Instead the (or ) components, inject ref and
style props
.

https://react-bootstrap.netlify.app/components/overlays/#overview

So what you need to do is to spread the props like this:

const MyPopover = React.forwardRef((props, ref) => {
  return (
    <Popover ref={ref} {...props}>

https://codesandbox.io/s/trusting-sid-0050g9

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文