You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击不同标签弹出相同模态框,如何按标签类型触发对应对话框?

问题原因

你的代码中,handleModalExport函数的条件判断逻辑存在错误。ExportTypes.some(e => e.id === 'AllRecommendations')是在检查数组中是否存在该ID,而这个判断始终为true(因为数组里确实包含这个元素),所以无论点击哪个菜单选项,都会执行第一个if分支的代码,弹出「All Recommendations」对应的模态框。

修复方案

需要把点击的菜单选项ID传递给处理函数,再根据这个ID判断应该弹出哪个模态框:

const ExportTypes: Array<{ label: string; id: string }> = [
 { label: 'All Recommendations', id: 'AllRecommendations' },
 { label: 'Entities By type', id: 'EntitiesBytype' },  
];

const handleModalExport = useCallback(
(selectedId: string) => {
  if (selectedId === 'AllRecommendations') {
      openModalDialog({
        title: t('Export all recommendations'),
        renderContent: () => <RecommendationExportModel />,
      });
    }
   else if(selectedId === 'EntitiesBytype') { 
      openModalDialog({
        title: t('Export all'),
        renderContent: () => <RecommendationExportEntitiesModel />,
      });
    }
  },
 [openModalDialog, t],
);

{ExportTypes.map((objType) => (
  <MenuItem key={objType.id} onClick={() => handleModalExport(objType.id)}>
     {t(objType.label)}
  </MenuItem>
))}
关键修改点
  • 让handleModalExport接收selectedId参数,代表当前点击的菜单ID
  • 将条件判断从检查数组是否包含该ID,改为直接对比传入的selectedId
  • 在MenuItem的onClick中,通过箭头函数把当前objType.id传递给handleModalExport

内容的提问来源于stack exchange,提问作者simran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:51:56