UNPKG

@mui/base

Version:

MUI Base is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

133 lines (131 loc) 3.78 kB
"use strict"; 'use client'; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.useTabsList = useTabsList; var React = _interopRequireWildcard(require("react")); var _Tabs = require("../Tabs"); var _useTabsList = require("./useTabsList.types"); var _useCompound = require("../useCompound"); var _useList = require("../useList"); var _tabsListReducer = require("./tabsListReducer"); /** * * Demos: * * - [Tabs](https://mui.com/base-ui/react-tabs/#hooks) * * API: * * - [useTabsList API](https://mui.com/base-ui/react-tabs/hooks-api/#use-tabs-list) */ function useTabsList(parameters) { const { rootRef: externalRef } = parameters; const { direction = 'ltr', onSelected, orientation = 'horizontal', value, registerTabIdLookup, selectionFollowsFocus } = (0, _Tabs.useTabsContext)(); const { subitems, contextValue: compoundComponentContextValue } = (0, _useCompound.useCompoundParent)(); const tabIdLookup = React.useCallback(tabValue => { return subitems.get(tabValue)?.id; }, [subitems]); registerTabIdLookup(tabIdLookup); const subitemKeys = React.useMemo(() => Array.from(subitems.keys()), [subitems]); const getTabElement = React.useCallback(tabValue => { if (tabValue == null) { return null; } return subitems.get(tabValue)?.ref.current ?? null; }, [subitems]); const isRtl = direction === 'rtl'; let listOrientation; if (orientation === 'vertical') { listOrientation = 'vertical'; } else { listOrientation = isRtl ? 'horizontal-rtl' : 'horizontal-ltr'; } const handleChange = React.useCallback((event, newValue) => { onSelected(event, newValue[0] ?? null); }, [onSelected]); const controlledProps = React.useMemo(() => { if (value === undefined) { return {}; } return value != null ? { selectedValues: [value] } : { selectedValues: [] }; }, [value]); const isItemDisabled = React.useCallback(item => subitems.get(item)?.disabled ?? false, [subitems]); const { contextValue: listContextValue, dispatch, getRootProps: getListboxRootProps, state: { highlightedValue, selectedValues }, rootRef: mergedRootRef } = (0, _useList.useList)({ controlledProps, disabledItemsFocusable: !selectionFollowsFocus, focusManagement: 'DOM', getItemDomElement: getTabElement, isItemDisabled, items: subitemKeys, rootRef: externalRef, onChange: handleChange, orientation: listOrientation, reducerActionContext: React.useMemo(() => ({ selectionFollowsFocus: selectionFollowsFocus || false }), [selectionFollowsFocus]), selectionMode: 'single', stateReducer: _tabsListReducer.tabsListReducer }); React.useEffect(() => { if (value === undefined) { return; } // when a value changes externally, the highlighted value should be synced to it if (value != null) { dispatch({ type: _useTabsList.TabsListActionTypes.valueChange, value }); } }, [dispatch, value]); const getRootProps = (externalProps = {}) => { return { ...externalProps, ...getListboxRootProps(externalProps), 'aria-orientation': orientation === 'vertical' ? 'vertical' : undefined, role: 'tablist' }; }; const contextValue = React.useMemo(() => ({ ...compoundComponentContextValue, ...listContextValue }), [compoundComponentContextValue, listContextValue]); return { contextValue, dispatch, getRootProps, highlightedValue, isRtl, orientation, rootRef: mergedRootRef, selectedValue: selectedValues[0] ?? null }; }