@mui/base
Version:
A library of headless ('unstyled') React UI components and low-level hooks.
92 lines (91 loc) • 2.57 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import { unstable_useId as useId, unstable_useForkRef as useForkRef } from '@mui/utils';
import { useTabsContext } from '../Tabs';
import { useCompoundItem } from '../utils/useCompoundItem';
import { useListItem } from '../useList';
import useButton from '../useButton';
function tabValueGenerator(otherTabValues) {
return otherTabValues.size;
}
/**
*
* Demos:
*
* - [Tabs](https://mui.com/base/react-tabs/#hooks)
*
* API:
*
* - [useTab API](https://mui.com/base/react-tabs/hooks-api/#use-tab)
*/
function useTab(parameters) {
const {
value: valueParam,
rootRef: externalRef,
disabled = false,
id: idParam
} = parameters;
const tabRef = React.useRef(null);
const id = useId(idParam);
const {
value: selectedValue,
selectionFollowsFocus,
getTabPanelId
} = useTabsContext();
const tabMetadata = React.useMemo(() => ({
disabled,
ref: tabRef,
id
}), [disabled, tabRef, id]);
const {
id: value,
index,
totalItemCount: totalTabsCount
} = useCompoundItem(valueParam != null ? valueParam : tabValueGenerator, tabMetadata);
const {
getRootProps: getTabProps,
rootRef: listItemRefHandler,
highlighted,
selected
} = useListItem({
item: value
});
const {
getRootProps: getButtonProps,
rootRef: buttonRefHandler,
active,
focusVisible,
setFocusVisible
} = useButton({
disabled,
focusableWhenDisabled: !selectionFollowsFocus,
type: 'button'
});
const handleRef = useForkRef(tabRef, externalRef, listItemRefHandler, buttonRefHandler);
const tabPanelId = value !== undefined ? getTabPanelId(value) : undefined;
const getRootProps = (otherHandlers = {}) => {
const resolvedTabProps = _extends({}, otherHandlers, getTabProps(otherHandlers));
const resolvedButtonProps = _extends({}, resolvedTabProps, getButtonProps(resolvedTabProps));
return _extends({}, resolvedButtonProps, {
role: 'tab',
'aria-controls': tabPanelId,
'aria-selected': selected,
id,
ref: handleRef
});
};
return {
getRootProps,
active,
focusVisible,
highlighted,
index,
rootRef: handleRef,
// the `selected` state isn't set on the server (it relies on effects to be calculated),
// so we fall back to checking the `value` prop with the selectedValue from the TabsContext
selected: selected || value === selectedValue,
setFocusVisible,
totalTabsCount
};
}
export default useTab;