fix(dashboard-v2): only render TabPanel's children when the panel is first activated
This commit is contained in:
parent
67cd527e1a
commit
e3fe7c60c2
|
@ -1,9 +1,26 @@
|
||||||
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 }) => {
|
||||||
|
const [wasActivated, setWasActivated] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (active) {
|
||||||
|
setWasActivated(true);
|
||||||
|
}
|
||||||
|
}, [active]);
|
||||||
|
|
||||||
|
// 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
|
<div
|
||||||
role="tabpanel"
|
role="tabpanel"
|
||||||
id={`tabpanel-${tabId}`}
|
id={`tabpanel-${tabId}`}
|
||||||
|
@ -13,7 +30,8 @@ export const TabPanel = ({ children, active, tabId, ...props }) => (
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
TabPanel.propTypes = {
|
TabPanel.propTypes = {
|
||||||
/**
|
/**
|
||||||
|
|
Reference in New Issue