fix(dashboard-v2): only render TabPanel's children when the panel is first activated

This commit is contained in:
Michał Leszczyk 2022-03-02 11:59:00 +01:00
parent 67cd527e1a
commit e3fe7c60c2
No known key found for this signature in database
GPG Key ID: FA123CA8BAA2FBF4
1 changed files with 29 additions and 11 deletions

View File

@ -1,19 +1,37 @@
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { useEffect, useState } from "react";
/** /**
* Besides documented props, it accepts all HMTL attributes a `<div>` element does. * Besides documented props, it accepts all HMTL attributes a `<div>` element does.
*/ */
export const TabPanel = ({ children, active, tabId, ...props }) => ( export const TabPanel = ({ children, active, tabId, ...props }) => {
<div const [wasActivated, setWasActivated] = useState(false);
role="tabpanel"
id={`tabpanel-${tabId}`} useEffect(() => {
aria-labelledby={`tab-${tabId}`} if (active) {
{...props} setWasActivated(true);
style={{ display: active ? "block" : "none" }} }
> }, [active]);
{children}
</div> // If the TabPanel was never activated, let's not render its children at all.
); // We'll only render them after the first activation and then won't unmount them
// unless the entire TabPanel is unmounted, too.
if (!active && !wasActivated) {
return null;
}
return (
<div
role="tabpanel"
id={`tabpanel-${tabId}`}
aria-labelledby={`tab-${tabId}`}
{...props}
style={{ display: active ? "block" : "none" }}
>
{children}
</div>
);
};
TabPanel.propTypes = { TabPanel.propTypes = {
/** /**