点击React下拉菜单中的锚点标签。

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

How to click on anchor tags inside React dropdown

问题

我在同一页中有一个下拉菜单,需要在多个地方使用它。

我的问题是:在打开任何下拉菜单后,我无法单击下拉菜单链接。当我尝试单击时,下拉菜单会消失。

Code Sandbox

英文:

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

My issue: after open any dropdown I'm not able to click on dropdown links. When I'm trying to click then dropdown is disappearing.

Code Sandbox

答案1

得分: 2

请查看 https://stackoverflow.com/questions/17769005/onclick-and-onblur-ordering-issue/57630197#57630197

a 组件上添加 onMouseDown 事件,并调用 e.preventDefault()

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 onClick={handleActionClick} onBlur={() => setActionIsOpen(false)} className="tw-cursor-pointer">
					<button>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">
								<a href="#" onMouseDown={(e) => e.preventDefault()}>
									{val.name}
								</a>
							</div>
						))}
					</div>
				)}
			</div>
		</>
	);
};

export default Dropdown;

codesandbox

英文:

See https://stackoverflow.com/questions/17769005/onclick-and-onblur-ordering-issue/57630197#57630197

Add the onMouseDown event on the a component and call e.preventDefault().

import React, { useState } from &#39;react&#39;;

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

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

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

	return (
		&lt;&gt;
			&lt;div className=&quot;tw-relative&quot;&gt;
				&lt;span onClick={handleActionClick} onBlur={() =&gt; setActionIsOpen(false)} className=&quot;tw-cursor-pointer&quot;&gt;
					&lt;button&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;&gt;
								&lt;a href=&quot;#&quot; onMouseDown={(e) =&gt; e.preventDefault()}&gt;
									{val.name}
								&lt;/a&gt;
							&lt;/div&gt;
						))}
					&lt;/div&gt;
				)}
			&lt;/div&gt;
		&lt;/&gt;
	);
};

export default Dropdown;

codesandbox

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

发表评论

匿名网友

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

确定