如何根据标签类型显示模态对话框

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

How to show the modal dialog based on label type

问题

我正在尝试基于标签单击显示一个打开的模态对话框,但是无论我点击哪个标签(Entities By type),它都显示相同的(All Recommendations)模态对话框。如何处理这个问题,请帮忙,提前感谢。

const ExportTypes: Array<{ label: string; id: string }> = [
 { label: '所有建议', id: 'AllRecommendations' },
 { label: '按类型的实体', id: 'EntitiesBytype' },  
];

const handleModalExport = useCallback(
() => {
  if (ExportTypes.some(e => e.id === 'AllRecommendations')) {
      openModalDialog({
        title: t('导出所有建议'),
        renderContent: () => <RecommendationExportModel />,
      });
    }
   else if(ExportTypes.some(e => e.id === 'EntitiesBytype')) { 
      openModalDialog({
        title: t('导出所有'),
        renderContent: () => <RecommendationExportEntitiesModel />,
      });
    }
  },
 [openModalDialog, t],
);

{ExportTypes.map((objType) => (
  <MenuItem key={objType.id} onClick={handleModalExport}>
     {t(objType.label)}
  </MenuItem>
))}
英文:

I'm trying to show a open modal dialog based on the label click, but it is showing the same(All Recommendations) modal dialog even if i clicked on other label(Entities By type) . How to handle this , please help me thanks in advance

const ExportTypes: Array&lt;{ label: string; id: string }&gt; = [
 { label: &#39;All Recommendations&#39;, id: &#39;AllRecommendations&#39; },
 { label: &#39;Entities By type&#39;, id: &#39;EntitiesBytype&#39; },  
];

const handleModalExport = useCallback(
() =&gt; {
  if (ExportTypes.some(e =&gt; e.id === &#39;AllRecommendations&#39;)) {
      openModalDialog({
        title: t(&#39;Export all recommendations&#39;),
        renderContent: () =&gt; &lt;RecommendationExportModel /&gt;,
      });
    }
   else if(ExportTypes.some(e =&gt; e.id === &#39;EntitiesBytype&#39;)) { 
      openModalDialog({
        title: t(&#39;Export all&#39;),
        renderContent: () =&gt; &lt;RecommendationExportEntitiesModel /&gt;,
      });
    }
  },
 [openModalDialog, t],
);

{ExportTypes.map((objType) =&gt; (
  &lt;MenuItem key={objType.id} onClick={handleModalExport}&gt;
     {t(objType.label)}
  &lt;/MenuItem&gt;
))}

答案1

得分: 1

some() 方法测试数组中是否至少有一个元素通过提供的函数实施的测试。如果在数组中找到提供的函数返回 true 的元素,它返回 true;否则返回 false。它不会修改数组。

在这种情况下,当你运行 handleModalExport() 时,它检查你的 ExportTypes 常量,看它是否有一个 id 等于 AllRecommendations 的项目。因为它总是有这个,所以每当你点击 MenuItem 时,它总是会通过第一个条件,因为该项目的 id 始终存在于你的常量中。

基本上你没有使用点击的项目的 id,这就是问题所在。你的 MenuItem 需要传递你点击的 id,并且你想要进行检查。

尝试使用这个代替:

<MenuItem key={objType.id} onClick={() => handleModalExport(objType.id})}>

然后更新你的回调以使用来自你的 onClick 事件的这个动态 id

const handleModalExport = useCallback(
  (id) => {
    switch (id) {
      case 'AllRecommendations':
        openModalDialog({
          title: t('Export all recommendations'),
          renderContent: () => <RecommendationExportModel />,
        });
        break;
      case 'EntitiesBytype':
        openModalDialog({
          title: t('Export all'),
          renderContent: () => <RecommendationExportEntitiesModel />,
        });
        break;
      default:
        // 处理这个,抛出异常?什么都不做?
    }
  },
  [openModalDialog, t],
);
英文:

> The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

In this case, when you run handleModalExport(), it's checking your ExportTypes constant for if it has an item with an id equal to AllRecommendations. Since it always has that, anytime you click the MenuItem, it will always pass the first conditional since that item id is always present in your constant.

You're basically not using the clicked item's id which is the problem. Your MenuItem needs to pass in the id that you are clicking and that you want to do a check on.

Try using this instead:

&lt;MenuItem key={objType.id} onClick={() =&gt; handleModalExport(objType.id})}&gt;

Then update your callback to use this dynamic id from your onClick event:

const handleModalExport = useCallback(
(id) =&gt; {
  switch (id) {
    case &#39;AllRecommendations&#39;:
      openModalDialog({
          title: t(&#39;Export all recommendations&#39;),
          renderContent: () =&gt; &lt;RecommendationExportModel /&gt;,
        });
      }
    break;
    case &#39;EntitiesBytype&#39;:
      openModalDialog({
        title: t(&#39;Export all&#39;),
        renderContent: () =&gt; &lt;RecommendationExportEntitiesModel /&gt;,
      });
    break;
    default:
      // handle this, throw? do nothing?
  },
 [openModalDialog, t],
);

huangapple
  • 本文由 发表于 2023年2月19日 14:09:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498324.html
匿名

发表评论

匿名网友

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

确定