react-children-hooks 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,61 +1,158 @@
1
1
  import * as React from 'react';
2
2
  import { ElementType, ReactNode, ReactElement } from 'react';
3
3
 
4
+ /**
5
+ * Inclusive minimum and maximum bounds used by the bounded validation hooks.
6
+ */
7
+ type ChildrenCountBounds = {
8
+ /**
9
+ * The minimum number of matching direct child elements required.
10
+ */
11
+ minimum: number;
12
+ /**
13
+ * The maximum number of matching direct child elements allowed.
14
+ */
15
+ maximum: number;
16
+ };
17
+ /**
18
+ * Optional traversal bounds used by the element-based query and validation hooks.
19
+ */
20
+ type TraversalOptions = {
21
+ /**
22
+ * The minimum inclusive child depth to include in results, where `0` represents direct children.
23
+ */
24
+ depth?: number;
25
+ /**
26
+ * The maximum inclusive child depth to include in results, where `0` represents direct children.
27
+ */
28
+ maximumDepth?: number;
29
+ };
30
+ /**
31
+ * Optional traversal metadata used by the element-based query hooks.
32
+ */
33
+ type QueryOptions = TraversalOptions;
4
34
  /**
5
35
  * Represents a React element whose props and element type are narrowed to the provided React element type.
6
36
  *
7
37
  * @typeParam T The React element type used to narrow the element's props and type.
8
38
  */
9
39
  type ElementOfType<T extends React.ElementType> = React.ReactElement<React.ComponentProps<T>, T>;
40
+ /**
41
+ * A callback child, also known as a render-prop child.
42
+ *
43
+ * @typeParam TArguments The positional argument tuple accepted by the callback.
44
+ * @typeParam TResult The value returned by the callback.
45
+ */
46
+ type CallbackChild<TArguments extends unknown[] = [], TResult = React.ReactNode> = (...args: TArguments) => TResult;
47
+ /**
48
+ * A direct-children value that may include callback children alongside regular React children.
49
+ *
50
+ * @typeParam TArguments The positional argument tuple accepted by callback children.
51
+ * @typeParam TResult The value returned by callback children.
52
+ */
53
+ type CallbackChildren<TArguments extends unknown[] = [], TResult = React.ReactNode> = React.ReactNode | CallbackChild<TArguments, TResult> | readonly CallbackChildren<TArguments, TResult>[];
54
+ /**
55
+ * Optional reporting metadata used by the validation hooks to derive thrown validation messages.
56
+ */
57
+ type ValidationMessageOptions = {
58
+ /**
59
+ * An optional consumer-defined trace code that is prefixed to the thrown validation message.
60
+ */
61
+ traceCode?: string;
62
+ /**
63
+ * An optional human-readable child name that is included in the thrown validation message.
64
+ */
65
+ childName?: string;
66
+ };
67
+ /**
68
+ * Optional reporting and traversal metadata used by the validation hooks.
69
+ */
70
+ type ValidationOptions = ValidationMessageOptions & TraversalOptions;
10
71
 
11
72
  /**
12
73
  * Returns the first direct child element whose React element type exactly matches the provided type.
13
74
  *
14
75
  * @param children The React children value to inspect.
15
76
  * @param type The element or component type to match against each direct child element.
77
+ * @param options Optional query metadata used to configure how child elements are inspected.
16
78
  * @returns The first direct child element whose type matches the provided element type, or `null` when no match is found.
17
79
  */
18
- declare function useChildByType<T extends ElementType>(children: ReactNode, type: T): ElementOfType<T> | null;
80
+ declare function useChildByType<T extends ElementType>(children: ReactNode, type: T, options?: QueryOptions): ElementOfType<T> | null;
81
+
82
+ /**
83
+ * Returns the first direct callback child from the provided children value.
84
+ *
85
+ * @param children The direct children value to inspect.
86
+ * @returns The first direct callback child, or `null` when no callback child is found.
87
+ */
88
+ declare function useCallbackChild<TArguments extends unknown[] = [], TResult = ReactNode>(children: CallbackChildren<TArguments, TResult>): CallbackChild<TArguments, TResult> | null;
19
89
 
20
90
  /**
21
91
  * Returns the first direct child element that satisfies the provided predicate.
22
92
  *
23
93
  * @param children The React children value to inspect.
24
94
  * @param predicate A predicate that is called with each direct child element to determine whether it matches.
95
+ * @param options Optional query metadata used to configure how child elements are inspected.
25
96
  * @returns The first direct child element that satisfies the provided predicate, or `null` when no match is found.
26
97
  */
27
- declare function useChildWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): T | null;
28
- declare function useChildWhere(children: ReactNode, predicate: (element: ReactElement) => boolean): ReactElement | null;
98
+ declare function useChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: QueryOptions): T | null;
99
+ declare function useChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: QueryOptions): ReactElement | null;
100
+
101
+ /**
102
+ * Returns the direct child elements whose React element type exactly matches the provided type, or throws when the count falls outside the inclusive bounds.
103
+ *
104
+ * @param children The React children value to inspect.
105
+ * @param type The element or component type to match against each direct child element.
106
+ * @param bounds The inclusive minimum and maximum number of matching direct child elements allowed.
107
+ * @param options Optional reporting metadata used to derive the thrown validation message.
108
+ * @returns The direct child elements whose type matches the provided element type.
109
+ */
110
+ declare function useBoundedChildrenByType<T extends ElementType>(children: ReactNode, type: T, bounds: ChildrenCountBounds, options?: ValidationOptions): ElementOfType<T>[];
111
+
112
+ /**
113
+ * Returns the direct child elements that satisfy the provided predicate, or throws when the count falls outside the inclusive bounds.
114
+ *
115
+ * @param children The React children value to inspect.
116
+ * @param predicate A predicate that is called with each direct child element to determine whether it matches.
117
+ * @param bounds The inclusive minimum and maximum number of matching direct child elements allowed.
118
+ * @param options Optional reporting metadata used to derive the thrown validation message.
119
+ * @returns The direct child elements that satisfy the provided predicate.
120
+ */
121
+ declare function useBoundedChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, bounds: ChildrenCountBounds, options?: ValidationOptions): T[];
122
+ declare function useBoundedChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, bounds: ChildrenCountBounds, options?: ValidationOptions): ReactElement[];
29
123
 
30
124
  /**
31
125
  * Returns the direct child elements whose React element type exactly matches the provided type.
32
126
  *
33
127
  * @param children The React children value to inspect.
34
128
  * @param type The element or component type to match against each direct child element.
129
+ * @param options Optional query metadata used to configure how child elements are inspected.
35
130
  * @returns An array of direct child elements whose type matches the provided element type.
36
131
  */
37
- declare function useChildrenByType<T extends ElementType>(children: ReactNode, type: T): ElementOfType<T>[];
132
+ declare function useChildrenByType<T extends ElementType>(children: ReactNode, type: T, options?: QueryOptions): ElementOfType<T>[];
38
133
 
39
134
  /**
40
135
  * Returns the direct child elements that satisfy the provided predicate.
41
136
  *
42
137
  * @param children The React children value to inspect.
43
138
  * @param predicate A predicate that is called with each direct child element to determine whether it should be included in the result.
139
+ * @param options Optional query metadata used to configure how child elements are inspected.
44
140
  * @returns An array of direct child elements that satisfy the provided predicate.
45
141
  */
46
- declare function useChildrenWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): T[];
47
- declare function useChildrenWhere(children: ReactNode, predicate: (element: ReactElement) => boolean): ReactElement[];
142
+ declare function useChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: QueryOptions): T[];
143
+ declare function useChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: QueryOptions): ReactElement[];
144
+
145
+ /**
146
+ * Returns the direct child elements whose React element type exactly matches the provided type, or throws when the exact count is not met.
147
+ *
148
+ * @param children The React children value to inspect.
149
+ * @param type The element or component type to match against each direct child element.
150
+ * @param exactCount The exact number of matching direct child elements required.
151
+ * @param options Optional reporting metadata used to derive the thrown validation message.
152
+ * @returns The direct child elements whose type matches the provided element type.
153
+ */
154
+ declare function useExactChildrenByType<T extends ElementType>(children: ReactNode, type: T, exactCount: number, options?: ValidationOptions): ElementOfType<T>[];
48
155
 
49
- type UseExactChildrenWhereOptions = {
50
- /**
51
- * An optional consumer-defined trace code that is prefixed to the thrown validation message.
52
- */
53
- traceCode?: string;
54
- /**
55
- * An optional human-readable child name that is included in the thrown validation message.
56
- */
57
- childName?: string;
58
- };
59
156
  /**
60
157
  * Returns the direct child elements that satisfy the provided predicate, or throws when the exact count is not met.
61
158
  *
@@ -65,29 +162,31 @@ type UseExactChildrenWhereOptions = {
65
162
  * @param options Optional reporting metadata used to derive the thrown validation message.
66
163
  * @returns The direct child elements that satisfy the provided predicate.
67
164
  */
68
- declare function useExactChildrenWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, exactCount: number, options?: UseExactChildrenWhereOptions): T[];
69
- declare function useExactChildrenWhere(children: ReactNode, predicate: (element: ReactElement) => boolean, exactCount: number, options?: UseExactChildrenWhereOptions): ReactElement[];
165
+ declare function useExactChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, exactCount: number, options?: ValidationOptions): T[];
166
+ declare function useExactChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, exactCount: number, options?: ValidationOptions): ReactElement[];
70
167
 
71
168
  /**
72
169
  * Determines whether any direct child element satisfies the provided predicate.
73
170
  *
74
171
  * @param children The React children value to inspect.
75
172
  * @param predicate A predicate that is called with each direct child element to determine whether it matches.
173
+ * @param options Optional query metadata used to configure how child elements are inspected.
76
174
  * @returns `true` when at least one direct child element satisfies the provided predicate; otherwise `false`.
77
175
  */
78
- declare function useHasChildWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): boolean;
79
- declare function useHasChildWhere(children: ReactNode, predicate: (element: ReactElement) => boolean): boolean;
176
+ declare function useHasChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: QueryOptions): boolean;
177
+ declare function useHasChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: QueryOptions): boolean;
178
+
179
+ /**
180
+ * Returns the direct child elements whose React element type exactly matches the provided type, or throws when more than the maximum count are found.
181
+ *
182
+ * @param children The React children value to inspect.
183
+ * @param type The element or component type to match against each direct child element.
184
+ * @param maximumCount The maximum number of matching direct child elements allowed.
185
+ * @param options Optional reporting metadata used to derive the thrown validation message.
186
+ * @returns The direct child elements whose type matches the provided element type.
187
+ */
188
+ declare function useMaximumChildrenByType<T extends ElementType>(children: ReactNode, type: T, maximumCount: number, options?: ValidationOptions): ElementOfType<T>[];
80
189
 
81
- type UseMaximumChildrenWhereOptions = {
82
- /**
83
- * An optional consumer-defined trace code that is prefixed to the thrown validation message.
84
- */
85
- traceCode?: string;
86
- /**
87
- * An optional human-readable child name that is included in the thrown validation message.
88
- */
89
- childName?: string;
90
- };
91
190
  /**
92
191
  * Returns the direct child elements that satisfy the provided predicate, or throws when more than the maximum count are found.
93
192
  *
@@ -97,19 +196,20 @@ type UseMaximumChildrenWhereOptions = {
97
196
  * @param options Optional reporting metadata used to derive the thrown validation message.
98
197
  * @returns The direct child elements that satisfy the provided predicate.
99
198
  */
100
- declare function useMaximumChildrenWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, maximumCount: number, options?: UseMaximumChildrenWhereOptions): T[];
101
- declare function useMaximumChildrenWhere(children: ReactNode, predicate: (element: ReactElement) => boolean, maximumCount: number, options?: UseMaximumChildrenWhereOptions): ReactElement[];
199
+ declare function useMaximumChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, maximumCount: number, options?: ValidationOptions): T[];
200
+ declare function useMaximumChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, maximumCount: number, options?: ValidationOptions): ReactElement[];
201
+
202
+ /**
203
+ * Returns the direct child elements whose React element type exactly matches the provided type, or throws when fewer than the minimum count are found.
204
+ *
205
+ * @param children The React children value to inspect.
206
+ * @param type The element or component type to match against each direct child element.
207
+ * @param minimumCount The minimum number of matching direct child elements required.
208
+ * @param options Optional reporting metadata used to derive the thrown validation message.
209
+ * @returns The direct child elements whose type matches the provided element type.
210
+ */
211
+ declare function useMinimumChildrenByType<T extends ElementType>(children: ReactNode, type: T, minimumCount: number, options?: ValidationOptions): ElementOfType<T>[];
102
212
 
103
- type UseMinimumChildrenWhereOptions = {
104
- /**
105
- * An optional consumer-defined trace code that is prefixed to the thrown validation message.
106
- */
107
- traceCode?: string;
108
- /**
109
- * An optional human-readable child name that is included in the thrown validation message.
110
- */
111
- childName?: string;
112
- };
113
213
  /**
114
214
  * Returns the direct child elements that satisfy the provided predicate, or throws when fewer than the minimum count are found.
115
215
  *
@@ -119,19 +219,49 @@ type UseMinimumChildrenWhereOptions = {
119
219
  * @param options Optional reporting metadata used to derive the thrown validation message.
120
220
  * @returns The direct child elements that satisfy the provided predicate.
121
221
  */
122
- declare function useMinimumChildrenWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, minimumCount: number, options?: UseMinimumChildrenWhereOptions): T[];
123
- declare function useMinimumChildrenWhere(children: ReactNode, predicate: (element: ReactElement) => boolean, minimumCount: number, options?: UseMinimumChildrenWhereOptions): ReactElement[];
222
+ declare function useMinimumChildrenMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, minimumCount: number, options?: ValidationOptions): T[];
223
+ declare function useMinimumChildrenMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, minimumCount: number, options?: ValidationOptions): ReactElement[];
224
+
225
+ /**
226
+ * Returns the optional direct child element whose React element type exactly matches the provided type, or throws when more than one match is found.
227
+ *
228
+ * @param children The React children value to inspect.
229
+ * @param type The element or component type to match against each direct child element.
230
+ * @param options Optional reporting metadata used to derive the thrown validation message.
231
+ * @returns The optional direct child element whose type matches the provided element type, or `null` when no match is found.
232
+ */
233
+ declare function useOptionalChildByType<T extends ElementType>(children: ReactNode, type: T, options?: ValidationOptions): ElementOfType<T> | null;
234
+
235
+ /**
236
+ * Returns the optional direct child element that satisfies the provided predicate, or throws when more than one match is found.
237
+ *
238
+ * @param children The React children value to inspect.
239
+ * @param predicate A predicate that is called with each direct child element to determine whether it matches.
240
+ * @param options Optional reporting metadata used to derive the thrown validation message.
241
+ * @returns The optional direct child element that satisfies the provided predicate, or `null` when no match is found.
242
+ */
243
+ declare function useOptionalChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: ValidationOptions): T | null;
244
+ declare function useOptionalChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: ValidationOptions): ReactElement | null;
245
+
246
+ /**
247
+ * Returns the first direct child element whose React element type exactly matches the provided type, or throws when no match is found.
248
+ *
249
+ * @param children The React children value to inspect.
250
+ * @param type The element or component type to match against each direct child element.
251
+ * @param options Optional reporting metadata used to derive the thrown validation message.
252
+ * @returns The first direct child element whose type matches the provided element type.
253
+ */
254
+ declare function useRequiredChildByType<T extends ElementType>(children: ReactNode, type: T, options?: ValidationOptions): ElementOfType<T>;
255
+
256
+ /**
257
+ * Returns the first direct callback child from the provided children value, or throws when none is found.
258
+ *
259
+ * @param children The direct children value to inspect.
260
+ * @param options Optional reporting metadata used to derive the thrown validation message.
261
+ * @returns The first direct callback child from the provided children value.
262
+ */
263
+ declare function useRequiredCallbackChild<TArguments extends unknown[] = [], TResult = ReactNode>(children: CallbackChildren<TArguments, TResult>, options?: ValidationOptions): CallbackChild<TArguments, TResult>;
124
264
 
125
- type UseRequiredChildWhereOptions = {
126
- /**
127
- * An optional consumer-defined trace code that is prefixed to the thrown validation message.
128
- */
129
- traceCode?: string;
130
- /**
131
- * An optional human-readable child name that is included in the thrown validation message.
132
- */
133
- childName?: string;
134
- };
135
265
  /**
136
266
  * Returns the first direct child element that satisfies the provided predicate, or throws when no match is found.
137
267
  *
@@ -140,16 +270,38 @@ type UseRequiredChildWhereOptions = {
140
270
  * @param options Optional reporting metadata used to derive the thrown validation message.
141
271
  * @returns The first direct child element that satisfies the provided predicate.
142
272
  */
143
- declare function useRequiredChildWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: UseRequiredChildWhereOptions): T;
144
- declare function useRequiredChildWhere(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: UseRequiredChildWhereOptions): ReactElement;
273
+ declare function useRequiredChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: ValidationOptions): T;
274
+ declare function useRequiredChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: ValidationOptions): ReactElement;
275
+
276
+ /**
277
+ * Returns the only direct child element whose React element type exactly matches the provided type, or throws when the match is not unique.
278
+ *
279
+ * @param children The React children value to inspect.
280
+ * @param type The element or component type to match against each direct child element.
281
+ * @param options Optional reporting metadata used to derive the thrown validation message.
282
+ * @returns The only direct child element whose type matches the provided element type.
283
+ */
284
+ declare function useUniqueChildByType<T extends ElementType>(children: ReactNode, type: T, options?: ValidationOptions): ElementOfType<T>;
285
+
286
+ /**
287
+ * Returns the only direct child element that satisfies the provided predicate, or throws when the match is not unique.
288
+ *
289
+ * @param children The React children value to inspect.
290
+ * @param predicate A predicate that is called with each direct child element to determine whether it matches.
291
+ * @param options Optional reporting metadata used to derive the thrown validation message.
292
+ * @returns The only direct child element that satisfies the provided predicate.
293
+ */
294
+ declare function useUniqueChildMatching<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: ValidationOptions): T;
295
+ declare function useUniqueChildMatching(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: ValidationOptions): ReactElement;
145
296
 
146
297
  /**
147
- * Normalizes a React children value into an array containing only valid direct child elements.
298
+ * Normalizes a React children value into an array containing valid child elements within the configured depth range.
148
299
  *
149
300
  * @param children The React children value to normalize.
150
- * @returns An array of valid React elements from the provided direct children.
301
+ * @param options Optional traversal bounds that control which child depths are included.
302
+ * @returns An array of valid React elements from the provided child depths.
151
303
  */
152
- declare function childrenToElements(children: ReactNode): ReactElement[];
304
+ declare function childrenToElements(children: ReactNode, options?: TraversalOptions): ReactElement[];
153
305
 
154
306
  /**
155
307
  * Determines whether a React element exactly matches the provided element or component type.
@@ -168,4 +320,4 @@ declare function isElementOfType<T extends ElementType>(element: ReactElement, t
168
320
  */
169
321
  declare function isReactElement(node: ReactNode): node is ReactElement;
170
322
 
171
- export { type ElementOfType, type UseExactChildrenWhereOptions, type UseMaximumChildrenWhereOptions, type UseMinimumChildrenWhereOptions, type UseRequiredChildWhereOptions, childrenToElements, isElementOfType, isReactElement, useChildByType, useChildWhere, useChildrenByType, useChildrenWhere, useExactChildrenWhere, useHasChildWhere, useMaximumChildrenWhere, useMinimumChildrenWhere, useRequiredChildWhere };
323
+ export { type CallbackChild, type CallbackChildren, type ChildrenCountBounds, type ElementOfType, type QueryOptions, type TraversalOptions, type ValidationOptions, childrenToElements, isElementOfType, isReactElement, useBoundedChildrenByType, useBoundedChildrenMatching, useCallbackChild, useChildByType, useChildMatching, useChildrenByType, useChildrenMatching, useExactChildrenByType, useExactChildrenMatching, useHasChildMatching, useMaximumChildrenByType, useMaximumChildrenMatching, useMinimumChildrenByType, useMinimumChildrenMatching, useOptionalChildByType, useOptionalChildMatching, useRequiredCallbackChild, useRequiredChildByType, useRequiredChildMatching, useUniqueChildByType, useUniqueChildMatching };