76 lines
2.2 KiB
JavaScript
76 lines
2.2 KiB
JavaScript
'use client';
|
|
|
|
import * as React from 'react';
|
|
import PropTypes from 'prop-types';
|
|
|
|
/**
|
|
* @type {React.Context<{ idPrefix: string; value: string } | null>}
|
|
*/
|
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
const Context = /*#__PURE__*/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 /*#__PURE__*/_jsx(Context.Provider, {
|
|
value: context,
|
|
children: children
|
|
});
|
|
}
|
|
process.env.NODE_ENV !== "production" ? 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
|
|
} : void 0;
|
|
|
|
/**
|
|
* @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}`;
|
|
} |