-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathTabContext.js
68 lines (59 loc) · 2.01 KB
/
TabContext.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
/**
* @type {React.Context<{ idPrefix: string; value: string } | null>}
*/
const Context = React.createContext(null);
if (process.env.NODE_ENV !== 'production') {
Context.displayName = 'TabContext';
}
function useUniquePrefix() {
const [id, setId] = React.useState(null);
React.useEffect(() => {
setId(`mui-p-${Math.round(Math.random() * 1e5)}`);
}, []);
return id;
}
export default function TabContext(props) {
const { children, value } = props;
const idPrefix = useUniquePrefix();
const context = React.useMemo(() => {
return { idPrefix, value };
}, [idPrefix, value]);
return <Context.Provider value={context}>{children}</Context.Provider>;
}
TabContext.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* The value of the currently selected `Tab`.
*/
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
};
/**
* @returns {unknown}
*/
export function useTabContext() {
return React.useContext(Context);
}
export function getPanelId(context, value) {
const { idPrefix } = context;
if (idPrefix === null) {
return null;
}
return `${context.idPrefix}-P-${value}`;
}
export function getTabId(context, value) {
const { idPrefix } = context;
if (idPrefix === null) {
return null;
}
return `${context.idPrefix}-T-${value}`;
}