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>
)}
);
When isSSH and !isActive, i want to show all cards except the one with title3 and title 4.
So below is how it should work if
!isSSH show all cards
isSSH and isActive show all cards except title3 and title4
isSSH and !isActive dont show any cards.
the above contentFields method actually returns all cards if !isSSH and if isSSH returns all except title3 and title4.
this line in code
return (
!isEmpty(values) && (isSSH ? !card.hidden : true)
);
does the filtering of cards based on hidden value.
now to fix this as i want i have changed contentFields method like below,
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]
);
the above code works as i want. but could some one help me make this if part of code cleaner. better way of handling it. could someone please help me with this. thanks.