UNPKG

1.95 kBJavaScriptView Raw
1import * as React from 'react';
2import * as PropTypes from 'prop-types';
3/**
4 * @type {React.Context<{ idPrefix: string; value: string } | null>}
5 */
6
7var Context = React.createContext(null);
8
9if (process.env.NODE_ENV !== 'production') {
10 Context.displayName = 'TabContext';
11}
12
13function useUniquePrefix() {
14 var _React$useState = React.useState(null),
15 id = _React$useState[0],
16 setId = _React$useState[1];
17
18 React.useEffect(function () {
19 setId("mui-p-".concat(Math.round(Math.random() * 1e5)));
20 }, []);
21 return id;
22}
23
24export default function TabContext(props) {
25 var children = props.children,
26 value = props.value;
27 var idPrefix = useUniquePrefix();
28 var context = React.useMemo(function () {
29 return {
30 idPrefix: idPrefix,
31 value: value
32 };
33 }, [idPrefix, value]);
34 return /*#__PURE__*/React.createElement(Context.Provider, {
35 value: context
36 }, children);
37}
38process.env.NODE_ENV !== "production" ? TabContext.propTypes = {
39 // ----------------------------- Warning --------------------------------
40 // | These PropTypes are generated from the TypeScript type definitions |
41 // | To update them edit the d.ts file and run "yarn proptypes" |
42 // ----------------------------------------------------------------------
43
44 /**
45 * The content of the component.
46 */
47 children: PropTypes.node,
48
49 /**
50 * The value of the currently selected `Tab`.
51 */
52 value: PropTypes.string.isRequired
53} : void 0;
54/**
55 * @returns {unknown}
56 */
57
58export function useTabContext() {
59 return React.useContext(Context);
60}
61export function getPanelId(context, value) {
62 var idPrefix = context.idPrefix;
63
64 if (idPrefix === null) {
65 return null;
66 }
67
68 return "".concat(context.idPrefix, "-P-").concat(value);
69}
70export function getTabId(context, value) {
71 var idPrefix = context.idPrefix;
72
73 if (idPrefix === null) {
74 return null;
75 }
76
77 return "".concat(context.idPrefix, "-T-").concat(value);
78}
\No newline at end of file