使用shadcn+NextJS时,下拉菜单触发Dialog无响应问题求助
NextJS + shadcn:下拉菜单中点击删除按钮仅关闭菜单不弹窗的解决办法
我在用NextJS搭配shadcn组件库时遇到个问题——从下拉菜单里点「Delete Trainee」想打开确认弹窗,结果菜单直接关了,弹窗根本没出来。我猜可能是ref、状态或者AlertDialog的位置有问题,相关代码如下:
return ( <AlertDialog> <DropdownMenu> <DropdownMenuTrigger asChild> <Button variant="ghost" className="w-8 h-8 p-0"> <span className="sr-only">Open Menu</span> <FiMoreHorizontal className="w-4 h-4" /> </Button> </DropdownMenuTrigger> <DropdownMenuContent align="center"> <DropdownMenuSeparator /> <DropdownMenuItem className="justify-start cursor-pointer"> <Link href={`/dashboard/profile/trainee/${traineeId}`}> Profile </Link> </DropdownMenuItem> <DropdownMenuItem className="justify-start cursor-pointer"> <Link href={`/dashboard/profile/trainee/${traineeId}/escort/add-escort`}> Add Escort </Link> </DropdownMenuItem> <DropdownMenuItem className="justify-start cursor-pointer"> <AlertDialogTrigger> Delete Trainee </AlertDialogTrigger> </DropdownMenuItem> </DropdownMenuContent> </DropdownMenu> <AlertDialogContent> <AlertDialogHeader> <AlertDialogTitle>Are you sure you wish to delete the trainee?</AlertDialogTitle> <AlertDialogDescription> This will delete the trainee and all of his data. </AlertDialogDescription> </AlertDialogHeader> <AlertDialogFooter> <AlertDialogCancel>Cancel</AlertDialogCancel> <AlertDialogAction onClick={() => deleteTrainee(traineeId)}>Continue</AlertDialogAction> </AlertDialogFooter> </AlertDialogContent> </AlertDialog> )
问题根源
DropdownMenuItem点击时会触发菜单关闭的默认行为,这个行为会先执行,直接打断了AlertDialogTrigger的触发逻辑;再加上AlertDialog把DropdownMenu包裹在内部的嵌套结构,进一步导致事件传递异常。
修复步骤
- 调整组件层级:把AlertDialog和DropdownMenu改成平级,不要互相嵌套
- 手动控制菜单与弹窗:用状态管理弹窗的开关,给删除按钮添加点击事件,先阻止菜单默认关闭,手动关闭菜单后再打开弹窗
修改后的完整代码
'use client'; import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'; import { AlertDialog, AlertDialogContent, AlertDialogHeader, AlertDialogTitle, AlertDialogDescription, AlertDialogFooter, AlertDialogCancel, AlertDialogAction } from '@/components/ui/alert-dialog'; import { Link } from 'next/link'; import { FiMoreHorizontal } from 'react-icons/fi'; export default function TraineeActions({ traineeId, deleteTrainee }) { // 管理弹窗的打开状态 const [isDialogOpen, setIsDialogOpen] = useState(false); // 存DropdownMenu的ref,用来手动关闭菜单 const [menuRef, setMenuRef] = useState(null); const handleDeleteClick = (e) => { // 阻止DropdownMenuItem的默认关闭行为 e.stopPropagation(); // 手动调用菜单的close方法关闭下拉菜单 menuRef?.close(); // 打开确认弹窗 setIsDialogOpen(true); }; return ( <> <DropdownMenu ref={setMenuRef}> <DropdownMenuTrigger asChild> <Button variant="ghost" className="w-8 h-8 p-0"> <span className="sr-only">Open Menu</span> <FiMoreHorizontal className="w-4 h-4" /> </Button> </DropdownMenuTrigger> <DropdownMenuContent align="center"> <DropdownMenuSeparator /> <DropdownMenuItem className="justify-start cursor-pointer"> <Link href={`/dashboard/profile/trainee/${traineeId}`}> Profile </Link> </DropdownMenuItem> <DropdownMenuItem className="justify-start cursor-pointer"> <Link href={`/dashboard/profile/trainee/${traineeId}/escort/add-escort`}> Add Escort </Link> </DropdownMenuItem> <DropdownMenuItem className="justify-start cursor-pointer text-red-600" onClick={handleDeleteClick} > Delete Trainee </DropdownMenuItem> </DropdownMenuContent> </DropdownMenu> <AlertDialog open={isDialogOpen} onOpenChange={setIsDialogOpen}> <AlertDialogContent> <AlertDialogHeader> <AlertDialogTitle>Are you sure you wish to delete the trainee?</AlertDialogTitle> <AlertDialogDescription> This will delete the trainee and all of his data. </AlertDialogDescription> </AlertDialogHeader> <AlertDialogFooter> <AlertDialogCancel onClick={() => setIsDialogOpen(false)}>Cancel</AlertDialogCancel> <AlertDialogAction onClick={() => { deleteTrainee(traineeId); setIsDialogOpen(false); }}>Continue</AlertDialogAction> </AlertDialogFooter> </AlertDialogContent> </AlertDialog> </> ); }
关键修改说明
- 移除了AlertDialog对DropdownMenu的包裹,改成平级结构,避免事件传递被拦截
- 用
useState控制AlertDialog的open属性,替代原有的AlertDialogTrigger触发方式 - 通过DropdownMenu的ref手动调用
close()方法关闭菜单,同时用e.stopPropagation()阻止默认的关闭行为,确保弹窗能正常触发 - 优化了弹窗关闭逻辑,点击取消或确认后都能正确关闭弹窗
内容的提问来源于stack exchange,提问作者steve123
相关产品推荐
相关产品推荐

