1 | import * as React from 'react';
|
2 | interface RegisterItemReturnValue<Key> {
|
3 | /**
|
4 | * The id of the item.
|
5 | * If the `id` was `undefined`, an id from the `missingKeyGenerator` will be used.
|
6 | */
|
7 | id: Key;
|
8 | /**
|
9 | * A function that deregisters the item.
|
10 | */
|
11 | deregister: () => void;
|
12 | }
|
13 | export type KeyGenerator<Key> = (existingKeys: Set<Key>) => Key;
|
14 | export type CompoundComponentContextValue<Key, Subitem> = {
|
15 | /**
|
16 | * Registers an item with the parent.
|
17 | * This should be called during the effect phase of the child component.
|
18 | * The `itemMetadata` should be a stable reference (e.g. a memoized object), to avoid unnecessary re-registrations.
|
19 | *
|
20 | * @param id Id of the item or A function that generates a unique id for the item.
|
21 | * It is called with the set of the ids of all the items that have already been registered.
|
22 | * Return `existingKeys.size` if you want to use the index of the new item as the id..
|
23 | * @param itemMetadata Arbitrary metadata to pass to the parent component.
|
24 | */
|
25 | registerItem: (id: Key | KeyGenerator<Key>, item: Subitem) => RegisterItemReturnValue<Key>;
|
26 | /**
|
27 | * Returns the 0-based index of the item in the parent component's list of registered items.
|
28 | *
|
29 | * @param id id of the item.
|
30 | */
|
31 | getItemIndex: (id: Key) => number;
|
32 | /**
|
33 | * The total number of items registered with the parent.
|
34 | */
|
35 | totalSubitemCount: number;
|
36 | };
|
37 | export declare const CompoundComponentContext: React.Context<CompoundComponentContextValue<any, any> | null>;
|
38 | export interface UseCompoundParentReturnValue<Key, Subitem extends {
|
39 | ref: React.RefObject<Node>;
|
40 | }> {
|
41 | /**
|
42 | * The value for the CompoundComponentContext provider.
|
43 | */
|
44 | contextValue: CompoundComponentContextValue<Key, Subitem>;
|
45 | /**
|
46 | * The subitems registered with the parent.
|
47 | * The key is the id of the subitem, and the value is the metadata passed to the `useCompoundItem` hook.
|
48 | * The order of the items is the same as the order in which they were registered.
|
49 | */
|
50 | subitems: Map<Key, Subitem>;
|
51 | }
|
52 | /**
|
53 | * Provides a way for a component to know about its children.
|
54 | *
|
55 | * Child components register themselves with the `useCompoundItem` hook, passing in arbitrary metadata to the parent.
|
56 | *
|
57 | * This is a more powerful altervantive to `children` traversal, as child components don't have to be placed
|
58 | * directly inside the parent component. They can be anywhere in the tree (and even rendered by other components).
|
59 | *
|
60 | * The downside is that this doesn't work with SSR as it relies on the useEffect hook.
|
61 | *
|
62 | * @ignore - internal hook.
|
63 | */
|
64 | export declare function useCompoundParent<Key, Subitem extends {
|
65 | ref: React.RefObject<Node>;
|
66 | }>(): UseCompoundParentReturnValue<Key, Subitem>;
|
67 | export {};
|