UNPKG

1.75 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 == null || 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 var _tabPanels$get;
43 return (_tabPanels$get = tabPanels.get(tabValue)) == null ? void 0 : _tabPanels$get.id;
44 }, [tabPanels]);
45 const getTabId = React.useCallback(tabPanelId => {
46 return tabIdLookup.current(tabPanelId);
47 }, []);
48 const registerTabIdLookup = React.useCallback(lookupFunction => {
49 tabIdLookup.current = lookupFunction;
50 }, []);
51 return {
52 contextValue: _extends({
53 direction,
54 getTabId,
55 getTabPanelId,
56 onSelected,
57 orientation,
58 registerTabIdLookup,
59 selectionFollowsFocus,
60 value
61 }, compoundComponentContextValue)
62 };
63}
64export { useTabs };
\No newline at end of file