UNPKG

4.06 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = void 0;
8var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9var React = _interopRequireWildcard(require("react"));
10var _utils = require("@mui/utils");
11var _Tabs = require("../Tabs");
12var _useCompoundItem = require("../utils/useCompoundItem");
13var _useList = require("../useList");
14var _useButton = _interopRequireDefault(require("../useButton"));
15function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
16function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17function tabValueGenerator(otherTabValues) {
18 return otherTabValues.size;
19}
20
21/**
22 *
23 * Demos:
24 *
25 * - [Tabs](https://mui.com/base/react-tabs/#hooks)
26 *
27 * API:
28 *
29 * - [useTab API](https://mui.com/base/react-tabs/hooks-api/#use-tab)
30 */
31function useTab(parameters) {
32 const {
33 value: valueParam,
34 rootRef: externalRef,
35 disabled = false,
36 id: idParam
37 } = parameters;
38 const tabRef = React.useRef(null);
39 const id = (0, _utils.unstable_useId)(idParam);
40 const {
41 value: selectedValue,
42 selectionFollowsFocus,
43 getTabPanelId
44 } = (0, _Tabs.useTabsContext)();
45 const tabMetadata = React.useMemo(() => ({
46 disabled,
47 ref: tabRef,
48 id
49 }), [disabled, tabRef, id]);
50 const {
51 id: value,
52 index,
53 totalItemCount: totalTabsCount
54 } = (0, _useCompoundItem.useCompoundItem)(valueParam != null ? valueParam : tabValueGenerator, tabMetadata);
55 const {
56 getRootProps: getTabProps,
57 rootRef: listItemRefHandler,
58 highlighted,
59 selected
60 } = (0, _useList.useListItem)({
61 item: value
62 });
63 const {
64 getRootProps: getButtonProps,
65 rootRef: buttonRefHandler,
66 active,
67 focusVisible,
68 setFocusVisible
69 } = (0, _useButton.default)({
70 disabled,
71 focusableWhenDisabled: !selectionFollowsFocus,
72 type: 'button'
73 });
74 const handleRef = (0, _utils.unstable_useForkRef)(tabRef, externalRef, listItemRefHandler, buttonRefHandler);
75 const tabPanelId = value !== undefined ? getTabPanelId(value) : undefined;
76 const getRootProps = (otherHandlers = {}) => {
77 const resolvedTabProps = (0, _extends2.default)({}, otherHandlers, getTabProps(otherHandlers));
78 const resolvedButtonProps = (0, _extends2.default)({}, resolvedTabProps, getButtonProps(resolvedTabProps));
79 return (0, _extends2.default)({}, resolvedButtonProps, {
80 role: 'tab',
81 'aria-controls': tabPanelId,
82 'aria-selected': selected,
83 id,
84 ref: handleRef
85 });
86 };
87 return {
88 getRootProps,
89 active,
90 focusVisible,
91 highlighted,
92 index,
93 rootRef: handleRef,
94 // the `selected` state isn't set on the server (it relies on effects to be calculated),
95 // so we fall back to checking the `value` prop with the selectedValue from the TabsContext
96 selected: selected || value === selectedValue,
97 setFocusVisible,
98 totalTabsCount
99 };
100}
101var _default = useTab;
102exports.default = _default;
\No newline at end of file