You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.6 KiB
JavaScript
63 lines
1.6 KiB
JavaScript
'use client';
|
|
|
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
import * as React from 'react';
|
|
import { unstable_useId as useId, unstable_useForkRef as useForkRef } from '@mui/utils';
|
|
import { useTabsContext } from '../Tabs';
|
|
import { useCompoundItem } from '../useCompound';
|
|
function tabPanelValueGenerator(otherTabPanelValues) {
|
|
return otherTabPanelValues.size;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* Demos:
|
|
*
|
|
* - [Tabs](https://mui.com/base-ui/react-tabs/#hooks)
|
|
*
|
|
* API:
|
|
*
|
|
* - [useTabPanel API](https://mui.com/base-ui/react-tabs/hooks-api/#use-tab-panel)
|
|
*/
|
|
function useTabPanel(parameters) {
|
|
const {
|
|
value: valueParam,
|
|
id: idParam,
|
|
rootRef: externalRef
|
|
} = parameters;
|
|
const context = useTabsContext();
|
|
if (context === null) {
|
|
throw new Error('No TabContext provided');
|
|
}
|
|
const {
|
|
value: selectedTabValue,
|
|
getTabId
|
|
} = context;
|
|
const id = useId(idParam);
|
|
const ref = React.useRef(null);
|
|
const handleRef = useForkRef(ref, externalRef);
|
|
const metadata = React.useMemo(() => ({
|
|
id,
|
|
ref
|
|
}), [id]);
|
|
const {
|
|
id: value
|
|
} = useCompoundItem(valueParam != null ? valueParam : tabPanelValueGenerator, metadata);
|
|
const hidden = value !== selectedTabValue;
|
|
const correspondingTabId = value !== undefined ? getTabId(value) : undefined;
|
|
const getRootProps = (externalProps = {}) => {
|
|
return _extends({
|
|
'aria-labelledby': correspondingTabId != null ? correspondingTabId : undefined,
|
|
hidden,
|
|
id: id != null ? id : undefined
|
|
}, externalProps, {
|
|
ref: handleRef
|
|
});
|
|
};
|
|
return {
|
|
hidden,
|
|
getRootProps,
|
|
rootRef: handleRef
|
|
};
|
|
}
|
|
export { useTabPanel }; |