如何在点击React TypeScript外部时关闭下拉菜单?

huangapple go评论74阅读模式
英文:

How to close dropdown on click outside React TypeScript?

问题

我有一个下拉菜单,需要在单个页面的多个位置使用。

当我打开任何一个下拉菜单时,其他的下拉菜单应该关闭。我该如何解决这个问题?

我的代码:
Code Sandbox

英文:

I have a dropdown & I need to use this dropdown in multiple places in single page.

When I open any dropdown then rest of the dropdown should be close. How can I fix this issue ?

My code:
Code Sandbox

答案1

得分: 1

以下是您提供的代码的翻译部分:

import React, { useState } from "react";

const Dropdown = () => {
  const [actionIsOpen, setActionIsOpen] = useState<boolean>(false);

  const actionList = [
    { id: "1", name: "One" },
    { id: "2", name: "Two" },
    { id: "3", name: "Three" },
    { id: "4", name: "Four" }
  ];

  const handleActionClick = () => {
    setActionIsOpen((prev) => !prev);
  };

  return (
    <>
      <div className="tw-relative">
        <span onMouseDown={handleActionClick} className="tw-cursor-pointer">
          <button onBlur={() => setActionIsOpen(false)}>Dropdown Click</button>
        </span>
        {actionIsOpen && (
          <div className="shadow tw-w-48 tw-absolute tw-top-2 tw-z-50 tw-left-4 tw-bg-white tw-pt-2">
            {actionList.map((val: any) => (
              <div
                key={val.id}
                className="tw-bg-white-100 hover:tw-bg-violet-100 hover:tw-text-violet-900 tw-cursor-pointer hover:tw-font-bold py-2 px-3 tw-text-gray-600"
                onMouseDown={() => alert(val.name)}
              >
                {val.name}
              </div>
            ))}
          </div>
        )}
      </div>
    </>
  );
};

export default Dropdown;

希望这对您有所帮助!如果您需要进一步的帮助,请随时提出。

英文:
import React, { useState } from &quot;react&quot;;

const Dropdown = () =&gt; {
  const [actionIsOpen, setActionIsOpen] = useState&lt;boolean&gt;(false);

  const actionList = [
    { id: &quot;1&quot;, name: &quot;One&quot; },
    { id: &quot;2&quot;, name: &quot;Two&quot; },
    { id: &quot;3&quot;, name: &quot;Three&quot; },
    { id: &quot;4&quot;, name: &quot;Four&quot; }
  ];

  const handleActionClick = () =&gt; {
    setActionIsOpen((prev) =&gt; !prev);
  };

  return (
    &lt;&gt;
      &lt;div className=&quot;tw-relative&quot;&gt;
        &lt;span onMouseDown={handleActionClick} className=&quot;tw-cursor-pointer&quot;&gt;
          &lt;button onBlur={() =&gt; setActionIsOpen(false)}&gt;Dropdown Click&lt;/button&gt;
        &lt;/span&gt;
        {actionIsOpen &amp;&amp; (
          &lt;div className=&quot;shadow tw-w-48 tw-absolute tw-top-2 tw-z-50 tw-left-4 tw-bg-white tw-pt-2&quot;&gt;
            {actionList.map((val: any) =&gt; (
              &lt;div
                key={val.id}
                className=&quot;tw-bg-white-100 hover:tw-bg-violet-100 hover:tw-text-violet-900 tw-cursor-pointer hover:tw-font-bold py-2 px-3 tw-text-gray-600&quot;
              onMouseDown={() =&gt; alert(val.name)}
              &gt;
                {val.name}
              &lt;/div&gt;
            ))}
          &lt;/div&gt;
        )}
      &lt;/div&gt;
    &lt;/&gt;
  );
};

export default Dropdown;

Something like this ?

huangapple
  • 本文由 发表于 2023年7月6日 19:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定