I have a GROQ query where the result is an array of documents, of which one of the fields, "fieldName", can contain "a", "b" or "c". If the array contains any with the value "a" then I want to display a Chakra-UI <Tab>
for it in the <TabList>
as well as a <TabPanel>
in the <TabPanels>
. If there are none with the value "a", don't display a tab, and repeat this for conditions "b" and "c".
// GROQ query
*[slug.current == $slug]{
array[]->
}
// GROQ query json response
"result": [
0:
"array": [
0: {...}
1: {
"_id": "1"
"fieldName": "a"
}
2: {
"_id": "2"
"fieldName": "c"
}
3: {...}
4: {...}
5: {...}
]
}
]
// This example would return tabs and panels for only a and c
I know how to loop through the items conditionally in the example below, but this doesn't help me with the Tab
items as looping through these would give me repeated tabs.
<Tabs>
<TabList>
<Tab>A</Tab> // Conditionally render this only if any array items' "fieldName" contains value "a"
...
</TabList>
<TabPanels>
<TabPanel>
{array &&
array.fieldName.map((panel) => (
<>{panel.fieldName == "a" ? <>{panel.fieldName}</> : null}</>
))}
</TabPanel>
...
</TabPanels>
</Tabs>
Because of the structure of Chakra-UI's tabs I am stumped how I can do this and would appreciate any help. There must be a better way of doing this than the one I am trying?
You can create a object and use it to check if to show specific tab and tabpanel or not
Working Example