You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
2.6 KiB
JavaScript
47 lines
2.6 KiB
JavaScript
"use strict";
|
|
'use client';
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.useCompoundItem = useCompoundItem;
|
|
var React = _interopRequireWildcard(require("react"));
|
|
var _utils = require("@mui/utils");
|
|
var _useCompoundParent = require("./useCompoundParent");
|
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
/**
|
|
* Registers a child component with the parent component.
|
|
*
|
|
* @param id A unique key for the child component. If the `id` is `undefined`, the registration logic will not run (this can sometimes be the case during SSR).
|
|
* This can be either a value, or a function that generates a value based on already registered siblings' ids.
|
|
* @param itemMetadata Arbitrary metadata to pass to the parent component. This should be a stable reference (e.g. a memoized object), to avoid unnecessary re-registrations.
|
|
* @param missingKeyGenerator A function that generates a unique id for the item.
|
|
* It is called with the set of the ids of all the items that have already been registered.
|
|
* Return `existingKeys.size` if you want to use the index of the new item as the id.
|
|
*
|
|
* @ignore - internal hook.
|
|
*/
|
|
function useCompoundItem(id, itemMetadata) {
|
|
const context = React.useContext(_useCompoundParent.CompoundComponentContext);
|
|
if (context === null) {
|
|
throw new Error('useCompoundItem must be used within a useCompoundParent');
|
|
}
|
|
const {
|
|
registerItem
|
|
} = context;
|
|
const [registeredId, setRegisteredId] = React.useState(typeof id === 'function' ? undefined : id);
|
|
(0, _utils.unstable_useEnhancedEffect)(() => {
|
|
const {
|
|
id: returnedId,
|
|
deregister
|
|
} = registerItem(id, itemMetadata);
|
|
setRegisteredId(returnedId);
|
|
return deregister;
|
|
}, [registerItem, itemMetadata, id]);
|
|
return {
|
|
id: registeredId,
|
|
index: registeredId !== undefined ? context.getItemIndex(registeredId) : -1,
|
|
totalItemCount: context.totalSubitemCount
|
|
};
|
|
} |