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