Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[useClickAway][Antd Input] 在 Antd Input 组件使用 useClickAway 页面报错 #2702

Open
justsso1 opened this issue Dec 17, 2024 · 1 comment · May be fixed by #2723
Open

[useClickAway][Antd Input] 在 Antd Input 组件使用 useClickAway 页面报错 #2702

justsso1 opened this issue Dec 17, 2024 · 1 comment · May be fixed by #2723

Comments

@justsso1
Copy link

报错:

image

最小可复现代码:

import { useClickAway } from 'ahooks';
import { Input } from 'antd';
import { useRef } from 'react';

const { TextArea } = Input;

const Comp = () => {
  const ref = useRef(null);

  useClickAway(() => {
    console.log('ClickAway');
  }, ref);

  return <TextArea ref={ref} />;
};

出错的代码位置是 packages/hooks/src/utils/getDocumentOrShadow.ts:

const checkIfAllInShadow = (targets: BasicTarget[]): boolean => {
  return targets.every((item) => {
    const targetElement = getTargetElement(item);
    if (!targetElement) return false;
    if (targetElement.getRootNode() instanceof ShadowRoot) return true; // 就是这里
    return false;
  });
};

使用原生的input元素是没有问题,但是使用 antd 的Input或者 TextArea 都会有问题。

@yangliguo7
Copy link

这里的 TextArea 的ref 的current 不是一个真实的dom 。所以解析失败了。

按这个修改下

return <div ref={ref}><TextArea /></div>

Wxh16144 added a commit to Wxh16144-forks/hooks that referenced this issue Jan 18, 2025
@Wxh16144 Wxh16144 linked a pull request Jan 18, 2025 that will close this issue
18 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants