react-children-hooks 0.3.0 → 0.5.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 +16 -10
- package/dist/index.cjs +245 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +115 -23
- package/dist/index.d.ts +115 -23
- package/dist/index.js +236 -55
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -6,12 +6,13 @@
|
|
|
6
6
|
|
|
7
7
|
### Query Hooks
|
|
8
8
|
|
|
9
|
+
- [`useCallbackChild`](./docs/useCallbackChild.md)
|
|
9
10
|
- [`useChildByType`](./docs/useChildByType.md)
|
|
10
11
|
- [`useChildMatching`](./docs/useChildMatching.md)
|
|
11
12
|
- [`useChildrenByType`](./docs/useChildrenByType.md)
|
|
12
13
|
- [`useChildrenMatching`](./docs/useChildrenMatching.md)
|
|
13
14
|
|
|
14
|
-
###
|
|
15
|
+
### Query + Validation Hooks
|
|
15
16
|
|
|
16
17
|
- [`useBoundedChildrenByType`](./docs/useBoundedChildrenByType.md)
|
|
17
18
|
- [`useBoundedChildrenMatching`](./docs/useBoundedChildrenMatching.md)
|
|
@@ -21,21 +22,26 @@
|
|
|
21
22
|
- [`useMaximumChildrenMatching`](./docs/useMaximumChildrenMatching.md)
|
|
22
23
|
- [`useMinimumChildrenByType`](./docs/useMinimumChildrenByType.md)
|
|
23
24
|
- [`useMinimumChildrenMatching`](./docs/useMinimumChildrenMatching.md)
|
|
25
|
+
- [`useOptionalCallbackChild`](./docs/useOptionalCallbackChild.md)
|
|
26
|
+
- [`useOptionalChildByType`](./docs/useOptionalChildByType.md)
|
|
27
|
+
- [`useOptionalChildMatching`](./docs/useOptionalChildMatching.md)
|
|
28
|
+
- [`useRequiredCallbackChild`](./docs/useRequiredCallbackChild.md)
|
|
24
29
|
- [`useRequiredChildByType`](./docs/useRequiredChildByType.md)
|
|
25
30
|
- [`useRequiredChildMatching`](./docs/useRequiredChildMatching.md)
|
|
31
|
+
- [`useUniqueCallbackChild`](./docs/useUniqueCallbackChild.md)
|
|
32
|
+
- [`useUniqueChildByType`](./docs/useUniqueChildByType.md)
|
|
33
|
+
- [`useUniqueChildMatching`](./docs/useUniqueChildMatching.md)
|
|
26
34
|
|
|
27
35
|
### Validation Hooks
|
|
28
36
|
|
|
29
37
|
- [`useHasChildMatching`](./docs/useHasChildMatching.md)
|
|
30
38
|
|
|
31
|
-
### Utilities
|
|
32
|
-
|
|
33
|
-
- [`childrenToElements`](./docs/childrenToElements.md)
|
|
34
|
-
- [`isElementOfType`](./docs/isElementOfType.md)
|
|
35
|
-
- [`isReactElement`](./docs/isReactElement.md)
|
|
36
|
-
|
|
37
39
|
### Types
|
|
38
40
|
|
|
39
|
-
- `
|
|
40
|
-
- `
|
|
41
|
-
- `
|
|
41
|
+
- [`CallbackChild`](./src/types.ts)
|
|
42
|
+
- [`CallbackChildren`](./src/types.ts)
|
|
43
|
+
- [`ChildrenCountBounds`](./src/types.ts)
|
|
44
|
+
- [`ElementOfType`](./src/types.ts)
|
|
45
|
+
- [`QueryOptions`](./src/types.ts)
|
|
46
|
+
- [`TraversalOptions`](./src/types.ts)
|
|
47
|
+
- [`ValidationOptions`](./src/types.ts)
|
package/dist/index.cjs
CHANGED
|
@@ -20,24 +20,29 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
childrenToElements: () => childrenToElements,
|
|
24
|
-
isElementOfType: () => isElementOfType,
|
|
25
|
-
isReactElement: () => isReactElement,
|
|
26
23
|
useBoundedChildrenByType: () => useBoundedChildrenByType,
|
|
27
|
-
useBoundedChildrenMatching: () =>
|
|
24
|
+
useBoundedChildrenMatching: () => useBoundedChildrenMatching_default,
|
|
25
|
+
useCallbackChild: () => useCallbackChild,
|
|
28
26
|
useChildByType: () => useChildByType,
|
|
29
|
-
useChildMatching: () =>
|
|
27
|
+
useChildMatching: () => useChildMatching_default,
|
|
30
28
|
useChildrenByType: () => useChildrenByType,
|
|
31
|
-
useChildrenMatching: () =>
|
|
29
|
+
useChildrenMatching: () => useChildrenMatching_default,
|
|
32
30
|
useExactChildrenByType: () => useExactChildrenByType,
|
|
33
|
-
useExactChildrenMatching: () =>
|
|
34
|
-
useHasChildMatching: () =>
|
|
31
|
+
useExactChildrenMatching: () => useExactChildrenMatching_default,
|
|
32
|
+
useHasChildMatching: () => useHasChildMatching_default,
|
|
35
33
|
useMaximumChildrenByType: () => useMaximumChildrenByType,
|
|
36
|
-
useMaximumChildrenMatching: () =>
|
|
34
|
+
useMaximumChildrenMatching: () => useMaximumChildrenMatching_default,
|
|
37
35
|
useMinimumChildrenByType: () => useMinimumChildrenByType,
|
|
38
|
-
useMinimumChildrenMatching: () =>
|
|
36
|
+
useMinimumChildrenMatching: () => useMinimumChildrenMatching_default,
|
|
37
|
+
useOptionalCallbackChild: () => useOptionalCallbackChild,
|
|
38
|
+
useOptionalChildByType: () => useOptionalChildByType,
|
|
39
|
+
useOptionalChildMatching: () => useOptionalChildMatching_default,
|
|
40
|
+
useRequiredCallbackChild: () => useRequiredCallbackChild,
|
|
39
41
|
useRequiredChildByType: () => useRequiredChildByType,
|
|
40
|
-
useRequiredChildMatching: () =>
|
|
42
|
+
useRequiredChildMatching: () => useRequiredChildMatching_default,
|
|
43
|
+
useUniqueCallbackChild: () => useUniqueCallbackChild,
|
|
44
|
+
useUniqueChildByType: () => useUniqueChildByType,
|
|
45
|
+
useUniqueChildMatching: () => useUniqueChildMatching_default
|
|
41
46
|
});
|
|
42
47
|
module.exports = __toCommonJS(index_exports);
|
|
43
48
|
|
|
@@ -58,54 +63,128 @@ function isReactElement(node) {
|
|
|
58
63
|
return (0, import_react.isValidElement)(node);
|
|
59
64
|
}
|
|
60
65
|
|
|
61
|
-
// src/childrenToElements.ts
|
|
62
|
-
function childrenToElements(children) {
|
|
63
|
-
return import_react2.Children.toArray(children).filter(isReactElement);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// src/useChildMatching.ts
|
|
67
|
-
function useChildMatching(children, predicate) {
|
|
68
|
-
return (0, import_react3.useMemo)(
|
|
69
|
-
() => childrenToElements(children).find(predicate) ?? null,
|
|
70
|
-
[children, predicate]
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// src/useChildByType.ts
|
|
75
|
-
function useChildByType(children, type) {
|
|
76
|
-
return useChildMatching(
|
|
77
|
-
children,
|
|
78
|
-
(element) => isElementOfType(element, type)
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
66
|
// src/reporter.ts
|
|
83
67
|
var import_runtime_reporter = require("runtime-reporter");
|
|
84
|
-
var
|
|
68
|
+
var validationMessages = {
|
|
69
|
+
OPTIONAL_CHILD_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Optional child validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most 1.",
|
|
70
|
+
UNIQUE_CHILD_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Unique child validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly 1.",
|
|
85
71
|
REQUIRED_CHILD_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.",
|
|
72
|
+
REQUIRED_CALLBACK_CHILD_FAILED: "{{ traceCodePrefix }}Required callback child validation failed{{ childNameSegment }} because no direct callback child was found.",
|
|
73
|
+
OPTIONAL_CALLBACK_CHILD_FAILED: "{{ traceCodePrefix }}Optional callback child validation failed{{ childNameSegment }} because {{ actualCount }} direct callback children were found; expected at most 1.",
|
|
74
|
+
UNIQUE_CALLBACK_CHILD_FAILED: "{{ traceCodePrefix }}Unique callback child validation failed{{ childNameSegment }} because {{ actualCount }} direct callback children were found; expected exactly 1.",
|
|
86
75
|
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 }}.",
|
|
87
76
|
MAXIMUM_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Maximum children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at most {{ maximumCount }}.",
|
|
88
77
|
EXACT_CHILDREN_MATCHING_PREDICATE_FAILED: "{{ traceCodePrefix }}Exact children validation failed{{ childNameSegment }} because {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected exactly {{ exactCount }}.",
|
|
89
78
|
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."
|
|
90
79
|
};
|
|
80
|
+
var publicMessages = {
|
|
81
|
+
RCH001: '[RCH001] Traversal option "depth" must be a non-negative integer.',
|
|
82
|
+
RCH002: '[RCH002] Traversal option "maximumDepth" must be a non-negative integer.',
|
|
83
|
+
RCH003: '[RCH003] Traversal option "depth" cannot be greater than "maximumDepth".'
|
|
84
|
+
};
|
|
85
|
+
var messages = {
|
|
86
|
+
...validationMessages,
|
|
87
|
+
...publicMessages
|
|
88
|
+
};
|
|
91
89
|
var reporter = (0, import_runtime_reporter.createReporter)(
|
|
92
90
|
process.env.NODE_ENV === "production" ? {} : messages,
|
|
93
91
|
{ formatMessage: (message) => message }
|
|
94
92
|
);
|
|
95
93
|
var reporter_default = reporter;
|
|
96
94
|
|
|
97
|
-
// src/
|
|
95
|
+
// src/childrenToElements.ts
|
|
96
|
+
function getTraversalBounds(options) {
|
|
97
|
+
const depth = options?.depth ?? 0;
|
|
98
|
+
const maximumDepth = options?.maximumDepth ?? 0;
|
|
99
|
+
if (!Number.isInteger(depth) || depth < 0) {
|
|
100
|
+
return reporter_default.fail("RCH001");
|
|
101
|
+
}
|
|
102
|
+
if (!Number.isInteger(maximumDepth) || maximumDepth < 0) {
|
|
103
|
+
return reporter_default.fail("RCH002");
|
|
104
|
+
}
|
|
105
|
+
if (depth > maximumDepth) {
|
|
106
|
+
return reporter_default.fail("RCH003");
|
|
107
|
+
}
|
|
108
|
+
return { depth, maximumDepth };
|
|
109
|
+
}
|
|
110
|
+
function collectElementsAtDepth(children, currentDepth, depth, maximumDepth) {
|
|
111
|
+
const directElements = import_react2.Children.toArray(children).filter(isReactElement);
|
|
112
|
+
let elements = [];
|
|
113
|
+
if (currentDepth >= depth && currentDepth <= maximumDepth) {
|
|
114
|
+
elements = [...directElements];
|
|
115
|
+
}
|
|
116
|
+
for (const element of directElements) {
|
|
117
|
+
const elementProps = element.props;
|
|
118
|
+
const elementsAtDepth = collectElementsAtDepth(
|
|
119
|
+
elementProps.children,
|
|
120
|
+
currentDepth + 1,
|
|
121
|
+
depth,
|
|
122
|
+
maximumDepth
|
|
123
|
+
);
|
|
124
|
+
elements.push(...elementsAtDepth);
|
|
125
|
+
}
|
|
126
|
+
return elements;
|
|
127
|
+
}
|
|
128
|
+
function childrenToElements(children, options) {
|
|
129
|
+
const { depth, maximumDepth } = getTraversalBounds(options);
|
|
130
|
+
return collectElementsAtDepth(children, 0, depth, maximumDepth);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/useChildMatching.ts
|
|
134
|
+
function useChildMatching(children, predicate, options) {
|
|
135
|
+
return (0, import_react3.useMemo)(
|
|
136
|
+
() => childrenToElements(children, options).find(predicate) ?? null,
|
|
137
|
+
[children, options, predicate]
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
var useChildMatching_default = useChildMatching;
|
|
141
|
+
|
|
142
|
+
// src/useChildByType.ts
|
|
143
|
+
function useChildByType(children, type, options) {
|
|
144
|
+
return useChildMatching_default(
|
|
145
|
+
children,
|
|
146
|
+
(element) => isElementOfType(element, type),
|
|
147
|
+
options
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/useCallbackChild.ts
|
|
98
152
|
var import_react4 = require("react");
|
|
99
|
-
|
|
153
|
+
|
|
154
|
+
// src/callbackChildrenToArray.ts
|
|
155
|
+
function callbackChildrenToArray(children) {
|
|
156
|
+
if (typeof children === "function") {
|
|
157
|
+
return [children];
|
|
158
|
+
}
|
|
159
|
+
if (!Array.isArray(children)) {
|
|
160
|
+
return [];
|
|
161
|
+
}
|
|
162
|
+
return children.flatMap(
|
|
163
|
+
(child) => callbackChildrenToArray(child)
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// src/useCallbackChild.ts
|
|
168
|
+
function useCallbackChild(children) {
|
|
100
169
|
return (0, import_react4.useMemo)(
|
|
101
|
-
() =>
|
|
102
|
-
[children
|
|
170
|
+
() => callbackChildrenToArray(children)[0] ?? null,
|
|
171
|
+
[children]
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/useChildrenMatching.ts
|
|
176
|
+
var import_react5 = require("react");
|
|
177
|
+
function useChildrenMatching(children, predicate, options) {
|
|
178
|
+
return (0, import_react5.useMemo)(
|
|
179
|
+
() => childrenToElements(children, options).filter(predicate),
|
|
180
|
+
[children, options, predicate]
|
|
103
181
|
);
|
|
104
182
|
}
|
|
183
|
+
var useChildrenMatching_default = useChildrenMatching;
|
|
105
184
|
|
|
106
185
|
// src/useBoundedChildrenMatching.ts
|
|
107
186
|
function useBoundedChildrenMatching(children, predicate, bounds, options) {
|
|
108
|
-
const matchingChildren =
|
|
187
|
+
const matchingChildren = useChildrenMatching_default(children, predicate, options);
|
|
109
188
|
if (matchingChildren.length >= bounds.minimum && matchingChildren.length <= bounds.maximum) {
|
|
110
189
|
return matchingChildren;
|
|
111
190
|
}
|
|
@@ -118,10 +197,11 @@ function useBoundedChildrenMatching(children, predicate, bounds, options) {
|
|
|
118
197
|
maximumCount: bounds.maximum
|
|
119
198
|
});
|
|
120
199
|
}
|
|
200
|
+
var useBoundedChildrenMatching_default = useBoundedChildrenMatching;
|
|
121
201
|
|
|
122
202
|
// src/useBoundedChildrenByType.ts
|
|
123
203
|
function useBoundedChildrenByType(children, type, bounds, options) {
|
|
124
|
-
return
|
|
204
|
+
return useBoundedChildrenMatching_default(
|
|
125
205
|
children,
|
|
126
206
|
(element) => isElementOfType(element, type),
|
|
127
207
|
bounds,
|
|
@@ -130,16 +210,17 @@ function useBoundedChildrenByType(children, type, bounds, options) {
|
|
|
130
210
|
}
|
|
131
211
|
|
|
132
212
|
// src/useChildrenByType.ts
|
|
133
|
-
function useChildrenByType(children, type) {
|
|
134
|
-
return
|
|
213
|
+
function useChildrenByType(children, type, options) {
|
|
214
|
+
return useChildrenMatching_default(
|
|
135
215
|
children,
|
|
136
|
-
(element) => isElementOfType(element, type)
|
|
216
|
+
(element) => isElementOfType(element, type),
|
|
217
|
+
options
|
|
137
218
|
);
|
|
138
219
|
}
|
|
139
220
|
|
|
140
221
|
// src/useExactChildrenMatching.ts
|
|
141
222
|
function useExactChildrenMatching(children, predicate, exactCount, options) {
|
|
142
|
-
const matchingChildren =
|
|
223
|
+
const matchingChildren = useChildrenMatching_default(children, predicate, options);
|
|
143
224
|
if (matchingChildren.length === exactCount) {
|
|
144
225
|
return matchingChildren;
|
|
145
226
|
}
|
|
@@ -151,10 +232,11 @@ function useExactChildrenMatching(children, predicate, exactCount, options) {
|
|
|
151
232
|
exactCount
|
|
152
233
|
});
|
|
153
234
|
}
|
|
235
|
+
var useExactChildrenMatching_default = useExactChildrenMatching;
|
|
154
236
|
|
|
155
237
|
// src/useExactChildrenByType.ts
|
|
156
238
|
function useExactChildrenByType(children, type, exactCount, options) {
|
|
157
|
-
return
|
|
239
|
+
return useExactChildrenMatching_default(
|
|
158
240
|
children,
|
|
159
241
|
(element) => isElementOfType(element, type),
|
|
160
242
|
exactCount,
|
|
@@ -163,17 +245,18 @@ function useExactChildrenByType(children, type, exactCount, options) {
|
|
|
163
245
|
}
|
|
164
246
|
|
|
165
247
|
// src/useHasChildMatching.ts
|
|
166
|
-
var
|
|
167
|
-
function useHasChildMatching(children, predicate) {
|
|
168
|
-
return (0,
|
|
169
|
-
() => childrenToElements(children).some(predicate),
|
|
170
|
-
[children, predicate]
|
|
248
|
+
var import_react6 = require("react");
|
|
249
|
+
function useHasChildMatching(children, predicate, options) {
|
|
250
|
+
return (0, import_react6.useMemo)(
|
|
251
|
+
() => childrenToElements(children, options).some(predicate),
|
|
252
|
+
[children, options, predicate]
|
|
171
253
|
);
|
|
172
254
|
}
|
|
255
|
+
var useHasChildMatching_default = useHasChildMatching;
|
|
173
256
|
|
|
174
257
|
// src/useMaximumChildrenMatching.ts
|
|
175
258
|
function useMaximumChildrenMatching(children, predicate, maximumCount, options) {
|
|
176
|
-
const matchingChildren =
|
|
259
|
+
const matchingChildren = useChildrenMatching_default(children, predicate, options);
|
|
177
260
|
if (matchingChildren.length <= maximumCount) {
|
|
178
261
|
return matchingChildren;
|
|
179
262
|
}
|
|
@@ -185,10 +268,11 @@ function useMaximumChildrenMatching(children, predicate, maximumCount, options)
|
|
|
185
268
|
maximumCount
|
|
186
269
|
});
|
|
187
270
|
}
|
|
271
|
+
var useMaximumChildrenMatching_default = useMaximumChildrenMatching;
|
|
188
272
|
|
|
189
273
|
// src/useMaximumChildrenByType.ts
|
|
190
274
|
function useMaximumChildrenByType(children, type, maximumCount, options) {
|
|
191
|
-
return
|
|
275
|
+
return useMaximumChildrenMatching_default(
|
|
192
276
|
children,
|
|
193
277
|
(element) => isElementOfType(element, type),
|
|
194
278
|
maximumCount,
|
|
@@ -198,7 +282,7 @@ function useMaximumChildrenByType(children, type, maximumCount, options) {
|
|
|
198
282
|
|
|
199
283
|
// src/useMinimumChildrenMatching.ts
|
|
200
284
|
function useMinimumChildrenMatching(children, predicate, minimumCount, options) {
|
|
201
|
-
const matchingChildren =
|
|
285
|
+
const matchingChildren = useChildrenMatching_default(children, predicate, options);
|
|
202
286
|
if (matchingChildren.length >= minimumCount) {
|
|
203
287
|
return matchingChildren;
|
|
204
288
|
}
|
|
@@ -210,10 +294,11 @@ function useMinimumChildrenMatching(children, predicate, minimumCount, options)
|
|
|
210
294
|
minimumCount
|
|
211
295
|
});
|
|
212
296
|
}
|
|
297
|
+
var useMinimumChildrenMatching_default = useMinimumChildrenMatching;
|
|
213
298
|
|
|
214
299
|
// src/useMinimumChildrenByType.ts
|
|
215
300
|
function useMinimumChildrenByType(children, type, minimumCount, options) {
|
|
216
|
-
return
|
|
301
|
+
return useMinimumChildrenMatching_default(
|
|
217
302
|
children,
|
|
218
303
|
(element) => isElementOfType(element, type),
|
|
219
304
|
minimumCount,
|
|
@@ -221,9 +306,50 @@ function useMinimumChildrenByType(children, type, minimumCount, options) {
|
|
|
221
306
|
);
|
|
222
307
|
}
|
|
223
308
|
|
|
309
|
+
// src/useOptionalCallbackChild.ts
|
|
310
|
+
var import_react7 = require("react");
|
|
311
|
+
function useOptionalCallbackChild(children, options) {
|
|
312
|
+
const callbackChildren = (0, import_react7.useMemo)(
|
|
313
|
+
() => callbackChildrenToArray(children),
|
|
314
|
+
[children]
|
|
315
|
+
);
|
|
316
|
+
if (callbackChildren.length <= 1) {
|
|
317
|
+
return callbackChildren[0] ?? null;
|
|
318
|
+
}
|
|
319
|
+
return reporter_default.fail("OPTIONAL_CALLBACK_CHILD_FAILED", {
|
|
320
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
321
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
322
|
+
actualCount: callbackChildren.length
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// src/useOptionalChildMatching.ts
|
|
327
|
+
function useOptionalChildMatching(children, predicate, options) {
|
|
328
|
+
const matchingChildren = useChildrenMatching_default(children, predicate, options);
|
|
329
|
+
if (matchingChildren.length <= 1) {
|
|
330
|
+
return matchingChildren[0] ?? null;
|
|
331
|
+
}
|
|
332
|
+
return reporter_default.fail("OPTIONAL_CHILD_MATCHING_PREDICATE_FAILED", {
|
|
333
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
334
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
335
|
+
actualCount: matchingChildren.length,
|
|
336
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren"
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
var useOptionalChildMatching_default = useOptionalChildMatching;
|
|
340
|
+
|
|
341
|
+
// src/useOptionalChildByType.ts
|
|
342
|
+
function useOptionalChildByType(children, type, options) {
|
|
343
|
+
return useOptionalChildMatching_default(
|
|
344
|
+
children,
|
|
345
|
+
(element) => isElementOfType(element, type),
|
|
346
|
+
options
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
|
|
224
350
|
// src/useRequiredChildMatching.ts
|
|
225
351
|
function useRequiredChildMatching(children, predicate, options) {
|
|
226
|
-
const child =
|
|
352
|
+
const child = useChildMatching_default(children, predicate, options);
|
|
227
353
|
if (child !== null) {
|
|
228
354
|
return child;
|
|
229
355
|
}
|
|
@@ -232,10 +358,65 @@ function useRequiredChildMatching(children, predicate, options) {
|
|
|
232
358
|
childNameSegment: options?.childName ? ` for ${options.childName}` : ""
|
|
233
359
|
});
|
|
234
360
|
}
|
|
361
|
+
var useRequiredChildMatching_default = useRequiredChildMatching;
|
|
235
362
|
|
|
236
363
|
// src/useRequiredChildByType.ts
|
|
237
364
|
function useRequiredChildByType(children, type, options) {
|
|
238
|
-
return
|
|
365
|
+
return useRequiredChildMatching_default(
|
|
366
|
+
children,
|
|
367
|
+
(element) => isElementOfType(element, type),
|
|
368
|
+
options
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// src/useRequiredCallbackChild.ts
|
|
373
|
+
var import_react8 = require("react");
|
|
374
|
+
function useRequiredCallbackChild(children, options) {
|
|
375
|
+
const callbackChild = useCallbackChild(children);
|
|
376
|
+
if (callbackChild !== null) {
|
|
377
|
+
return callbackChild;
|
|
378
|
+
}
|
|
379
|
+
return reporter_default.fail("REQUIRED_CALLBACK_CHILD_FAILED", {
|
|
380
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
381
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : ""
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// src/useUniqueCallbackChild.ts
|
|
386
|
+
var import_react9 = require("react");
|
|
387
|
+
function useUniqueCallbackChild(children, options) {
|
|
388
|
+
const callbackChildren = (0, import_react9.useMemo)(
|
|
389
|
+
() => callbackChildrenToArray(children),
|
|
390
|
+
[children]
|
|
391
|
+
);
|
|
392
|
+
if (callbackChildren.length === 1) {
|
|
393
|
+
return callbackChildren[0];
|
|
394
|
+
}
|
|
395
|
+
return reporter_default.fail("UNIQUE_CALLBACK_CHILD_FAILED", {
|
|
396
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
397
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
398
|
+
actualCount: callbackChildren.length
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// src/useUniqueChildMatching.ts
|
|
403
|
+
function useUniqueChildMatching(children, predicate, options) {
|
|
404
|
+
const matchingChildren = useChildrenMatching_default(children, predicate, options);
|
|
405
|
+
if (matchingChildren.length === 1) {
|
|
406
|
+
return matchingChildren[0];
|
|
407
|
+
}
|
|
408
|
+
return reporter_default.fail("UNIQUE_CHILD_MATCHING_PREDICATE_FAILED", {
|
|
409
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
410
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
411
|
+
actualCount: matchingChildren.length,
|
|
412
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren"
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
var useUniqueChildMatching_default = useUniqueChildMatching;
|
|
416
|
+
|
|
417
|
+
// src/useUniqueChildByType.ts
|
|
418
|
+
function useUniqueChildByType(children, type, options) {
|
|
419
|
+
return useUniqueChildMatching_default(
|
|
239
420
|
children,
|
|
240
421
|
(element) => isElementOfType(element, type),
|
|
241
422
|
options
|
|
@@ -243,11 +424,9 @@ function useRequiredChildByType(children, type, options) {
|
|
|
243
424
|
}
|
|
244
425
|
// Annotate the CommonJS export names for ESM import in node:
|
|
245
426
|
0 && (module.exports = {
|
|
246
|
-
childrenToElements,
|
|
247
|
-
isElementOfType,
|
|
248
|
-
isReactElement,
|
|
249
427
|
useBoundedChildrenByType,
|
|
250
428
|
useBoundedChildrenMatching,
|
|
429
|
+
useCallbackChild,
|
|
251
430
|
useChildByType,
|
|
252
431
|
useChildMatching,
|
|
253
432
|
useChildrenByType,
|
|
@@ -259,7 +438,14 @@ function useRequiredChildByType(children, type, options) {
|
|
|
259
438
|
useMaximumChildrenMatching,
|
|
260
439
|
useMinimumChildrenByType,
|
|
261
440
|
useMinimumChildrenMatching,
|
|
441
|
+
useOptionalCallbackChild,
|
|
442
|
+
useOptionalChildByType,
|
|
443
|
+
useOptionalChildMatching,
|
|
444
|
+
useRequiredCallbackChild,
|
|
262
445
|
useRequiredChildByType,
|
|
263
|
-
useRequiredChildMatching
|
|
446
|
+
useRequiredChildMatching,
|
|
447
|
+
useUniqueCallbackChild,
|
|
448
|
+
useUniqueChildByType,
|
|
449
|
+
useUniqueChildMatching
|
|
264
450
|
});
|
|
265
451
|
//# sourceMappingURL=index.cjs.map
|