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

feat[float-button]: add clickOutAutoClose feature #39501

Merged
merged 13 commits into from Dec 14, 2022

Conversation

BoyYangzai
Copy link
Contributor

@BoyYangzai BoyYangzai commented Dec 12, 2022

[中文版模板 / Chinese template]

🤔 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • Component style update
  • TypeScript definition update
  • Bundle size optimization
  • Performance optimization
  • Enhancement feature
  • Internationalization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Workflow
  • Other (about what?)

🔗 Related issue link

💡 Background and solution

看到 issue 里有这个需求,就写了第一版
加了一个 clickOutAutoClose 开启这个功能
辛苦维护者 review 一下

📝 Changelog

Language Changelog
🇺🇸 English add clickOutAutoClose feature
🇨🇳 Chinese 添加 float-button 点击外侧自动关闭功能

☑️ Self-Check before Merge

⚠️ Please check all items below before requesting a reviewing. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • TypeScript definition is updated/provided or not needed
  • Changelog is provided or not needed

@li-jia-nan
Copy link
Member

feature branch please

@BoyYangzai BoyYangzai changed the base branch from master to feature December 12, 2022 16:46
@li-jia-nan
Copy link
Member

冲突了哈,而且把 master 的 diff 带过来了,建议清理一下分支,把 feature 作为上游分支

@BoyYangzai
Copy link
Contributor Author

冲突了哈,而且把 master 的 diff 带过来了,建议清理一下分支,把 feature 作为上游分支

好的

@github-actions
Copy link
Contributor

github-actions bot commented Dec 12, 2022

@BoyYangzai
Copy link
Contributor Author

BoyYangzai commented Dec 12, 2022

@li-jia-nan @Thulof 按照要求修改了一版,并修复了一个错误-点击其他地方自动关闭时,其他地方不包括 gruop 里展开的元素

@li-jia-nan
Copy link
Member

li-jia-nan commented Dec 13, 2022

大概看了一下,你的实现太复杂了,这个需求应该用不到 while 循环,我提供一个思路:

const onClick = useCallback(
  (e: MouseEvent) => {
    const target = e.target;
    if (floatButtonGroupRef.current?.contains(target as Node)) {
      return;
    }
    if (clickOutAutoClose && trigger === 'click') {
      setOpen(false);
    }
  },
  [clickOutAutoClose, trigger],
);

useEffect(() => {
  document.addEventListener('click', onClick);
  return () => {
    document.removeEventListener('click', onClick);
  };
}, []);

大概思路就这样,用 contains 方法判断一下是不是就可以了?

@codecov
Copy link

codecov bot commented Dec 13, 2022

Codecov Report

Base: 100.00% // Head: 100.00% // No change to project coverage 👍

Coverage data is based on head (fcb3ff8) compared to base (11a5866).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff            @@
##           feature    #39501   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          556       557    +1     
  Lines         9562      9593   +31     
  Branches      2711      2713    +2     
=========================================
+ Hits          9562      9593   +31     
Impacted Files Coverage Δ
components/float-button/FloatButtonGroup.tsx 100.00% <100.00%> (ø)
components/select/index.tsx 100.00% <0.00%> (ø)
components/watermark/index.tsx 100.00% <0.00%> (ø)
components/style/compact-item.tsx 100.00% <0.00%> (ø)
components/config-provider/index.tsx 100.00% <0.00%> (ø)
components/config-provider/context.tsx 100.00% <0.00%> (ø)
components/style/compact-item-vertical.tsx 100.00% <0.00%> (ø)
components/watermark/useMutationObserver.ts 100.00% <0.00%> (ø)
components/watermark/utils.ts 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@MadCcc
Copy link
Member

MadCcc commented Dec 13, 2022

Please provide test case

@MadCcc
Copy link
Member

MadCcc commented Dec 13, 2022

可以作为默认能力,不提供 API

@BoyYangzai
Copy link
Contributor Author

可以作为默认能力,不提供 API

或许作为api拓展性更强一点?将点击外侧自动关闭默认设置为true,可以关闭?

@BoyYangzai
Copy link
Contributor Author

@li-jia-nan @Thulof @MadCcc
第一次知道这个api哈哈哈,用上了
修改如下:
1.循环逻辑使用 contains 代替,补充上了点击在 floatButtonGroupRef,但是不在 floatButtonRef中的case
2.clickOutAutoClose 默认在click下开启,可以设置false关闭
3.hover 使用 useMemo 拿到 hoverTypeAction属性

 const hoverTypeAction = {
    onMouseEnter() {
      setOpen(true);
      onOpenChange?.(true);
    },
    onMouseLeave() {
      setOpen(false);
      onOpenChange?.(false);
    },
  };
+  const hoverAction = useMemo(() => {
+    if (trigger === 'hover') {
+      return hoverTypeAction
+    }
+  }, [trigger]);

  const openChange = () => {
    setOpen((prevState) => {
      onOpenChange?.(!prevState);
      return !prevState;
    });
  };

  const onClick = useCallback(
    (e: MouseEvent) => {
      if (trigger !== 'click') return;
      const target = e.target;
      if (floatButtonGroupRef.current!.contains(target as Node)) {
+      if (floatButtonRef.current!.contains(target as Node)) {
+      openChange()
+      return;
        } else {
          return;
        }
      }
      if (clickOutAutoClose) {
        setOpen(false);
      }
    },
    [clickOutAutoClose, trigger],
  );

  useEffect(() => {
    document.addEventListener('click', onClick);
    return () => {
      document.removeEventListener('click', onClick);
    };

@MadCcc
Copy link
Member

MadCcc commented Dec 13, 2022

可以作为默认能力,不提供 API

或许作为api拓展性更强一点?将点击外侧自动关闭默认设置为true,可以关闭?

参考一下所有有打开弹层的组件,比如 Select、Dropdown,默认都是点击其他区域可以关闭。这个行为可以保持一致,先不提供 API 也 OK

@MadCcc
Copy link
Member

MadCcc commented Dec 13, 2022

另外测试还是要补补全

@BoyYangzai
Copy link
Contributor Author

另外测试还是要补补全

👌

@BoyYangzai
Copy link
Contributor Author

@li-jia-nan @MadCcc 加上了test,rename了一下名称,默认开启clickOutAutoClose,留下了关闭的api
辛苦review一下

@BoyYangzai
Copy link
Contributor Author

BoyYangzai commented Dec 13, 2022

@li-jia-nan @MadCcc 都已经OK了,辛苦最后review一次了(应该是最后一次 qwq 修复了lint

@BoyYangzai
Copy link
Contributor Author

BoyYangzai commented Dec 14, 2022

@li-jia-nan 这次应该没有问题了
辛苦最后review一次了

@li-jia-nan li-jia-nan merged commit e65a188 into ant-design:feature Dec 14, 2022
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 this pull request may close these issues.

None yet

4 participants