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/README.md +36 -16
- package/dist/index.cjs +287 -68
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +212 -60
- package/dist/index.d.ts +212 -60
- package/dist/index.js +268 -61
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ function isElementOfType(element, type) {
|
|
|
3
3
|
return element.type === type;
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
-
// src/
|
|
6
|
+
// src/useChildMatching.ts
|
|
7
7
|
import { useMemo } from "react";
|
|
8
8
|
|
|
9
9
|
// src/childrenToElements.ts
|
|
@@ -15,65 +15,162 @@ function isReactElement(node) {
|
|
|
15
15
|
return isValidElement(node);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
// src/reporter.ts
|
|
19
|
+
import { createReporter } from "runtime-reporter";
|
|
20
|
+
var validationMessages = {
|
|
21
|
+
OPTIONAL_CHILD_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Optional child validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most 1.",
|
|
22
|
+
UNIQUE_CHILD_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Unique child validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly 1.",
|
|
23
|
+
REQUIRED_CHILD_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.",
|
|
24
|
+
REQUIRED_CALLBACK_CHILD_FAILED: "{{ traceCodePrefix }}Required callback child validation failed{{ childNameSegment }} because no direct callback child was found.",
|
|
25
|
+
MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}.",
|
|
26
|
+
MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.",
|
|
27
|
+
EXACT_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.",
|
|
28
|
+
BOUNDED_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Bounded children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected between {{ minimumCount }} and {{ maximumCount }} inclusive."
|
|
29
|
+
};
|
|
30
|
+
var publicMessages = {
|
|
31
|
+
RCH001: '[RCH001] Traversal option "depth" must be a non-negative integer.',
|
|
32
|
+
RCH002: '[RCH002] Traversal option "maximumDepth" must be a non-negative integer.',
|
|
33
|
+
RCH003: '[RCH003] Traversal option "depth" cannot be greater than "maximumDepth".'
|
|
34
|
+
};
|
|
35
|
+
var messages = {
|
|
36
|
+
...validationMessages,
|
|
37
|
+
...publicMessages
|
|
38
|
+
};
|
|
39
|
+
var reporter = createReporter(
|
|
40
|
+
process.env.NODE_ENV === "production" ? {} : messages,
|
|
41
|
+
{ formatMessage: (message) => message }
|
|
42
|
+
);
|
|
43
|
+
var reporter_default = reporter;
|
|
44
|
+
|
|
18
45
|
// src/childrenToElements.ts
|
|
19
|
-
function
|
|
20
|
-
|
|
46
|
+
function getTraversalBounds(options) {
|
|
47
|
+
const depth = options?.depth ?? 0;
|
|
48
|
+
const maximumDepth = options?.maximumDepth ?? 0;
|
|
49
|
+
if (!Number.isInteger(depth) || depth < 0) {
|
|
50
|
+
return reporter_default.fail("RCH001");
|
|
51
|
+
}
|
|
52
|
+
if (!Number.isInteger(maximumDepth) || maximumDepth < 0) {
|
|
53
|
+
return reporter_default.fail("RCH002");
|
|
54
|
+
}
|
|
55
|
+
if (depth > maximumDepth) {
|
|
56
|
+
return reporter_default.fail("RCH003");
|
|
57
|
+
}
|
|
58
|
+
return { depth, maximumDepth };
|
|
59
|
+
}
|
|
60
|
+
function collectElementsAtDepth(children, currentDepth, depth, maximumDepth) {
|
|
61
|
+
const directElements = Children.toArray(children).filter(isReactElement);
|
|
62
|
+
let elements = [];
|
|
63
|
+
if (currentDepth >= depth && currentDepth <= maximumDepth) {
|
|
64
|
+
elements = [...directElements];
|
|
65
|
+
}
|
|
66
|
+
for (const element of directElements) {
|
|
67
|
+
const elementProps = element.props;
|
|
68
|
+
const elementsAtDepth = collectElementsAtDepth(
|
|
69
|
+
elementProps.children,
|
|
70
|
+
currentDepth + 1,
|
|
71
|
+
depth,
|
|
72
|
+
maximumDepth
|
|
73
|
+
);
|
|
74
|
+
elements.push(...elementsAtDepth);
|
|
75
|
+
}
|
|
76
|
+
return elements;
|
|
77
|
+
}
|
|
78
|
+
function childrenToElements(children, options) {
|
|
79
|
+
const { depth, maximumDepth } = getTraversalBounds(options);
|
|
80
|
+
return collectElementsAtDepth(children, 0, depth, maximumDepth);
|
|
21
81
|
}
|
|
22
82
|
|
|
23
|
-
// src/
|
|
24
|
-
function
|
|
83
|
+
// src/useChildMatching.ts
|
|
84
|
+
function useChildMatching(children, predicate, options) {
|
|
25
85
|
return useMemo(
|
|
26
|
-
() => childrenToElements(children).find(predicate) ?? null,
|
|
27
|
-
[children, predicate]
|
|
86
|
+
() => childrenToElements(children, options).find(predicate) ?? null,
|
|
87
|
+
[children, options, predicate]
|
|
28
88
|
);
|
|
29
89
|
}
|
|
30
90
|
|
|
31
91
|
// src/useChildByType.ts
|
|
32
|
-
function useChildByType(children, type) {
|
|
33
|
-
return
|
|
92
|
+
function useChildByType(children, type, options) {
|
|
93
|
+
return useChildMatching(
|
|
34
94
|
children,
|
|
35
|
-
(element) => isElementOfType(element, type)
|
|
95
|
+
(element) => isElementOfType(element, type),
|
|
96
|
+
options
|
|
36
97
|
);
|
|
37
98
|
}
|
|
38
99
|
|
|
39
|
-
// src/
|
|
100
|
+
// src/useCallbackChild.ts
|
|
40
101
|
import { useMemo as useMemo2 } from "react";
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
102
|
+
function findFirstCallbackChild(children) {
|
|
103
|
+
if (typeof children === "function") {
|
|
104
|
+
return children;
|
|
105
|
+
}
|
|
106
|
+
if (!Array.isArray(children)) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
for (const child of children) {
|
|
110
|
+
const callbackChild = findFirstCallbackChild(
|
|
111
|
+
child
|
|
112
|
+
);
|
|
113
|
+
if (callbackChild) {
|
|
114
|
+
return callbackChild;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
function useCallbackChild(children) {
|
|
120
|
+
return useMemo2(() => findFirstCallbackChild(children), [children]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/useChildrenMatching.ts
|
|
124
|
+
import { useMemo as useMemo3 } from "react";
|
|
125
|
+
function useChildrenMatching(children, predicate, options) {
|
|
126
|
+
return useMemo3(
|
|
127
|
+
() => childrenToElements(children, options).filter(predicate),
|
|
128
|
+
[children, options, predicate]
|
|
45
129
|
);
|
|
46
130
|
}
|
|
47
131
|
|
|
48
|
-
// src/
|
|
49
|
-
function
|
|
50
|
-
|
|
132
|
+
// src/useBoundedChildrenMatching.ts
|
|
133
|
+
function useBoundedChildrenMatching(children, predicate, bounds, options) {
|
|
134
|
+
const matchingChildren = useChildrenMatching(children, predicate, options);
|
|
135
|
+
if (matchingChildren.length >= bounds.minimum && matchingChildren.length <= bounds.maximum) {
|
|
136
|
+
return matchingChildren;
|
|
137
|
+
}
|
|
138
|
+
return reporter_default.fail("BOUNDED_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
139
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
140
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
141
|
+
actualCount: matchingChildren.length,
|
|
142
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren",
|
|
143
|
+
minimumCount: bounds.minimum,
|
|
144
|
+
maximumCount: bounds.maximum
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/useBoundedChildrenByType.ts
|
|
149
|
+
function useBoundedChildrenByType(children, type, bounds, options) {
|
|
150
|
+
return useBoundedChildrenMatching(
|
|
51
151
|
children,
|
|
52
|
-
(element) => isElementOfType(element, type)
|
|
152
|
+
(element) => isElementOfType(element, type),
|
|
153
|
+
bounds,
|
|
154
|
+
options
|
|
53
155
|
);
|
|
54
156
|
}
|
|
55
157
|
|
|
56
|
-
// src/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
var reporter = createReporter(
|
|
65
|
-
process.env.NODE_ENV === "production" ? {} : messages,
|
|
66
|
-
{ formatMessage: (message) => message }
|
|
67
|
-
);
|
|
68
|
-
var reporter_default = reporter;
|
|
158
|
+
// src/useChildrenByType.ts
|
|
159
|
+
function useChildrenByType(children, type, options) {
|
|
160
|
+
return useChildrenMatching(
|
|
161
|
+
children,
|
|
162
|
+
(element) => isElementOfType(element, type),
|
|
163
|
+
options
|
|
164
|
+
);
|
|
165
|
+
}
|
|
69
166
|
|
|
70
|
-
// src/
|
|
71
|
-
function
|
|
72
|
-
const matchingChildren =
|
|
167
|
+
// src/useExactChildrenMatching.ts
|
|
168
|
+
function useExactChildrenMatching(children, predicate, exactCount, options) {
|
|
169
|
+
const matchingChildren = useChildrenMatching(children, predicate, options);
|
|
73
170
|
if (matchingChildren.length === exactCount) {
|
|
74
171
|
return matchingChildren;
|
|
75
172
|
}
|
|
76
|
-
return reporter_default.fail("
|
|
173
|
+
return reporter_default.fail("EXACT_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
77
174
|
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
78
175
|
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
79
176
|
actualCount: matchingChildren.length,
|
|
@@ -82,22 +179,32 @@ function useExactChildrenWhere(children, predicate, exactCount, options) {
|
|
|
82
179
|
});
|
|
83
180
|
}
|
|
84
181
|
|
|
85
|
-
// src/
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
() =>
|
|
90
|
-
|
|
182
|
+
// src/useExactChildrenByType.ts
|
|
183
|
+
function useExactChildrenByType(children, type, exactCount, options) {
|
|
184
|
+
return useExactChildrenMatching(
|
|
185
|
+
children,
|
|
186
|
+
(element) => isElementOfType(element, type),
|
|
187
|
+
exactCount,
|
|
188
|
+
options
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/useHasChildMatching.ts
|
|
193
|
+
import { useMemo as useMemo4 } from "react";
|
|
194
|
+
function useHasChildMatching(children, predicate, options) {
|
|
195
|
+
return useMemo4(
|
|
196
|
+
() => childrenToElements(children, options).some(predicate),
|
|
197
|
+
[children, options, predicate]
|
|
91
198
|
);
|
|
92
199
|
}
|
|
93
200
|
|
|
94
|
-
// src/
|
|
95
|
-
function
|
|
96
|
-
const matchingChildren =
|
|
201
|
+
// src/useMaximumChildrenMatching.ts
|
|
202
|
+
function useMaximumChildrenMatching(children, predicate, maximumCount, options) {
|
|
203
|
+
const matchingChildren = useChildrenMatching(children, predicate, options);
|
|
97
204
|
if (matchingChildren.length <= maximumCount) {
|
|
98
205
|
return matchingChildren;
|
|
99
206
|
}
|
|
100
|
-
return reporter_default.fail("
|
|
207
|
+
return reporter_default.fail("MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
101
208
|
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
102
209
|
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
103
210
|
actualCount: matchingChildren.length,
|
|
@@ -106,13 +213,23 @@ function useMaximumChildrenWhere(children, predicate, maximumCount, options) {
|
|
|
106
213
|
});
|
|
107
214
|
}
|
|
108
215
|
|
|
109
|
-
// src/
|
|
110
|
-
function
|
|
111
|
-
|
|
216
|
+
// src/useMaximumChildrenByType.ts
|
|
217
|
+
function useMaximumChildrenByType(children, type, maximumCount, options) {
|
|
218
|
+
return useMaximumChildrenMatching(
|
|
219
|
+
children,
|
|
220
|
+
(element) => isElementOfType(element, type),
|
|
221
|
+
maximumCount,
|
|
222
|
+
options
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// src/useMinimumChildrenMatching.ts
|
|
227
|
+
function useMinimumChildrenMatching(children, predicate, minimumCount, options) {
|
|
228
|
+
const matchingChildren = useChildrenMatching(children, predicate, options);
|
|
112
229
|
if (matchingChildren.length >= minimumCount) {
|
|
113
230
|
return matchingChildren;
|
|
114
231
|
}
|
|
115
|
-
return reporter_default.fail("
|
|
232
|
+
return reporter_default.fail("MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED", {
|
|
116
233
|
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
117
234
|
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
118
235
|
actualCount: matchingChildren.length,
|
|
@@ -121,29 +238,119 @@ function useMinimumChildrenWhere(children, predicate, minimumCount, options) {
|
|
|
121
238
|
});
|
|
122
239
|
}
|
|
123
240
|
|
|
124
|
-
// src/
|
|
125
|
-
function
|
|
126
|
-
|
|
241
|
+
// src/useMinimumChildrenByType.ts
|
|
242
|
+
function useMinimumChildrenByType(children, type, minimumCount, options) {
|
|
243
|
+
return useMinimumChildrenMatching(
|
|
244
|
+
children,
|
|
245
|
+
(element) => isElementOfType(element, type),
|
|
246
|
+
minimumCount,
|
|
247
|
+
options
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// src/useOptionalChildMatching.ts
|
|
252
|
+
function useOptionalChildMatching(children, predicate, options) {
|
|
253
|
+
const matchingChildren = useChildrenMatching(children, predicate, options);
|
|
254
|
+
if (matchingChildren.length <= 1) {
|
|
255
|
+
return matchingChildren[0] ?? null;
|
|
256
|
+
}
|
|
257
|
+
return reporter_default.fail("OPTIONAL_CHILD_MATCHING_PREDICATE_FAILED", {
|
|
258
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
259
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
260
|
+
actualCount: matchingChildren.length,
|
|
261
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren"
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// src/useOptionalChildByType.ts
|
|
266
|
+
function useOptionalChildByType(children, type, options) {
|
|
267
|
+
return useOptionalChildMatching(
|
|
268
|
+
children,
|
|
269
|
+
(element) => isElementOfType(element, type),
|
|
270
|
+
options
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/useRequiredChildMatching.ts
|
|
275
|
+
function useRequiredChildMatching(children, predicate, options) {
|
|
276
|
+
const child = useChildMatching(children, predicate, options);
|
|
127
277
|
if (child !== null) {
|
|
128
278
|
return child;
|
|
129
279
|
}
|
|
130
|
-
return reporter_default.fail("
|
|
280
|
+
return reporter_default.fail("REQUIRED_CHILD_MATCHING_PREDICATE_FAILED", {
|
|
281
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
282
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : ""
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// src/useRequiredChildByType.ts
|
|
287
|
+
function useRequiredChildByType(children, type, options) {
|
|
288
|
+
return useRequiredChildMatching(
|
|
289
|
+
children,
|
|
290
|
+
(element) => isElementOfType(element, type),
|
|
291
|
+
options
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// src/useRequiredCallbackChild.ts
|
|
296
|
+
import "react";
|
|
297
|
+
function useRequiredCallbackChild(children, options) {
|
|
298
|
+
const callbackChild = useCallbackChild(children);
|
|
299
|
+
if (callbackChild !== null) {
|
|
300
|
+
return callbackChild;
|
|
301
|
+
}
|
|
302
|
+
return reporter_default.fail("REQUIRED_CALLBACK_CHILD_FAILED", {
|
|
131
303
|
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
132
304
|
childNameSegment: options?.childName ? ` for ${options.childName}` : ""
|
|
133
305
|
});
|
|
134
306
|
}
|
|
307
|
+
|
|
308
|
+
// src/useUniqueChildMatching.ts
|
|
309
|
+
function useUniqueChildMatching(children, predicate, options) {
|
|
310
|
+
const matchingChildren = useChildrenMatching(children, predicate, options);
|
|
311
|
+
if (matchingChildren.length === 1) {
|
|
312
|
+
return matchingChildren[0];
|
|
313
|
+
}
|
|
314
|
+
return reporter_default.fail("UNIQUE_CHILD_MATCHING_PREDICATE_FAILED", {
|
|
315
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
316
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
317
|
+
actualCount: matchingChildren.length,
|
|
318
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren"
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// src/useUniqueChildByType.ts
|
|
323
|
+
function useUniqueChildByType(children, type, options) {
|
|
324
|
+
return useUniqueChildMatching(
|
|
325
|
+
children,
|
|
326
|
+
(element) => isElementOfType(element, type),
|
|
327
|
+
options
|
|
328
|
+
);
|
|
329
|
+
}
|
|
135
330
|
export {
|
|
136
331
|
childrenToElements,
|
|
137
332
|
isElementOfType,
|
|
138
333
|
isReactElement,
|
|
334
|
+
useBoundedChildrenByType,
|
|
335
|
+
useBoundedChildrenMatching,
|
|
336
|
+
useCallbackChild,
|
|
139
337
|
useChildByType,
|
|
140
|
-
|
|
338
|
+
useChildMatching,
|
|
141
339
|
useChildrenByType,
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
340
|
+
useChildrenMatching,
|
|
341
|
+
useExactChildrenByType,
|
|
342
|
+
useExactChildrenMatching,
|
|
343
|
+
useHasChildMatching,
|
|
344
|
+
useMaximumChildrenByType,
|
|
345
|
+
useMaximumChildrenMatching,
|
|
346
|
+
useMinimumChildrenByType,
|
|
347
|
+
useMinimumChildrenMatching,
|
|
348
|
+
useOptionalChildByType,
|
|
349
|
+
useOptionalChildMatching,
|
|
350
|
+
useRequiredCallbackChild,
|
|
351
|
+
useRequiredChildByType,
|
|
352
|
+
useRequiredChildMatching,
|
|
353
|
+
useUniqueChildByType,
|
|
354
|
+
useUniqueChildMatching
|
|
148
355
|
};
|
|
149
356
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/isElementOfType.ts","../src/useChildWhere.ts","../src/childrenToElements.ts","../src/isReactElement.ts","../src/useChildByType.ts","../src/useChildrenWhere.ts","../src/useChildrenByType.ts","../src/reporter.ts","../src/useExactChildrenWhere.ts","../src/useHasChildWhere.ts","../src/useMaximumChildrenWhere.ts","../src/useMinimumChildrenWhere.ts","../src/useRequiredChildWhere.ts"],"sourcesContent":["import type { ElementType, ReactElement } from \"react\";\n\nimport type { ElementOfType } from \"./types\";\n\n/**\n * Determines whether a React element exactly matches the provided element or component type.\n *\n * @param element The React element to compare.\n * @param type The element or component type to match.\n * @returns `true` when the element's type exactly matches the provided type; otherwise `false`.\n */\nexport function isElementOfType<T extends ElementType>(\n element: ReactElement,\n type: T\n): element is ElementOfType<T> {\n return element.type === type;\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\n\n/**\n * Returns the first direct child element that satisfies the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @returns The first direct child element that satisfies the provided predicate, or `null` when no match is found.\n */\nexport function useChildWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T\n): T | null;\nexport function useChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement | null;\nexport function useChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement | null {\n return useMemo(\n () => childrenToElements(children).find(predicate) ?? null,\n [children, predicate]\n );\n}\n","import { Children, type ReactElement, type ReactNode } from \"react\";\n\nimport { isReactElement } from \"./isReactElement\";\n\n/**\n * Normalizes a React children value into an array containing only valid direct child elements.\n *\n * @param children The React children value to normalize.\n * @returns An array of valid React elements from the provided direct children.\n */\nexport function childrenToElements(children: ReactNode): ReactElement[] {\n return Children.toArray(children).filter(isReactElement);\n}\n","import { isValidElement, type ReactElement, type ReactNode } from \"react\";\n\n/**\n * Determines whether a React node is a valid React element.\n *\n * @param node The React node to check.\n * @returns `true` when the node is a valid React element; otherwise `false`.\n */\nexport function isReactElement(node: ReactNode): node is ReactElement {\n return isValidElement(node);\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport { useChildWhere } from \"./useChildWhere\";\nimport type { ElementOfType } from \"./types\";\n\n/**\n * Returns the first direct child element whose React element type exactly matches the provided type.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @returns The first direct child element whose type matches the provided element type, or `null` when no match is found.\n */\nexport function useChildByType<T extends ElementType>(\n children: ReactNode,\n type: T\n): ElementOfType<T> | null {\n return useChildWhere(children, (element): element is ElementOfType<T> =>\n isElementOfType(element, type)\n );\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it should be included in the result.\n * @returns An array of direct child elements that satisfy the provided predicate.\n */\nexport function useChildrenWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T\n): T[];\nexport function useChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement[];\nexport function useChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): ReactElement[] {\n return useMemo(\n () => childrenToElements(children).filter(predicate),\n [children, predicate]\n );\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport { useChildrenWhere } from \"./useChildrenWhere\";\nimport type { ElementOfType } from \"./types\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @returns An array of direct child elements whose type matches the provided element type.\n */\nexport function useChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T\n): ElementOfType<T>[] {\n return useChildrenWhere(children, (element): element is ElementOfType<T> =>\n isElementOfType(element, type)\n );\n}\n","import { createReporter, type RuntimeReporterMessages } from \"runtime-reporter\";\n\nconst messages: RuntimeReporterMessages<\n | {\n code: \"REQUIRED_CHILD_WHERE_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.\";\n tokens: \"traceCodePrefix\" | \"childNameSegment\";\n }\n | {\n code: \"MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}.\";\n tokens:\n | \"traceCodePrefix\"\n | \"childNameSegment\"\n | \"actualCount\"\n | \"actualCountPluralSuffix\"\n | \"minimumCount\";\n }\n | {\n code: \"MAXIMUM_CHILDREN_WHERE_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.\";\n tokens:\n | \"traceCodePrefix\"\n | \"childNameSegment\"\n | \"actualCount\"\n | \"actualCountPluralSuffix\"\n | \"maximumCount\";\n }\n | {\n code: \"EXACT_CHILDREN_WHERE_PREDICATE_FAILED\";\n template: \"{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.\";\n tokens:\n | \"traceCodePrefix\"\n | \"childNameSegment\"\n | \"actualCount\"\n | \"actualCountPluralSuffix\"\n | \"exactCount\";\n }\n> = {\n REQUIRED_CHILD_WHERE_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.\",\n MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}.\",\n MAXIMUM_CHILDREN_WHERE_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.\",\n EXACT_CHILDREN_WHERE_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.\"\n};\n\n/** The runtime reporter for react-children-hooks */\nconst reporter = createReporter(\n process.env.NODE_ENV === \"production\" ? ({} as typeof messages) : messages,\n { formatMessage: (message) => message }\n);\n\nexport default reporter;\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport { useChildrenWhere } from \"./useChildrenWhere\";\n\nexport type UseExactChildrenWhereOptions = {\n /**\n * An optional consumer-defined trace code that is prefixed to the thrown validation message.\n */\n traceCode?: string;\n /**\n * An optional human-readable child name that is included in the thrown validation message.\n */\n childName?: string;\n};\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when the exact count is not met.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param exactCount The exact number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useExactChildrenWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n exactCount: number,\n options?: UseExactChildrenWhereOptions\n): T[];\nexport function useExactChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n exactCount: number,\n options?: UseExactChildrenWhereOptions\n): ReactElement[];\nexport function useExactChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n exactCount: number,\n options?: UseExactChildrenWhereOptions\n): ReactElement[] {\n const matchingChildren = useChildrenWhere(children, predicate);\n\n if (matchingChildren.length === exactCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"EXACT_CHILDREN_WHERE_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n exactCount\n });\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\n\n/**\n * Determines whether any direct child element satisfies the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @returns `true` when at least one direct child element satisfies the provided predicate; otherwise `false`.\n */\nexport function useHasChildWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T\n): boolean;\nexport function useHasChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): boolean;\nexport function useHasChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean\n): boolean {\n return useMemo(\n () => childrenToElements(children).some(predicate),\n [children, predicate]\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport { useChildrenWhere } from \"./useChildrenWhere\";\n\nexport type UseMaximumChildrenWhereOptions = {\n /**\n * An optional consumer-defined trace code that is prefixed to the thrown validation message.\n */\n traceCode?: string;\n /**\n * An optional human-readable child name that is included in the thrown validation message.\n */\n childName?: string;\n};\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when more than the maximum count are found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param maximumCount The maximum number of matching direct child elements allowed.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useMaximumChildrenWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n maximumCount: number,\n options?: UseMaximumChildrenWhereOptions\n): T[];\nexport function useMaximumChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n maximumCount: number,\n options?: UseMaximumChildrenWhereOptions\n): ReactElement[];\nexport function useMaximumChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n maximumCount: number,\n options?: UseMaximumChildrenWhereOptions\n): ReactElement[] {\n const matchingChildren = useChildrenWhere(children, predicate);\n\n if (matchingChildren.length <= maximumCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"MAXIMUM_CHILDREN_WHERE_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n maximumCount\n });\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport { useChildrenWhere } from \"./useChildrenWhere\";\n\nexport type UseMinimumChildrenWhereOptions = {\n /**\n * An optional consumer-defined trace code that is prefixed to the thrown validation message.\n */\n traceCode?: string;\n /**\n * An optional human-readable child name that is included in the thrown validation message.\n */\n childName?: string;\n};\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when fewer than the minimum count are found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param minimumCount The minimum number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useMinimumChildrenWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n minimumCount: number,\n options?: UseMinimumChildrenWhereOptions\n): T[];\nexport function useMinimumChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n minimumCount: number,\n options?: UseMinimumChildrenWhereOptions\n): ReactElement[];\nexport function useMinimumChildrenWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n minimumCount: number,\n options?: UseMinimumChildrenWhereOptions\n): ReactElement[] {\n const matchingChildren = useChildrenWhere(children, predicate);\n\n if (matchingChildren.length >= minimumCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n minimumCount\n });\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport { useChildWhere } from \"./useChildWhere\";\n\nexport type UseRequiredChildWhereOptions = {\n /**\n * An optional consumer-defined trace code that is prefixed to the thrown validation message.\n */\n traceCode?: string;\n /**\n * An optional human-readable child name that is included in the thrown validation message.\n */\n childName?: string;\n};\n\n/**\n * Returns the first direct child element that satisfies the provided predicate, or throws when no match is found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The first direct child element that satisfies the provided predicate.\n */\nexport function useRequiredChildWhere<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n options?: UseRequiredChildWhereOptions\n): T;\nexport function useRequiredChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: UseRequiredChildWhereOptions\n): ReactElement;\nexport function useRequiredChildWhere(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: UseRequiredChildWhereOptions\n): ReactElement {\n const child = useChildWhere(children, predicate);\n\n if (child !== null) {\n return child;\n }\n\n return reporter.fail(\"REQUIRED_CHILD_WHERE_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\"\n });\n}\n"],"mappings":";AAWO,SAAS,gBACZ,SACA,MAC2B;AAC3B,SAAO,QAAQ,SAAS;AAC5B;;;AChBA,SAAS,eAAkD;;;ACA3D,SAAS,gBAAmD;;;ACA5D,SAAS,sBAAyD;AAQ3D,SAAS,eAAe,MAAuC;AAClE,SAAO,eAAe,IAAI;AAC9B;;;ADAO,SAAS,mBAAmB,UAAqC;AACpE,SAAO,SAAS,QAAQ,QAAQ,EAAE,OAAO,cAAc;AAC3D;;;ADOO,SAAS,cACZ,UACA,WACmB;AACnB,SAAO;AAAA,IACH,MAAM,mBAAmB,QAAQ,EAAE,KAAK,SAAS,KAAK;AAAA,IACtD,CAAC,UAAU,SAAS;AAAA,EACxB;AACJ;;;AGdO,SAAS,eACZ,UACA,MACuB;AACvB,SAAO;AAAA,IAAc;AAAA,IAAU,CAAC,YAC5B,gBAAgB,SA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AS;AAE7D,MAAI,iBAAiB,UAAU,cAAc;AACzC,WAAO;AAAA,EACX;AAEA,SAAO,iBAAS,KAAK,2CAA2C;AAAA,IAC5D,iBAAiB,SAAS,YAAY,IAAI,QAAQ,SAAS,OAAO;AAAA,IAClE,kBAAkB,SAAS,YAAY,QAAQ,QAAQ,SAAS,KAAK;AAAA,IACrE,aAAa,iBAAiB;AAAA,IAC9B,yBAAyB,iBAAiB,WAAW,IAAI,KAAK;AAAA,IAC9D;AAAA,EACJ,CAAC;AACL;;;ACnBO,SAAS,wBACZ,UACA,WACA,cACA,SACc;AACd,QAAM,mBAAmB,iBAAiB,UAAU,SAAS;AAE7D,MAAI,iBAAiB,UAAU,cAAc;AACzC,WAAO;AAAA,EACX;AAEA,SAAO,iBAAS,KAAK,2CAA2C;AAAA,IAC5D,iBAAiB,SAAS,YAAY,IAAI,QAAQ,SAAS,OAAO;AAAA,IAClE,kBAAkB,SAAS,YAAY,QAAQ,QAAQ,SAAS,KAAK;AAAA,IACrE,aAAa,iBAAiB;AAAA,IAC9B,yBAAyB,iBAAiB,WAAW,IAAI,KAAK;AAAA,IAC9D;AAAA,EACJ,CAAC;AACL;;;ACtBO,SAAS,sBACZ,UACA,WACA,SACY;AACZ,QAAM,QAAQ,cAAc,UAAU,SAAS;AAE/C,MAAI,UAAU,MAAM;AAChB,WAAO;AAAA,EACX;AAEA,SAAO,iBAAS,KAAK,yCAAyC;AAAA,IAC1D,iBAAiB,SAAS,YAAY,IAAI,QAAQ,SAAS,OAAO;AAAA,IAClE,kBAAkB,SAAS,YAAY,QAAQ,QAAQ,SAAS,KAAK;AAAA,EACzE,CAAC;AACL;","names":["useMemo","useMemo","useMemo","useMemo"]}
|
|
1
|
+
{"version":3,"sources":["../src/isElementOfType.ts","../src/useChildMatching.ts","../src/childrenToElements.ts","../src/isReactElement.ts","../src/reporter.ts","../src/useChildByType.ts","../src/useCallbackChild.ts","../src/useChildrenMatching.ts","../src/useBoundedChildrenMatching.ts","../src/useBoundedChildrenByType.ts","../src/useChildrenByType.ts","../src/useExactChildrenMatching.ts","../src/useExactChildrenByType.ts","../src/useHasChildMatching.ts","../src/useMaximumChildrenMatching.ts","../src/useMaximumChildrenByType.ts","../src/useMinimumChildrenMatching.ts","../src/useMinimumChildrenByType.ts","../src/useOptionalChildMatching.ts","../src/useOptionalChildByType.ts","../src/useRequiredChildMatching.ts","../src/useRequiredChildByType.ts","../src/useRequiredCallbackChild.ts","../src/useUniqueChildMatching.ts","../src/useUniqueChildByType.ts"],"sourcesContent":["import type { ElementType, ReactElement } from \"react\";\n\nimport type { ElementOfType } from \"./types\";\n\n/**\n * Determines whether a React element exactly matches the provided element or component type.\n *\n * @param element The React element to compare.\n * @param type The element or component type to match.\n * @returns `true` when the element's type exactly matches the provided type; otherwise `false`.\n */\nexport function isElementOfType<T extends ElementType>(\n element: ReactElement,\n type: T\n): element is ElementOfType<T> {\n return element.type === type;\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\nimport type { QueryOptions } from \"./types\";\n\n/**\n * Returns the first direct child element that satisfies the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param options Optional query metadata used to configure how child elements are inspected.\n * @returns The first direct child element that satisfies the provided predicate, or `null` when no match is found.\n */\nexport function useChildMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n options?: QueryOptions\n): T | null;\nexport function useChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: QueryOptions\n): ReactElement | null;\nexport function useChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: QueryOptions\n): ReactElement | null {\n return useMemo(\n () => childrenToElements(children, options).find(predicate) ?? null,\n [children, options, predicate]\n );\n}\n","import { Children, type ReactElement, type ReactNode } from \"react\";\n\nimport { isReactElement } from \"./isReactElement\";\nimport reporter from \"./reporter\";\nimport type { TraversalOptions } from \"./types\";\n\n/**\n * Normalizes traversal bounds and validates that the provided options define a valid inclusive depth range.\n *\n * @param options Optional traversal bounds supplied to an element-inspection API.\n * @returns The normalized inclusive traversal bounds, defaulting to direct children only when omitted.\n */\nfunction getTraversalBounds(options?: TraversalOptions): {\n depth: number;\n maximumDepth: number;\n} {\n const depth = options?.depth ?? 0;\n const maximumDepth = options?.maximumDepth ?? 0;\n\n if (!Number.isInteger(depth) || depth < 0) {\n return reporter.fail(\"RCH001\");\n }\n\n if (!Number.isInteger(maximumDepth) || maximumDepth < 0) {\n return reporter.fail(\"RCH002\");\n }\n\n if (depth > maximumDepth) {\n return reporter.fail(\"RCH003\");\n }\n\n return { depth, maximumDepth };\n}\n\n/**\n * Recursively collects valid React child elements within the provided inclusive depth range.\n *\n * @param children The React children value to inspect at the current traversal level.\n * @param currentDepth The zero-based depth of the current traversal level, where `0` represents direct children.\n * @param depth The minimum inclusive child depth to include in the collected results.\n * @param maximumDepth The maximum inclusive child depth to include in the collected results.\n * @returns An array of valid React elements collected from the current level and any eligible descendant levels.\n */\nfunction collectElementsAtDepth(\n children: ReactNode,\n currentDepth: number,\n depth: number,\n maximumDepth: number\n): ReactElement[] {\n const directElements = Children.toArray(children).filter(isReactElement);\n let elements: ReactElement[] = [];\n\n if (currentDepth >= depth && currentDepth <= maximumDepth) {\n elements = [...directElements];\n }\n\n for (const element of directElements) {\n const elementProps = element.props as { children?: ReactNode };\n const elementsAtDepth = collectElementsAtDepth(\n elementProps.children,\n currentDepth + 1,\n depth,\n maximumDepth\n );\n\n elements.push(...elementsAtDepth);\n }\n\n return elements;\n}\n\n/**\n * Normalizes a React children value into an array containing valid child elements within the configured depth range.\n *\n * @param children The React children value to normalize.\n * @param options Optional traversal bounds that control which child depths are included.\n * @returns An array of valid React elements from the provided child depths.\n */\nexport function childrenToElements(\n children: ReactNode,\n options?: TraversalOptions\n): ReactElement[] {\n const { depth, maximumDepth } = getTraversalBounds(options);\n\n return collectElementsAtDepth(children, 0, depth, maximumDepth);\n}\n","import { isValidElement, type ReactElement, type ReactNode } from \"react\";\n\n/**\n * Determines whether a React node is a valid React element.\n *\n * @param node The React node to check.\n * @returns `true` when the node is a valid React element; otherwise `false`.\n */\nexport function isReactElement(node: ReactNode): node is ReactElement {\n return isValidElement(node);\n}\n","import { createReporter } from \"runtime-reporter\";\n\n/** Internal validation reporter messages used by the hook validation APIs */\nconst validationMessages = {\n OPTIONAL_CHILD_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Optional child validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most 1.\",\n UNIQUE_CHILD_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Unique child validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly 1.\",\n REQUIRED_CHILD_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.\",\n REQUIRED_CALLBACK_CHILD_FAILED:\n \"{{ traceCodePrefix }}Required callback child validation failed{{ childNameSegment }} because no direct callback child was found.\",\n MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}.\",\n MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.\",\n EXACT_CHILDREN_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.\",\n BOUNDED_CHILDREN_MATCHING_PREDICATE_FAILED:\n \"{{ traceCodePrefix }}Bounded children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected between {{ minimumCount }} and {{ maximumCount }} inclusive.\"\n} as const;\n\n/** Public-facing reporter messages for this library */\nconst publicMessages = {\n RCH001: '[RCH001] Traversal option \"depth\" must be a non-negative integer.',\n RCH002: '[RCH002] Traversal option \"maximumDepth\" must be a non-negative integer.',\n RCH003: '[RCH003] Traversal option \"depth\" cannot be greater than \"maximumDepth\".'\n} as const;\n\n/** All reporter messages for this library */\nconst messages = {\n ...validationMessages,\n ...publicMessages\n} as const;\n\n/** The runtime reporter for react-children-hooks */\nconst reporter = createReporter(\n process.env.NODE_ENV === \"production\" ? ({} as typeof messages) : messages,\n { formatMessage: (message) => message }\n);\n\nexport default reporter;\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, QueryOptions } from \"./types\";\nimport { useChildMatching } from \"./useChildMatching\";\n\n/**\n * Returns the first direct child element whose React element type exactly matches the provided type.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param options Optional query metadata used to configure how child elements are inspected.\n * @returns The first direct child element whose type matches the provided element type, or `null` when no match is found.\n */\nexport function useChildByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n options?: QueryOptions\n): ElementOfType<T> | null {\n return useChildMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n options\n );\n}\n","import { useMemo, type ReactNode } from \"react\";\n\nimport type { CallbackChild, CallbackChildren } from \"./types\";\n\nfunction findFirstCallbackChild<TArguments extends unknown[], TResult>(\n children: CallbackChildren<TArguments, TResult>\n): CallbackChild<TArguments, TResult> | null {\n if (typeof children === \"function\") {\n return children;\n }\n\n if (!Array.isArray(children)) {\n return null;\n }\n\n for (const child of children) {\n const callbackChild = findFirstCallbackChild<TArguments, TResult>(\n child\n );\n\n if (callbackChild) {\n return callbackChild;\n }\n }\n\n return null;\n}\n\n/**\n * Returns the first direct callback child from the provided children value.\n *\n * @param children The direct children value to inspect.\n * @returns The first direct callback child, or `null` when no callback child is found.\n */\nexport function useCallbackChild<\n TArguments extends unknown[] = [],\n TResult = ReactNode\n>(\n children: CallbackChildren<TArguments, TResult>\n): CallbackChild<TArguments, TResult> | null {\n return useMemo(() => findFirstCallbackChild(children), [children]);\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\nimport type { QueryOptions } from \"./types\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it should be included in the result.\n * @param options Optional query metadata used to configure how child elements are inspected.\n * @returns An array of direct child elements that satisfy the provided predicate.\n */\nexport function useChildrenMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n options?: QueryOptions\n): T[];\nexport function useChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: QueryOptions\n): ReactElement[];\nexport function useChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: QueryOptions\n): ReactElement[] {\n return useMemo(\n () => childrenToElements(children, options).filter(predicate),\n [children, options, predicate]\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ChildrenCountBounds, ValidationOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when the count falls outside the inclusive bounds.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param bounds The inclusive minimum and maximum number of matching direct child elements allowed.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useBoundedChildrenMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n bounds: ChildrenCountBounds,\n options?: ValidationOptions\n): T[];\nexport function useBoundedChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n bounds: ChildrenCountBounds,\n options?: ValidationOptions\n): ReactElement[];\nexport function useBoundedChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n bounds: ChildrenCountBounds,\n options?: ValidationOptions\n): ReactElement[] {\n const matchingChildren = useChildrenMatching(children, predicate, options);\n\n if (\n matchingChildren.length >= bounds.minimum &&\n matchingChildren.length <= bounds.maximum\n ) {\n return matchingChildren;\n }\n\n return reporter.fail(\"BOUNDED_CHILDREN_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n minimumCount: bounds.minimum,\n maximumCount: bounds.maximum\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type {\n ChildrenCountBounds,\n ElementOfType,\n ValidationOptions\n} from \"./types\";\nimport { useBoundedChildrenMatching } from \"./useBoundedChildrenMatching\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type, or throws when the count falls outside the inclusive bounds.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param bounds The inclusive minimum and maximum number of matching direct child elements allowed.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements whose type matches the provided element type.\n */\nexport function useBoundedChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n bounds: ChildrenCountBounds,\n options?: ValidationOptions\n): ElementOfType<T>[] {\n return useBoundedChildrenMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n bounds,\n options\n );\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, QueryOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param options Optional query metadata used to configure how child elements are inspected.\n * @returns An array of direct child elements whose type matches the provided element type.\n */\nexport function useChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n options?: QueryOptions\n): ElementOfType<T>[] {\n return useChildrenMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n options\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ValidationOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when the exact count is not met.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param exactCount The exact number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useExactChildrenMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n exactCount: number,\n options?: ValidationOptions\n): T[];\nexport function useExactChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n exactCount: number,\n options?: ValidationOptions\n): ReactElement[];\nexport function useExactChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n exactCount: number,\n options?: ValidationOptions\n): ReactElement[] {\n const matchingChildren = useChildrenMatching(children, predicate, options);\n\n if (matchingChildren.length === exactCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"EXACT_CHILDREN_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n exactCount\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, ValidationOptions } from \"./types\";\nimport { useExactChildrenMatching } from \"./useExactChildrenMatching\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type, or throws when the exact count is not met.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param exactCount The exact number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements whose type matches the provided element type.\n */\nexport function useExactChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n exactCount: number,\n options?: ValidationOptions\n): ElementOfType<T>[] {\n return useExactChildrenMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n exactCount,\n options\n );\n}\n","import { useMemo, type ReactElement, type ReactNode } from \"react\";\n\nimport { childrenToElements } from \"./childrenToElements\";\nimport type { QueryOptions } from \"./types\";\n\n/**\n * Determines whether any direct child element satisfies the provided predicate.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param options Optional query metadata used to configure how child elements are inspected.\n * @returns `true` when at least one direct child element satisfies the provided predicate; otherwise `false`.\n */\nexport function useHasChildMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n options?: QueryOptions\n): boolean;\nexport function useHasChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: QueryOptions\n): boolean;\nexport function useHasChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: QueryOptions\n): boolean {\n return useMemo(\n () => childrenToElements(children, options).some(predicate),\n [children, options, predicate]\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ValidationOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when more than the maximum count are found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param maximumCount The maximum number of matching direct child elements allowed.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useMaximumChildrenMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n maximumCount: number,\n options?: ValidationOptions\n): T[];\nexport function useMaximumChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n maximumCount: number,\n options?: ValidationOptions\n): ReactElement[];\nexport function useMaximumChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n maximumCount: number,\n options?: ValidationOptions\n): ReactElement[] {\n const matchingChildren = useChildrenMatching(children, predicate, options);\n\n if (matchingChildren.length <= maximumCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n maximumCount\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, ValidationOptions } from \"./types\";\nimport { useMaximumChildrenMatching } from \"./useMaximumChildrenMatching\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type, or throws when more than the maximum count are found.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param maximumCount The maximum number of matching direct child elements allowed.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements whose type matches the provided element type.\n */\nexport function useMaximumChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n maximumCount: number,\n options?: ValidationOptions\n): ElementOfType<T>[] {\n return useMaximumChildrenMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n maximumCount,\n options\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ValidationOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the direct child elements that satisfy the provided predicate, or throws when fewer than the minimum count are found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param minimumCount The minimum number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements that satisfy the provided predicate.\n */\nexport function useMinimumChildrenMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n minimumCount: number,\n options?: ValidationOptions\n): T[];\nexport function useMinimumChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n minimumCount: number,\n options?: ValidationOptions\n): ReactElement[];\nexport function useMinimumChildrenMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n minimumCount: number,\n options?: ValidationOptions\n): ReactElement[] {\n const matchingChildren = useChildrenMatching(children, predicate, options);\n\n if (matchingChildren.length >= minimumCount) {\n return matchingChildren;\n }\n\n return reporter.fail(\"MINIMUM_CHILDREN_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\",\n minimumCount\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, ValidationOptions } from \"./types\";\nimport { useMinimumChildrenMatching } from \"./useMinimumChildrenMatching\";\n\n/**\n * Returns the direct child elements whose React element type exactly matches the provided type, or throws when fewer than the minimum count are found.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param minimumCount The minimum number of matching direct child elements required.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The direct child elements whose type matches the provided element type.\n */\nexport function useMinimumChildrenByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n minimumCount: number,\n options?: ValidationOptions\n): ElementOfType<T>[] {\n return useMinimumChildrenMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n minimumCount,\n options\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ValidationOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the optional direct child element that satisfies the provided predicate, or throws when more than one match is found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The optional direct child element that satisfies the provided predicate, or `null` when no match is found.\n */\nexport function useOptionalChildMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n options?: ValidationOptions\n): T | null;\nexport function useOptionalChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: ValidationOptions\n): ReactElement | null;\nexport function useOptionalChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: ValidationOptions\n): ReactElement | null {\n const matchingChildren = useChildrenMatching(children, predicate, options);\n\n if (matchingChildren.length <= 1) {\n return matchingChildren[0] ?? null;\n }\n\n return reporter.fail(\"OPTIONAL_CHILD_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\"\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, ValidationOptions } from \"./types\";\nimport { useOptionalChildMatching } from \"./useOptionalChildMatching\";\n\n/**\n * Returns the optional direct child element whose React element type exactly matches the provided type, or throws when more than one match is found.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The optional direct child element whose type matches the provided element type, or `null` when no match is found.\n */\nexport function useOptionalChildByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n options?: ValidationOptions\n): ElementOfType<T> | null {\n return useOptionalChildMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n options\n );\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ValidationOptions } from \"./types\";\nimport { useChildMatching } from \"./useChildMatching\";\n\n/**\n * Returns the first direct child element that satisfies the provided predicate, or throws when no match is found.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The first direct child element that satisfies the provided predicate.\n */\nexport function useRequiredChildMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n options?: ValidationOptions\n): T;\nexport function useRequiredChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: ValidationOptions\n): ReactElement;\nexport function useRequiredChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: ValidationOptions\n): ReactElement {\n const child = useChildMatching(children, predicate, options);\n\n if (child !== null) {\n return child;\n }\n\n return reporter.fail(\"REQUIRED_CHILD_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\"\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, ValidationOptions } from \"./types\";\nimport { useRequiredChildMatching } from \"./useRequiredChildMatching\";\n\n/**\n * Returns the first direct child element whose React element type exactly matches the provided type, or throws when no match is found.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The first direct child element whose type matches the provided element type.\n */\nexport function useRequiredChildByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n options?: ValidationOptions\n): ElementOfType<T> {\n return useRequiredChildMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n options\n );\n}\n","import { type ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type {\n CallbackChild,\n CallbackChildren,\n ValidationOptions\n} from \"./types\";\nimport { useCallbackChild } from \"./useCallbackChild\";\n\n/**\n * Returns the first direct callback child from the provided children value, or throws when none is found.\n *\n * @param children The direct children value to inspect.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The first direct callback child from the provided children value.\n */\nexport function useRequiredCallbackChild<\n TArguments extends unknown[] = [],\n TResult = ReactNode\n>(\n children: CallbackChildren<TArguments, TResult>,\n options?: ValidationOptions\n): CallbackChild<TArguments, TResult> {\n const callbackChild = useCallbackChild(children);\n\n if (callbackChild !== null) {\n return callbackChild;\n }\n\n return reporter.fail(\"REQUIRED_CALLBACK_CHILD_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\"\n });\n}\n","import type { ReactElement, ReactNode } from \"react\";\n\nimport reporter from \"./reporter\";\nimport type { ValidationOptions } from \"./types\";\nimport { useChildrenMatching } from \"./useChildrenMatching\";\n\n/**\n * Returns the only direct child element that satisfies the provided predicate, or throws when the match is not unique.\n *\n * @param children The React children value to inspect.\n * @param predicate A predicate that is called with each direct child element to determine whether it matches.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The only direct child element that satisfies the provided predicate.\n */\nexport function useUniqueChildMatching<T extends ReactElement>(\n children: ReactNode,\n predicate: (element: ReactElement) => element is T,\n options?: ValidationOptions\n): T;\nexport function useUniqueChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: ValidationOptions\n): ReactElement;\nexport function useUniqueChildMatching(\n children: ReactNode,\n predicate: (element: ReactElement) => boolean,\n options?: ValidationOptions\n): ReactElement {\n const matchingChildren = useChildrenMatching(children, predicate, options);\n\n if (matchingChildren.length === 1) {\n return matchingChildren[0] as ReactElement;\n }\n\n return reporter.fail(\"UNIQUE_CHILD_MATCHING_PREDICATE_FAILED\", {\n traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : \"\",\n childNameSegment: options?.childName ? ` for ${options.childName}` : \"\",\n actualCount: matchingChildren.length,\n actualCountPluralSuffix: matchingChildren.length === 1 ? \"\" : \"ren\"\n });\n}\n","import type { ElementType, ReactNode } from \"react\";\n\nimport { isElementOfType } from \"./isElementOfType\";\nimport type { ElementOfType, ValidationOptions } from \"./types\";\nimport { useUniqueChildMatching } from \"./useUniqueChildMatching\";\n\n/**\n * Returns the only direct child element whose React element type exactly matches the provided type, or throws when the match is not unique.\n *\n * @param children The React children value to inspect.\n * @param type The element or component type to match against each direct child element.\n * @param options Optional reporting metadata used to derive the thrown validation message.\n * @returns The only direct child element whose type matches the provided element type.\n */\nexport function useUniqueChildByType<T extends ElementType>(\n children: ReactNode,\n type: T,\n options?: ValidationOptions\n): ElementOfType<T> {\n return useUniqueChildMatching(\n children,\n (element): element is ElementOfType<T> =>\n isElementOfType(element, type),\n options\n );\n}\n"],"mappings":";AAWO,SAAS,gBACZ,SACA,MAC2B;AAC3B,SAAO,QAAQ,SAAS;AAC5B;;;AChBA,SAAS,eAAkD;;;ACA3D,SAAS,gBAAmD;;;ACA5D,SAAS,sBAAyD;AAQ3D,SAAS,eAAe,MAAuC;AAClE,SAAO,eAAe,IAAI;AAC9B;;;ACVA,SAAS,sBAAsB;AAG/B,IAAM,qBAAqB;AAAA,EACvB,0CACI;AAAA,EACJ,wCACI;AAAA,EACJ,0CACI;AAAA,EACJ,gCACI;AAAA,EACJ,4CACI;AAAA,EACJ,4CACI;AAAA,EACJ,0CACI;AAAA,EACJ,4CACI;AACR;AAGA,IAAM,iBAAiB;AAAA,EACnB,QAAQ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KAAK,0CAA0C;AAAA,IAC3D,iBAAiB,SAAS,YAAY,IAAI,QAAQ,SAAS,OAAO;AAAA,IAClE,kBAAkB,SAAS,YAAY,QAAQ,QAAQ,SAAS,KAAK;AAAA,IACrE,aAAa,iBAAiB;AAAA,IAC9B,yBAAyB,iBAAiB,WAAW,IAAI,KAAK;AAAA,EAClE,CAAC;AACL;;;AC3BO,SAAS,qBACZ,UACA,MACA,SACgB;AAChB,SAAO;AAAA,IACH;AAAA,IACA,CAAC,YACG,gBAAgB,SAAS,IAAI;AAAA,IACjC;AAAA,EACJ;AACJ;","names":["useMemo","useMemo","useMemo","useMemo","useMemo"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-children-hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "React hooks for inspecting, traversing, querying, and validating props.children.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -86,6 +86,6 @@
|
|
|
86
86
|
"vitest": "^3.1.4"
|
|
87
87
|
},
|
|
88
88
|
"dependencies": {
|
|
89
|
-
"runtime-reporter": "^0.
|
|
89
|
+
"runtime-reporter": "^0.9.0"
|
|
90
90
|
}
|
|
91
91
|
}
|