UNPKG

@mui/lab

Version:
106 lines (85 loc) 3.36 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = TabContext; exports.getPanelId = getPanelId; exports.getTabId = getTabId; exports.useTabContext = useTabContext; var React = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _jsxRuntime = require("react/jsx-runtime"); function _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); } function _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; } /** * @type {React.Context<{ idPrefix: string; value: string } | null>} */ 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; } function TabContext(props) { const { children, value } = props; const idPrefix = useUniquePrefix(); const context = React.useMemo(() => { return { idPrefix, value }; }, [idPrefix, value]); return /*#__PURE__*/(0, _jsxRuntime.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 "yarn proptypes" | // ---------------------------------------------------------------------- /** * The content of the component. */ children: _propTypes.default.node, /** * The value of the currently selected `Tab`. */ value: _propTypes.default.string.isRequired } : void 0; /** * @returns {unknown} */ function useTabContext() { return React.useContext(Context); } function getPanelId(context, value) { const { idPrefix } = context; if (idPrefix === null) { return null; } return `${context.idPrefix}-P-${value}`; } function getTabId(context, value) { const { idPrefix } = context; if (idPrefix === null) { return null; } return `${context.idPrefix}-T-${value}`; }