Chakra UI 和 React |弹出窗口触发器不起作用

发布于 2025-01-15 16:03:39 字数 3412 浏览 0 评论 0原文

示例

每当我单击此配置文件组件时;弹出窗口将被触发。但是,我将 ...code 的子项包装到此 中因此,弹出窗口不起作用,我只是包装子组件,因为我希望 ProfileComponent 可重用

这是我的整个代码

Profile组件

interface IProfileProps extends BoxProps {
  query: boolean;
}

const Profile = ({ query }: IProfileProps) => {
  return (
    <Box px={query ? "0px" : "5"} w="full" position="absolute" bottom="10">
      {" "}
      <HStack
        p="10px"
        borderRadius="full"
        w="full"
        h="full"
        // spacing="20px"
        cursor="pointer"
        _hover={{ bg: "rgba(255, 233, 206, 0.45)" }}
      >
        <Image
          m={query ? "auto" : "0"}
          boxSize="45px"
          borderRadius="full"
          alt="user profile"
          src="https://i.pinimg.com/originals/32/38/6c/32386c72c7f2a8b5c1a10fc51c149cb1.jpg"
        />
        <VStack
          spacing="5px"
          alignItems="baseline"
          w="full"
          h="full"
          display={{ lg: query ? "none" : "block" }}
        >
          <Text fontWeight="bold">Mosh Ontong</Text>

          <HStack color="green.400">
            <Icon as={MdVerifiedUser}></Icon>
            <Text fontWeight="bold" fontSize="0.8rem">
              Verified
            </Text>
          </HStack>
        </VStack>
        <Icon
          as={HiDotsVertical}
          boxSize="20px"
          display={{ lg: query ? "none" : "block" }}
        />
      </HStack>{" "}
    </Box>
  );
};

然后是我的 Profile Popover 组件

const DesktopProfile = ({ query }: IProfileProps) => {
  const initRef = React.useRef<HTMLButtonElement>(null);
  return (
    <Popover placement="right" initialFocusRef={initRef}>
  {/* //Focus here */}
      <PopoverTrigger>
     {/* //So this is my Profile Component,
 when I click this profile component it failed to trigger the popover */}
        <Profile query={query} />
      </PopoverTrigger>

      <PopoverContent color="white" bg="blue.800" borderColor="blue.800">
        <PopoverHeader pt={4} fontWeight="bold" border="0">
          Mosh Ontong
        </PopoverHeader>
        <PopoverArrow />
        <PopoverCloseButton />
        <PopoverBody>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
          eiusmod tempor incididunt ut labore et dolore.
        </PopoverBody>
        <PopoverFooter
          border="0"
          d="flex"
          alignItems="center"
          justifyContent="space-between"
          pb={4}
        >
          <Box fontSize="sm">Step 2 of 4</Box>
          <ButtonGroup size="sm">
            <Button colorScheme="green">Setup Email</Button>
            <Button colorScheme="blue" ref={initRef}>
              Next
            </Button>
          </ButtonGroup>
        </PopoverFooter>
      </PopoverContent>
    </Popover>
  );
};

我的目标是 popovertrigger 标签接受我的 ProfileComponent 作为 Popover 的触发器

Example

Whenever, I click this profile component; the popover will be trigger. But, I wrap the children of <PopoverTrigger>...code</Popovertrigger> into this <PopoverTrigger><ProfileComponent/></Popovertrigger> Because of this, the popover is not working, I just wrap the children because I want the ProfileComponent is reusable

This is my whole code

Profile Component

interface IProfileProps extends BoxProps {
  query: boolean;
}

const Profile = ({ query }: IProfileProps) => {
  return (
    <Box px={query ? "0px" : "5"} w="full" position="absolute" bottom="10">
      {" "}
      <HStack
        p="10px"
        borderRadius="full"
        w="full"
        h="full"
        // spacing="20px"
        cursor="pointer"
        _hover={{ bg: "rgba(255, 233, 206, 0.45)" }}
      >
        <Image
          m={query ? "auto" : "0"}
          boxSize="45px"
          borderRadius="full"
          alt="user profile"
          src="https://i.pinimg.com/originals/32/38/6c/32386c72c7f2a8b5c1a10fc51c149cb1.jpg"
        />
        <VStack
          spacing="5px"
          alignItems="baseline"
          w="full"
          h="full"
          display={{ lg: query ? "none" : "block" }}
        >
          <Text fontWeight="bold">Mosh Ontong</Text>

          <HStack color="green.400">
            <Icon as={MdVerifiedUser}></Icon>
            <Text fontWeight="bold" fontSize="0.8rem">
              Verified
            </Text>
          </HStack>
        </VStack>
        <Icon
          as={HiDotsVertical}
          boxSize="20px"
          display={{ lg: query ? "none" : "block" }}
        />
      </HStack>{" "}
    </Box>
  );
};

Then my Profile Popover Component

const DesktopProfile = ({ query }: IProfileProps) => {
  const initRef = React.useRef<HTMLButtonElement>(null);
  return (
    <Popover placement="right" initialFocusRef={initRef}>
  {/* //Focus here */}
      <PopoverTrigger>
     {/* //So this is my Profile Component,
 when I click this profile component it failed to trigger the popover */}
        <Profile query={query} />
      </PopoverTrigger>

      <PopoverContent color="white" bg="blue.800" borderColor="blue.800">
        <PopoverHeader pt={4} fontWeight="bold" border="0">
          Mosh Ontong
        </PopoverHeader>
        <PopoverArrow />
        <PopoverCloseButton />
        <PopoverBody>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
          eiusmod tempor incididunt ut labore et dolore.
        </PopoverBody>
        <PopoverFooter
          border="0"
          d="flex"
          alignItems="center"
          justifyContent="space-between"
          pb={4}
        >
          <Box fontSize="sm">Step 2 of 4</Box>
          <ButtonGroup size="sm">
            <Button colorScheme="green">Setup Email</Button>
            <Button colorScheme="blue" ref={initRef}>
              Next
            </Button>
          </ButtonGroup>
        </PopoverFooter>
      </PopoverContent>
    </Popover>
  );
};

My goal is the popovertrigger tag accept my ProfileComponent as the trigger of Popover

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

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

发布评论

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

评论(2

过度放纵 2025-01-22 16:03:39

我解决了我刚刚包装我的 ProfileComponent 的问题

 <PopoverTrigger>
        {/* I wrapped my Profile Component*/}
        <Box px={query ? "0px" : "5"} w="full" position="absolute" bottom="10">
          <Profile query={query}></Profile>
        </Box>
      </PopoverTrigger>

I solved the problem I just wrapped my ProfileComponent

 <PopoverTrigger>
        {/* I wrapped my Profile Component*/}
        <Box px={query ? "0px" : "5"} w="full" position="absolute" bottom="10">
          <Profile query={query}></Profile>
        </Box>
      </PopoverTrigger>
鲜血染红嫁衣 2025-01-22 16:03:39

用 Box 组件包装你的组件,或者用 chakra ui 的forwardRef 包装你的 Profile 组件,这样他就可以使用你组件的 Ref

wrap your component by a Box component or wrap your Profile component by a forwardRef of chakra ui so he can use the Ref of your component

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