英文:
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<{ label: string; id: string }> = [
{ label: 'All Recommendations', id: 'AllRecommendations' },
{ label: 'Entities By type', id: 'EntitiesBytype' },
];
const handleModalExport = useCallback(
() => {
if (ExportTypes.some(e => e.id === 'AllRecommendations')) {
openModalDialog({
title: t('Export all recommendations'),
renderContent: () => <RecommendationExportModel />,
});
}
else if(ExportTypes.some(e => e.id === 'EntitiesBytype')) {
openModalDialog({
title: t('Export all'),
renderContent: () => <RecommendationExportEntitiesModel />,
});
}
},
[openModalDialog, t],
);
{ExportTypes.map((objType) => (
<MenuItem key={objType.id} onClick={handleModalExport}>
{t(objType.label)}
</MenuItem>
))}
答案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:
<MenuItem key={objType.id} onClick={() => handleModalExport(objType.id})}>
Then update your callback to use this dynamic id
from your onClick event:
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:
// handle this, throw? do nothing?
},
[openModalDialog, t],
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论