1 | 'use client';
|
2 |
|
3 | import _extends from "@babel/runtime/helpers/esm/extends";
|
4 | import * as React from 'react';
|
5 | import { unstable_useControlled as useControlled } from '@mui/utils';
|
6 | import { useCompoundParent } from '../useCompound';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function useTabs(parameters) {
|
18 | const {
|
19 | value: valueProp,
|
20 | defaultValue,
|
21 | onChange,
|
22 | orientation = 'horizontal',
|
23 | direction = 'ltr',
|
24 | selectionFollowsFocus = false
|
25 | } = parameters;
|
26 | const [value, setValue] = useControlled({
|
27 | controlled: valueProp,
|
28 | default: defaultValue,
|
29 | name: 'Tabs',
|
30 | state: 'value'
|
31 | });
|
32 | const onSelected = React.useCallback((event, newValue) => {
|
33 | setValue(newValue);
|
34 | onChange?.(event, newValue);
|
35 | }, [onChange, setValue]);
|
36 | const {
|
37 | subitems: tabPanels,
|
38 | contextValue: compoundComponentContextValue
|
39 | } = useCompoundParent();
|
40 | const tabIdLookup = React.useRef(() => undefined);
|
41 | const getTabPanelId = React.useCallback(tabValue => {
|
42 | return tabPanels.get(tabValue)?.id;
|
43 | }, [tabPanels]);
|
44 | const getTabId = React.useCallback(tabPanelId => {
|
45 | return tabIdLookup.current(tabPanelId);
|
46 | }, []);
|
47 | const registerTabIdLookup = React.useCallback(lookupFunction => {
|
48 | tabIdLookup.current = lookupFunction;
|
49 | }, []);
|
50 | return {
|
51 | contextValue: _extends({
|
52 | direction,
|
53 | getTabId,
|
54 | getTabPanelId,
|
55 | onSelected,
|
56 | orientation,
|
57 | registerTabIdLookup,
|
58 | selectionFollowsFocus,
|
59 | value
|
60 | }, compoundComponentContextValue)
|
61 | };
|
62 | }
|
63 | export { useTabs }; |
\ | No newline at end of file |