admin管理员组文章数量:1323724
In my React app, I'm using MUI Tabs and I have to make the tabs to show/hide based if an option is true/false.
My issue is that I'm able to hide the tabs but the tabs index gets a different number of what is it when the tabs are all show. That causes when I click on a tab I see the content of another tab.
I need to understand a better way to achieve my goal as what I'm doing seems wrong.
My code is as follow
conditions on how I should show/hide the tabs
const isRecruitmentPortal = options.recruitmentPortal;
const isEngagementPortal = options.engagementPortal;
const isConsenteesPortal = options.consenteesPortal;
The tabs:
<div className={classes.root}>
<AppBar className={classes.appBar} position="static" elevation={0}>
<Toolbar disableGutters>
<Tabs value={tab} onChange={(e, newValue) => setTab(newValue)}>
<Tab label="Study Info" />
<Tab label="Study Tracks" />
<Tab label="User Admin" />
<Tab label="Study Locales" />
{isEngagementPortal && <Tab label="Resources" />}
{isRecruitmentPortal && <Tab label="Pre-screener" />}
{isRecruitmentPortal && <Tab label="Consent" />}
{isRecruitmentPortal && <Tab label="Manuscript" />}
<Tab label="Survey" />
{isRecruitmentPortal && <Tab label="Translations" />}
<Tab label="Sites" />
{isConsenteesPortal && <Tab label="eConsent" />}
{isRecruitmentPortal && <Tab label="Reports" />}
</Tabs>
</Toolbar>
</AppBar>
{console.log(tab)}
<Grid>
{tab === 0 && (
<StudyInfo
studyId={studyId}
refetchQueries={refetchQueries}
setAlert={setAlert}
/>
)}
{tab === 1 && <StudyTracks studyId={studyId} />}
{tab === 2 && <div>USER ADMIN</div>}
{tab === 3 && (
<StudyLocales
setAlert={setAlert}
studyId={studyId}
locales={locales}
currentLocales={currentLocales}
refetchQueries={refetchQueries}
/>
)}
{isEngagementPortal && tab === 4 && (
<StudyResources studyId={studyId} locales={locales} />
)}
{tab === 5 && isRecruitmentPortal && (
<Questionnaire
type="PRESCREENER"
preview
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 6 && (
<Questionnaire
type="CONSENT"
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 7 && (
<Questionnaire
type="MANUSCRIPT"
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{tab === 8 && (
<Questionnaire
type="SURVEY"
preview
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 9 && (
<StudyTranslation
studyId={studyId}
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
/>
)}
{tab === 10 && (
<Sites
studyId={studyId}
refetchQueries={refetchQueries}
setAlert={setAlert}
locales={locales}
/>
)}
{isConsenteesPortal && tab === 11 && <div>E-CONSENT</div>}
{isRecruitmentPortal && tab === 12 && <Reports studyId={studyId} />}
</Grid>
<Alert content={alert} closeDialog={setAlert} />
</div>
);
In my React app, I'm using MUI Tabs and I have to make the tabs to show/hide based if an option is true/false.
My issue is that I'm able to hide the tabs but the tabs index gets a different number of what is it when the tabs are all show. That causes when I click on a tab I see the content of another tab.
I need to understand a better way to achieve my goal as what I'm doing seems wrong.
My code is as follow
conditions on how I should show/hide the tabs
const isRecruitmentPortal = options.recruitmentPortal;
const isEngagementPortal = options.engagementPortal;
const isConsenteesPortal = options.consenteesPortal;
The tabs:
<div className={classes.root}>
<AppBar className={classes.appBar} position="static" elevation={0}>
<Toolbar disableGutters>
<Tabs value={tab} onChange={(e, newValue) => setTab(newValue)}>
<Tab label="Study Info" />
<Tab label="Study Tracks" />
<Tab label="User Admin" />
<Tab label="Study Locales" />
{isEngagementPortal && <Tab label="Resources" />}
{isRecruitmentPortal && <Tab label="Pre-screener" />}
{isRecruitmentPortal && <Tab label="Consent" />}
{isRecruitmentPortal && <Tab label="Manuscript" />}
<Tab label="Survey" />
{isRecruitmentPortal && <Tab label="Translations" />}
<Tab label="Sites" />
{isConsenteesPortal && <Tab label="eConsent" />}
{isRecruitmentPortal && <Tab label="Reports" />}
</Tabs>
</Toolbar>
</AppBar>
{console.log(tab)}
<Grid>
{tab === 0 && (
<StudyInfo
studyId={studyId}
refetchQueries={refetchQueries}
setAlert={setAlert}
/>
)}
{tab === 1 && <StudyTracks studyId={studyId} />}
{tab === 2 && <div>USER ADMIN</div>}
{tab === 3 && (
<StudyLocales
setAlert={setAlert}
studyId={studyId}
locales={locales}
currentLocales={currentLocales}
refetchQueries={refetchQueries}
/>
)}
{isEngagementPortal && tab === 4 && (
<StudyResources studyId={studyId} locales={locales} />
)}
{tab === 5 && isRecruitmentPortal && (
<Questionnaire
type="PRESCREENER"
preview
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 6 && (
<Questionnaire
type="CONSENT"
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 7 && (
<Questionnaire
type="MANUSCRIPT"
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{tab === 8 && (
<Questionnaire
type="SURVEY"
preview
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 9 && (
<StudyTranslation
studyId={studyId}
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
/>
)}
{tab === 10 && (
<Sites
studyId={studyId}
refetchQueries={refetchQueries}
setAlert={setAlert}
locales={locales}
/>
)}
{isConsenteesPortal && tab === 11 && <div>E-CONSENT</div>}
{isRecruitmentPortal && tab === 12 && <Reports studyId={studyId} />}
</Grid>
<Alert content={alert} closeDialog={setAlert} />
</div>
);
Share
Improve this question
asked Feb 2, 2021 at 15:15
JakubJakub
2,7298 gold badges44 silver badges102 bronze badges
1 Answer
Reset to default 6The tab itself can receive a value
prop as written in the docs.
Just set them to a value you need and it should work independent of the hiding:
<div className={classes.root}>
<AppBar className={classes.appBar} position="static" elevation={0}>
<Toolbar disableGutters>
<Tabs value={tab} onChange={(e, newValue) => setTab(newValue)}>
<Tab value={0} label="Study Info" />
<Tab value={1} label="Study Tracks" />
<Tab value={2} label="User Admin" />
<Tab value={3} label="Study Locales" />
{isEngagementPortal && <Tab value={4} label="Resources" />}
{isRecruitmentPortal && <Tab value={5} label="Pre-screener" />}
{isRecruitmentPortal && <Tab value={6} label="Consent" />}
{isRecruitmentPortal && <Tab value={7} label="Manuscript" />}
<Tab value={8} label="Survey" />
{isRecruitmentPortal && <Tab value={9} label="Translations" />}
<Tab value={10} label="Sites" />
{isConsenteesPortal && <Tab value={11} label="eConsent" />}
{isRecruitmentPortal && <Tab value= {12} label="Reports" />}
</Tabs>
</Toolbar>
</AppBar>
{console.log(tab)}
<Grid>
{tab === 0 && (
<StudyInfo
studyId={studyId}
refetchQueries={refetchQueries}
setAlert={setAlert}
/>
)}
{tab === 1 && <StudyTracks studyId={studyId} />}
{tab === 2 && <div>USER ADMIN</div>}
{tab === 3 && (
<StudyLocales
setAlert={setAlert}
studyId={studyId}
locales={locales}
currentLocales={currentLocales}
refetchQueries={refetchQueries}
/>
)}
{isEngagementPortal && tab === 4 && (
<StudyResources studyId={studyId} locales={locales} />
)}
{tab === 5 && isRecruitmentPortal && (
<Questionnaire
type="PRESCREENER"
preview
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 6 && (
<Questionnaire
type="CONSENT"
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 7 && (
<Questionnaire
type="MANUSCRIPT"
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{tab === 8 && (
<Questionnaire
type="SURVEY"
preview
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
studyId={studyId}
/>
)}
{isRecruitmentPortal && tab === 9 && (
<StudyTranslation
studyId={studyId}
locales={currentLocales}
refetchQueries={refetchQueries}
setAlert={setAlert}
/>
)}
{tab === 10 && (
<Sites
studyId={studyId}
refetchQueries={refetchQueries}
setAlert={setAlert}
locales={locales}
/>
)}
{isConsenteesPortal && tab === 11 && <div>E-CONSENT</div>}
{isRecruitmentPortal && tab === 12 && <Reports studyId={studyId} />}
</Grid>
<Alert content={alert} closeDialog={setAlert} />
</div>
);
版权声明:本文标题:javascript - How to showhide MUI Tabs based on a condition and maintain the right tab index - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742124036a2421856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论