1 | import * as React from 'react';
|
2 | import { unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
|
3 | import { CompoundComponentContext } from './useCompound';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export function useCompoundItem(id, itemMetadata) {
|
18 | const context = React.useContext(CompoundComponentContext);
|
19 | if (context === null) {
|
20 | throw new Error('useCompoundItem must be used within a useCompoundParent');
|
21 | }
|
22 | const {
|
23 | registerItem
|
24 | } = context;
|
25 | const [registeredId, setRegisteredId] = React.useState(typeof id === 'function' ? undefined : id);
|
26 | useEnhancedEffect(() => {
|
27 | const {
|
28 | id: returnedId,
|
29 | deregister
|
30 | } = registerItem(id, itemMetadata);
|
31 | setRegisteredId(returnedId);
|
32 | return deregister;
|
33 | }, [registerItem, itemMetadata, id]);
|
34 | return {
|
35 | id: registeredId,
|
36 | index: registeredId !== undefined ? context.getItemIndex(registeredId) : -1,
|
37 | totalItemCount: context.totalSubitemCount
|
38 | };
|
39 | } |
\ | No newline at end of file |