UNPKG

1.65 kBJavaScriptView Raw
1import * as React from 'react';
2import { unstable_useId as useId, unstable_useForkRef as useForkRef } from '@mui/utils';
3import { useTabsContext } from '../Tabs';
4import { useCompoundItem } from '../utils/useCompoundItem';
5function tabPanelValueGenerator(otherTabPanelValues) {
6 return otherTabPanelValues.size;
7}
8
9/**
10 *
11 * Demos:
12 *
13 * - [Tabs](https://mui.com/base/react-tabs/#hooks)
14 *
15 * API:
16 *
17 * - [useTabPanel API](https://mui.com/base/react-tabs/hooks-api/#use-tab-panel)
18 */
19function useTabPanel(parameters) {
20 var valueParam = parameters.value,
21 idParam = parameters.id,
22 externalRef = parameters.rootRef;
23 var context = useTabsContext();
24 if (context === null) {
25 throw new Error('No TabContext provided');
26 }
27 var selectedTabValue = context.value,
28 getTabId = context.getTabId;
29 var id = useId(idParam);
30 var ref = React.useRef(null);
31 var handleRef = useForkRef(ref, externalRef);
32 var metadata = React.useMemo(function () {
33 return {
34 id: id,
35 ref: ref
36 };
37 }, [id]);
38 var _useCompoundItem = useCompoundItem(valueParam != null ? valueParam : tabPanelValueGenerator, metadata),
39 value = _useCompoundItem.id;
40 var hidden = value !== selectedTabValue;
41 var correspondingTabId = value !== undefined ? getTabId(value) : undefined;
42 var getRootProps = function getRootProps() {
43 return {
44 'aria-labelledby': correspondingTabId != null ? correspondingTabId : undefined,
45 hidden: hidden,
46 id: id != null ? id : undefined,
47 ref: handleRef
48 };
49 };
50 return {
51 hidden: hidden,
52 getRootProps: getRootProps,
53 rootRef: handleRef
54 };
55}
56export default useTabPanel;
\No newline at end of file