【发布时间】:2022-09-24 12:36:39
【问题描述】:
const contentCards: any[] = sortBy(
[
{
title: \'title1\',
field: //array of objects,
},
{ title: \'title2\',
field: //array of objects,
},
{
title: \'title3\',
hidden: true,
field: //array of objects,
},
{
title: \'title4\',
hidden: true,
field: //array of objects,
},
{
title: \'title5\',
field: //array of objects,
},
]
);
const contentFields = React.useMemo(() =>
contentCards.filter(card => {
const values = card.fields.filter(
field => getFieldValue(field) !== null
);
return (
!isEmpty(values) && (isSSH ? !card.hidden : true)
);
}),
[getFieldValue, isSSH]
);
return (
{!isActive && (
<FormField label=\"Content\" >
{() => (
<>
{contentFields.map(card =>
//this renders each card
)}
</>
)}
</FormField>
)}
);
当isSSH 和!isActive 时,我想显示除标题3 和标题4 之外的所有卡片。
所以下面是它应该如何工作,如果
!isSSH show all cards
isSSH and isActive show all cards except title3 and title4
isSSH and !isActive dont show any cards.
如果 !isSSH 并且如果 isSSH 返回除 title3 和 title4 之外的所有卡片,则上述 contentFields 方法实际上返回所有卡片。
代码中的这一行
return (
!isEmpty(values) && (isSSH ? !card.hidden : true)
);
根据隐藏值过滤卡片。
现在要解决此问题,因为我希望我更改了 contentFields 方法,如下所示,
const contentFields = React.useMemo(() =>
contentCards.filter(card => {
const values = card.fields.filter(
field => getFieldValue(field) !== null
);
if (!isEmpty(values)) {
if (!isSSH) return true;
if (isSSH && !isActive) return !card.hidden;
if (isSSH && isActive) return false;
}
}),
[getFieldValue, isSSH, isActive]
);
上面的代码按我的意愿工作。但是如果代码的一部分更清洁,有人可以帮助我做到这一点。更好的处理方式。有人可以帮我解决这个问题吗?谢谢。
-
cleaner是什么意思?
标签: reactjs typescript