UNPKG

1.65 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import * as React from 'react';
5import { unstable_useControlled as useControlled } from '@mui/utils';
6import { useCompoundParent } from '../useCompound';
7/**
8 *
9 * Demos:
10 *
11 * - [Tabs](https://mui.com/base-ui/react-tabs/#hooks)
12 *
13 * API:
14 *
15 * - [useTabs API](https://mui.com/base-ui/react-tabs/hooks-api/#use-tabs)
16 */
17function 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}
63export { useTabs };
\No newline at end of file