react-children-hooks 0.1.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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/index.cjs +151 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +127 -0
- package/dist/index.d.ts +127 -0
- package/dist/index.js +115 -0
- package/dist/index.js.map +1 -0
- package/package.json +91 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mark McCann
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# react-children-hooks
|
|
2
|
+
|
|
3
|
+
`react-children-hooks` is a TypeScript-first React library for inspecting, traversing, querying, and validating `props.children`.
|
|
4
|
+
|
|
5
|
+
## API Reference
|
|
6
|
+
|
|
7
|
+
### Query Hooks
|
|
8
|
+
|
|
9
|
+
- [`useChildByType`](./docs/api/useChildByType.md)
|
|
10
|
+
- [`useChildWhere`](./docs/api/useChildWhere.md)
|
|
11
|
+
- [`useChildrenByType`](./docs/api/useChildrenByType.md)
|
|
12
|
+
- [`useChildrenWhere`](./docs/api/useChildrenWhere.md)
|
|
13
|
+
|
|
14
|
+
### Validation Hooks
|
|
15
|
+
|
|
16
|
+
- [`useHasChildWhere`](./docs/api/useHasChildWhere.md)
|
|
17
|
+
- [`useMinimumChildrenWhere`](./docs/api/useMinimumChildrenWhere.md)
|
|
18
|
+
- [`useRequiredChildWhere`](./docs/api/useRequiredChildWhere.md)
|
|
19
|
+
|
|
20
|
+
### Utilities
|
|
21
|
+
|
|
22
|
+
- [`childrenToElements`](./docs/api/childrenToElements.md)
|
|
23
|
+
- [`isElementOfType`](./docs/api/isElementOfType.md)
|
|
24
|
+
- [`isReactElement`](./docs/api/isReactElement.md)
|
|
25
|
+
|
|
26
|
+
### Types
|
|
27
|
+
|
|
28
|
+
- [`ElementOfType`](./docs/api/ElementOfType.md)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
childrenToElements: () => childrenToElements,
|
|
24
|
+
isElementOfType: () => isElementOfType,
|
|
25
|
+
isReactElement: () => isReactElement,
|
|
26
|
+
useChildByType: () => useChildByType,
|
|
27
|
+
useChildWhere: () => useChildWhere,
|
|
28
|
+
useChildrenByType: () => useChildrenByType,
|
|
29
|
+
useChildrenWhere: () => useChildrenWhere,
|
|
30
|
+
useHasChildWhere: () => useHasChildWhere,
|
|
31
|
+
useMinimumChildrenWhere: () => useMinimumChildrenWhere,
|
|
32
|
+
useRequiredChildWhere: () => useRequiredChildWhere
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(index_exports);
|
|
35
|
+
|
|
36
|
+
// src/isElementOfType.ts
|
|
37
|
+
function isElementOfType(element, type) {
|
|
38
|
+
return element.type === type;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/useChildWhere.ts
|
|
42
|
+
var import_react3 = require("react");
|
|
43
|
+
|
|
44
|
+
// src/childrenToElements.ts
|
|
45
|
+
var import_react2 = require("react");
|
|
46
|
+
|
|
47
|
+
// src/isReactElement.ts
|
|
48
|
+
var import_react = require("react");
|
|
49
|
+
function isReactElement(node) {
|
|
50
|
+
return (0, import_react.isValidElement)(node);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/childrenToElements.ts
|
|
54
|
+
function childrenToElements(children) {
|
|
55
|
+
return import_react2.Children.toArray(children).filter(isReactElement);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/useChildWhere.ts
|
|
59
|
+
function useChildWhere(children, predicate) {
|
|
60
|
+
return (0, import_react3.useMemo)(
|
|
61
|
+
() => childrenToElements(children).find(predicate) ?? null,
|
|
62
|
+
[children, predicate]
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/useChildByType.ts
|
|
67
|
+
function useChildByType(children, type) {
|
|
68
|
+
return useChildWhere(
|
|
69
|
+
children,
|
|
70
|
+
(element) => isElementOfType(element, type)
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/useChildrenWhere.ts
|
|
75
|
+
var import_react4 = require("react");
|
|
76
|
+
function useChildrenWhere(children, predicate) {
|
|
77
|
+
return (0, import_react4.useMemo)(
|
|
78
|
+
() => childrenToElements(children).filter(predicate),
|
|
79
|
+
[children, predicate]
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/useChildrenByType.ts
|
|
84
|
+
function useChildrenByType(children, type) {
|
|
85
|
+
return useChildrenWhere(
|
|
86
|
+
children,
|
|
87
|
+
(element) => isElementOfType(element, type)
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/useHasChildWhere.ts
|
|
92
|
+
var import_react5 = require("react");
|
|
93
|
+
function useHasChildWhere(children, predicate) {
|
|
94
|
+
return (0, import_react5.useMemo)(
|
|
95
|
+
() => childrenToElements(children).some(predicate),
|
|
96
|
+
[children, predicate]
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/reporter.ts
|
|
101
|
+
var import_runtime_reporter = require("runtime-reporter");
|
|
102
|
+
var messages = {
|
|
103
|
+
REQUIRED_CHILD_WHERE_PREDICATE_FAILED: "{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.",
|
|
104
|
+
MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED: "{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}."
|
|
105
|
+
};
|
|
106
|
+
var reporter = (0, import_runtime_reporter.createReporter)(
|
|
107
|
+
process.env.NODE_ENV === "production" ? {} : messages,
|
|
108
|
+
{ formatMessage: (message) => message }
|
|
109
|
+
);
|
|
110
|
+
var reporter_default = reporter;
|
|
111
|
+
|
|
112
|
+
// src/useMinimumChildrenWhere.ts
|
|
113
|
+
function useMinimumChildrenWhere(children, predicate, minimumCount, options) {
|
|
114
|
+
const matchingChildren = useChildrenWhere(children, predicate);
|
|
115
|
+
if (matchingChildren.length >= minimumCount) {
|
|
116
|
+
return matchingChildren;
|
|
117
|
+
}
|
|
118
|
+
return reporter_default.fail("MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED", {
|
|
119
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
120
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
121
|
+
actualCount: matchingChildren.length,
|
|
122
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren",
|
|
123
|
+
minimumCount
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/useRequiredChildWhere.ts
|
|
128
|
+
function useRequiredChildWhere(children, predicate, options) {
|
|
129
|
+
const child = useChildWhere(children, predicate);
|
|
130
|
+
if (child !== null) {
|
|
131
|
+
return child;
|
|
132
|
+
}
|
|
133
|
+
return reporter_default.fail("REQUIRED_CHILD_WHERE_PREDICATE_FAILED", {
|
|
134
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
135
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : ""
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
139
|
+
0 && (module.exports = {
|
|
140
|
+
childrenToElements,
|
|
141
|
+
isElementOfType,
|
|
142
|
+
isReactElement,
|
|
143
|
+
useChildByType,
|
|
144
|
+
useChildWhere,
|
|
145
|
+
useChildrenByType,
|
|
146
|
+
useChildrenWhere,
|
|
147
|
+
useHasChildWhere,
|
|
148
|
+
useMinimumChildrenWhere,
|
|
149
|
+
useRequiredChildWhere
|
|
150
|
+
});
|
|
151
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/isElementOfType.ts","../src/useChildWhere.ts","../src/childrenToElements.ts","../src/isReactElement.ts","../src/useChildByType.ts","../src/useChildrenWhere.ts","../src/useChildrenByType.ts","../src/useHasChildWhere.ts","../src/reporter.ts","../src/useMinimumChildrenWhere.ts","../src/useRequiredChildWhere.ts"],"sourcesContent":["/**\n * Public package entrypoint.\n *\n * Hooks and child utilities will be exported here as the API is defined.\n */\nexport { useChildByType } from \"./useChildByType\";\nexport { useChildWhere } from \"./useChildWhere\";\nexport { useChildrenByType } from \"./useChildrenByType\";\nexport { useChildrenWhere } from \"./useChildrenWhere\";\nexport { useHasChildWhere } from \"./useHasChildWhere\";\nexport { useMinimumChildrenWhere } from \"./useMinimumChildrenWhere\";\nexport { useRequiredChildWhere } from \"./useRequiredChildWhere\";\nexport { childrenToElements } from \"./childrenToElements\";\nexport { isElementOfType } from \"./isElementOfType\";\nexport { isReactElement } from \"./isReactElement\";\nexport type { ElementOfType } from \"./types\";\nexport type { UseMinimumChildrenWhereOptions } from \"./useMinimumChildrenWhere\";\nexport type { UseRequiredChildWhereOptions } from \"./useRequiredChildWhere\";\n","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 { 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 { 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 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};\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 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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACWO,SAAS,gBACZ,SACA,MAC2B;AAC3B,SAAO,QAAQ,SAAS;AAC5B;;;AChBA,IAAAA,gBAA2D;;;ACA3D,IAAAC,gBAA4D;;;ACA5D,mBAAkE;AAQ3D,SAAS,eAAe,MAAuC;AAClE,aAAO,6BAAe,IAAI;AAC9B;;;ADAO,SAAS,mBAAmB,UAAqC;AACpE,SAAO,uBAAS,QAAQ,QAAQ,EAAE,OAAO,cAAc;AAC3D;;;ADOO,SAAS,cACZ,UACA,WACmB;AACnB,aAAO;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,SAAS,IAAI;AAAA,EACjC;AACJ;;;ACpBA,IAAAC,gBAA2D;AAmBpD,SAAS,iBACZ,UACA,WACc;AACd,aAAO;AAAA,IACH,MAAM,mBAAmB,QAAQ,EAAE,OAAO,SAAS;AAAA,IACnD,CAAC,UAAU,SAAS;AAAA,EACxB;AACJ;;;ACdO,SAAS,kBACZ,UACA,MACkB;AAClB,SAAO;AAAA,IAAiB;AAAA,IAAU,CAAC,YAC/B,gBAAgB,SAAS,IAAI;AAAA,EACjC;AACJ;;;ACpBA,IAAAC,gBAA2D;AAmBpD,SAAS,iBACZ,UACA,WACO;AACP,aAAO;AAAA,IACH,MAAM,mBAAmB,QAAQ,EAAE,KAAK,SAAS;AAAA,IACjD,CAAC,UAAU,SAAS;AAAA,EACxB;AACJ;;;AC3BA,8BAA6D;AAE7D,IAAM,WAgBF;AAAA,EACA,uCACI;AAAA,EACJ,yCACI;AACR;AAGA,IAAM,eAAW;AAAA,EACb,QAAQ,IAAI,aAAa,eAAgB,CAAC,IAAwB;AAAA,EAClE,EAAE,eAAe,CAAC,YAAY,QAAQ;AAC1C;AAEA,IAAO,mBAAQ;;;ACMR,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":["import_react","import_react","import_react","import_react"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { ElementType, ReactNode, ReactElement } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a React element whose props and element type are narrowed to the provided React element type.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T The React element type used to narrow the element's props and type.
|
|
8
|
+
*/
|
|
9
|
+
type ElementOfType<T extends React.ElementType> = React.ReactElement<React.ComponentProps<T>, T>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Returns the first direct child element whose React element type exactly matches the provided type.
|
|
13
|
+
*
|
|
14
|
+
* @param children The React children value to inspect.
|
|
15
|
+
* @param type The element or component type to match against each direct child element.
|
|
16
|
+
* @returns The first direct child element whose type matches the provided element type, or `null` when no match is found.
|
|
17
|
+
*/
|
|
18
|
+
declare function useChildByType<T extends ElementType>(children: ReactNode, type: T): ElementOfType<T> | null;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Returns the first direct child element that satisfies the provided predicate.
|
|
22
|
+
*
|
|
23
|
+
* @param children The React children value to inspect.
|
|
24
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
25
|
+
* @returns The first direct child element that satisfies the provided predicate, or `null` when no match is found.
|
|
26
|
+
*/
|
|
27
|
+
declare function useChildWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): T | null;
|
|
28
|
+
declare function useChildWhere(children: ReactNode, predicate: (element: ReactElement) => boolean): ReactElement | null;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the direct child elements whose React element type exactly matches the provided type.
|
|
32
|
+
*
|
|
33
|
+
* @param children The React children value to inspect.
|
|
34
|
+
* @param type The element or component type to match against each direct child element.
|
|
35
|
+
* @returns An array of direct child elements whose type matches the provided element type.
|
|
36
|
+
*/
|
|
37
|
+
declare function useChildrenByType<T extends ElementType>(children: ReactNode, type: T): ElementOfType<T>[];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Returns the direct child elements that satisfy the provided predicate.
|
|
41
|
+
*
|
|
42
|
+
* @param children The React children value to inspect.
|
|
43
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it should be included in the result.
|
|
44
|
+
* @returns An array of direct child elements that satisfy the provided predicate.
|
|
45
|
+
*/
|
|
46
|
+
declare function useChildrenWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): T[];
|
|
47
|
+
declare function useChildrenWhere(children: ReactNode, predicate: (element: ReactElement) => boolean): ReactElement[];
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Determines whether any direct child element satisfies the provided predicate.
|
|
51
|
+
*
|
|
52
|
+
* @param children The React children value to inspect.
|
|
53
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
54
|
+
* @returns `true` when at least one direct child element satisfies the provided predicate; otherwise `false`.
|
|
55
|
+
*/
|
|
56
|
+
declare function useHasChildWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): boolean;
|
|
57
|
+
declare function useHasChildWhere(children: ReactNode, predicate: (element: ReactElement) => boolean): boolean;
|
|
58
|
+
|
|
59
|
+
type UseMinimumChildrenWhereOptions = {
|
|
60
|
+
/**
|
|
61
|
+
* An optional consumer-defined trace code that is prefixed to the thrown validation message.
|
|
62
|
+
*/
|
|
63
|
+
traceCode?: string;
|
|
64
|
+
/**
|
|
65
|
+
* An optional human-readable child name that is included in the thrown validation message.
|
|
66
|
+
*/
|
|
67
|
+
childName?: string;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Returns the direct child elements that satisfy the provided predicate, or throws when fewer than the minimum count are found.
|
|
71
|
+
*
|
|
72
|
+
* @param children The React children value to inspect.
|
|
73
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
74
|
+
* @param minimumCount The minimum number of matching direct child elements required.
|
|
75
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
76
|
+
* @returns The direct child elements that satisfy the provided predicate.
|
|
77
|
+
*/
|
|
78
|
+
declare function useMinimumChildrenWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, minimumCount: number, options?: UseMinimumChildrenWhereOptions): T[];
|
|
79
|
+
declare function useMinimumChildrenWhere(children: ReactNode, predicate: (element: ReactElement) => boolean, minimumCount: number, options?: UseMinimumChildrenWhereOptions): ReactElement[];
|
|
80
|
+
|
|
81
|
+
type UseRequiredChildWhereOptions = {
|
|
82
|
+
/**
|
|
83
|
+
* An optional consumer-defined trace code that is prefixed to the thrown validation message.
|
|
84
|
+
*/
|
|
85
|
+
traceCode?: string;
|
|
86
|
+
/**
|
|
87
|
+
* An optional human-readable child name that is included in the thrown validation message.
|
|
88
|
+
*/
|
|
89
|
+
childName?: string;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Returns the first direct child element that satisfies the provided predicate, or throws when no match is found.
|
|
93
|
+
*
|
|
94
|
+
* @param children The React children value to inspect.
|
|
95
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
96
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
97
|
+
* @returns The first direct child element that satisfies the provided predicate.
|
|
98
|
+
*/
|
|
99
|
+
declare function useRequiredChildWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: UseRequiredChildWhereOptions): T;
|
|
100
|
+
declare function useRequiredChildWhere(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: UseRequiredChildWhereOptions): ReactElement;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Normalizes a React children value into an array containing only valid direct child elements.
|
|
104
|
+
*
|
|
105
|
+
* @param children The React children value to normalize.
|
|
106
|
+
* @returns An array of valid React elements from the provided direct children.
|
|
107
|
+
*/
|
|
108
|
+
declare function childrenToElements(children: ReactNode): ReactElement[];
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Determines whether a React element exactly matches the provided element or component type.
|
|
112
|
+
*
|
|
113
|
+
* @param element The React element to compare.
|
|
114
|
+
* @param type The element or component type to match.
|
|
115
|
+
* @returns `true` when the element's type exactly matches the provided type; otherwise `false`.
|
|
116
|
+
*/
|
|
117
|
+
declare function isElementOfType<T extends ElementType>(element: ReactElement, type: T): element is ElementOfType<T>;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Determines whether a React node is a valid React element.
|
|
121
|
+
*
|
|
122
|
+
* @param node The React node to check.
|
|
123
|
+
* @returns `true` when the node is a valid React element; otherwise `false`.
|
|
124
|
+
*/
|
|
125
|
+
declare function isReactElement(node: ReactNode): node is ReactElement;
|
|
126
|
+
|
|
127
|
+
export { type ElementOfType, type UseMinimumChildrenWhereOptions, type UseRequiredChildWhereOptions, childrenToElements, isElementOfType, isReactElement, useChildByType, useChildWhere, useChildrenByType, useChildrenWhere, useHasChildWhere, useMinimumChildrenWhere, useRequiredChildWhere };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { ElementType, ReactNode, ReactElement } from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a React element whose props and element type are narrowed to the provided React element type.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T The React element type used to narrow the element's props and type.
|
|
8
|
+
*/
|
|
9
|
+
type ElementOfType<T extends React.ElementType> = React.ReactElement<React.ComponentProps<T>, T>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Returns the first direct child element whose React element type exactly matches the provided type.
|
|
13
|
+
*
|
|
14
|
+
* @param children The React children value to inspect.
|
|
15
|
+
* @param type The element or component type to match against each direct child element.
|
|
16
|
+
* @returns The first direct child element whose type matches the provided element type, or `null` when no match is found.
|
|
17
|
+
*/
|
|
18
|
+
declare function useChildByType<T extends ElementType>(children: ReactNode, type: T): ElementOfType<T> | null;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Returns the first direct child element that satisfies the provided predicate.
|
|
22
|
+
*
|
|
23
|
+
* @param children The React children value to inspect.
|
|
24
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
25
|
+
* @returns The first direct child element that satisfies the provided predicate, or `null` when no match is found.
|
|
26
|
+
*/
|
|
27
|
+
declare function useChildWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): T | null;
|
|
28
|
+
declare function useChildWhere(children: ReactNode, predicate: (element: ReactElement) => boolean): ReactElement | null;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns the direct child elements whose React element type exactly matches the provided type.
|
|
32
|
+
*
|
|
33
|
+
* @param children The React children value to inspect.
|
|
34
|
+
* @param type The element or component type to match against each direct child element.
|
|
35
|
+
* @returns An array of direct child elements whose type matches the provided element type.
|
|
36
|
+
*/
|
|
37
|
+
declare function useChildrenByType<T extends ElementType>(children: ReactNode, type: T): ElementOfType<T>[];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Returns the direct child elements that satisfy the provided predicate.
|
|
41
|
+
*
|
|
42
|
+
* @param children The React children value to inspect.
|
|
43
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it should be included in the result.
|
|
44
|
+
* @returns An array of direct child elements that satisfy the provided predicate.
|
|
45
|
+
*/
|
|
46
|
+
declare function useChildrenWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): T[];
|
|
47
|
+
declare function useChildrenWhere(children: ReactNode, predicate: (element: ReactElement) => boolean): ReactElement[];
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Determines whether any direct child element satisfies the provided predicate.
|
|
51
|
+
*
|
|
52
|
+
* @param children The React children value to inspect.
|
|
53
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
54
|
+
* @returns `true` when at least one direct child element satisfies the provided predicate; otherwise `false`.
|
|
55
|
+
*/
|
|
56
|
+
declare function useHasChildWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T): boolean;
|
|
57
|
+
declare function useHasChildWhere(children: ReactNode, predicate: (element: ReactElement) => boolean): boolean;
|
|
58
|
+
|
|
59
|
+
type UseMinimumChildrenWhereOptions = {
|
|
60
|
+
/**
|
|
61
|
+
* An optional consumer-defined trace code that is prefixed to the thrown validation message.
|
|
62
|
+
*/
|
|
63
|
+
traceCode?: string;
|
|
64
|
+
/**
|
|
65
|
+
* An optional human-readable child name that is included in the thrown validation message.
|
|
66
|
+
*/
|
|
67
|
+
childName?: string;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Returns the direct child elements that satisfy the provided predicate, or throws when fewer than the minimum count are found.
|
|
71
|
+
*
|
|
72
|
+
* @param children The React children value to inspect.
|
|
73
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
74
|
+
* @param minimumCount The minimum number of matching direct child elements required.
|
|
75
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
76
|
+
* @returns The direct child elements that satisfy the provided predicate.
|
|
77
|
+
*/
|
|
78
|
+
declare function useMinimumChildrenWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, minimumCount: number, options?: UseMinimumChildrenWhereOptions): T[];
|
|
79
|
+
declare function useMinimumChildrenWhere(children: ReactNode, predicate: (element: ReactElement) => boolean, minimumCount: number, options?: UseMinimumChildrenWhereOptions): ReactElement[];
|
|
80
|
+
|
|
81
|
+
type UseRequiredChildWhereOptions = {
|
|
82
|
+
/**
|
|
83
|
+
* An optional consumer-defined trace code that is prefixed to the thrown validation message.
|
|
84
|
+
*/
|
|
85
|
+
traceCode?: string;
|
|
86
|
+
/**
|
|
87
|
+
* An optional human-readable child name that is included in the thrown validation message.
|
|
88
|
+
*/
|
|
89
|
+
childName?: string;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Returns the first direct child element that satisfies the provided predicate, or throws when no match is found.
|
|
93
|
+
*
|
|
94
|
+
* @param children The React children value to inspect.
|
|
95
|
+
* @param predicate A predicate that is called with each direct child element to determine whether it matches.
|
|
96
|
+
* @param options Optional reporting metadata used to derive the thrown validation message.
|
|
97
|
+
* @returns The first direct child element that satisfies the provided predicate.
|
|
98
|
+
*/
|
|
99
|
+
declare function useRequiredChildWhere<T extends ReactElement>(children: ReactNode, predicate: (element: ReactElement) => element is T, options?: UseRequiredChildWhereOptions): T;
|
|
100
|
+
declare function useRequiredChildWhere(children: ReactNode, predicate: (element: ReactElement) => boolean, options?: UseRequiredChildWhereOptions): ReactElement;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Normalizes a React children value into an array containing only valid direct child elements.
|
|
104
|
+
*
|
|
105
|
+
* @param children The React children value to normalize.
|
|
106
|
+
* @returns An array of valid React elements from the provided direct children.
|
|
107
|
+
*/
|
|
108
|
+
declare function childrenToElements(children: ReactNode): ReactElement[];
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Determines whether a React element exactly matches the provided element or component type.
|
|
112
|
+
*
|
|
113
|
+
* @param element The React element to compare.
|
|
114
|
+
* @param type The element or component type to match.
|
|
115
|
+
* @returns `true` when the element's type exactly matches the provided type; otherwise `false`.
|
|
116
|
+
*/
|
|
117
|
+
declare function isElementOfType<T extends ElementType>(element: ReactElement, type: T): element is ElementOfType<T>;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Determines whether a React node is a valid React element.
|
|
121
|
+
*
|
|
122
|
+
* @param node The React node to check.
|
|
123
|
+
* @returns `true` when the node is a valid React element; otherwise `false`.
|
|
124
|
+
*/
|
|
125
|
+
declare function isReactElement(node: ReactNode): node is ReactElement;
|
|
126
|
+
|
|
127
|
+
export { type ElementOfType, type UseMinimumChildrenWhereOptions, type UseRequiredChildWhereOptions, childrenToElements, isElementOfType, isReactElement, useChildByType, useChildWhere, useChildrenByType, useChildrenWhere, useHasChildWhere, useMinimumChildrenWhere, useRequiredChildWhere };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// src/isElementOfType.ts
|
|
2
|
+
function isElementOfType(element, type) {
|
|
3
|
+
return element.type === type;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// src/useChildWhere.ts
|
|
7
|
+
import { useMemo } from "react";
|
|
8
|
+
|
|
9
|
+
// src/childrenToElements.ts
|
|
10
|
+
import { Children } from "react";
|
|
11
|
+
|
|
12
|
+
// src/isReactElement.ts
|
|
13
|
+
import { isValidElement } from "react";
|
|
14
|
+
function isReactElement(node) {
|
|
15
|
+
return isValidElement(node);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/childrenToElements.ts
|
|
19
|
+
function childrenToElements(children) {
|
|
20
|
+
return Children.toArray(children).filter(isReactElement);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/useChildWhere.ts
|
|
24
|
+
function useChildWhere(children, predicate) {
|
|
25
|
+
return useMemo(
|
|
26
|
+
() => childrenToElements(children).find(predicate) ?? null,
|
|
27
|
+
[children, predicate]
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/useChildByType.ts
|
|
32
|
+
function useChildByType(children, type) {
|
|
33
|
+
return useChildWhere(
|
|
34
|
+
children,
|
|
35
|
+
(element) => isElementOfType(element, type)
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/useChildrenWhere.ts
|
|
40
|
+
import { useMemo as useMemo2 } from "react";
|
|
41
|
+
function useChildrenWhere(children, predicate) {
|
|
42
|
+
return useMemo2(
|
|
43
|
+
() => childrenToElements(children).filter(predicate),
|
|
44
|
+
[children, predicate]
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/useChildrenByType.ts
|
|
49
|
+
function useChildrenByType(children, type) {
|
|
50
|
+
return useChildrenWhere(
|
|
51
|
+
children,
|
|
52
|
+
(element) => isElementOfType(element, type)
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/useHasChildWhere.ts
|
|
57
|
+
import { useMemo as useMemo3 } from "react";
|
|
58
|
+
function useHasChildWhere(children, predicate) {
|
|
59
|
+
return useMemo3(
|
|
60
|
+
() => childrenToElements(children).some(predicate),
|
|
61
|
+
[children, predicate]
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/reporter.ts
|
|
66
|
+
import { createReporter } from "runtime-reporter";
|
|
67
|
+
var messages = {
|
|
68
|
+
REQUIRED_CHILD_WHERE_PREDICATE_FAILED: "{{ traceCodePrefix }}Required child validation failed{{ childNameSegment }} because no direct child satisfied the provided predicate.",
|
|
69
|
+
MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED: "{{ traceCodePrefix }}Minimum children validation failed{{ childNameSegment }} because only {{ actualCount }} direct child{{ actualCountPluralSuffix }} satisfied the provided predicate; expected at least {{ minimumCount }}."
|
|
70
|
+
};
|
|
71
|
+
var reporter = createReporter(
|
|
72
|
+
process.env.NODE_ENV === "production" ? {} : messages,
|
|
73
|
+
{ formatMessage: (message) => message }
|
|
74
|
+
);
|
|
75
|
+
var reporter_default = reporter;
|
|
76
|
+
|
|
77
|
+
// src/useMinimumChildrenWhere.ts
|
|
78
|
+
function useMinimumChildrenWhere(children, predicate, minimumCount, options) {
|
|
79
|
+
const matchingChildren = useChildrenWhere(children, predicate);
|
|
80
|
+
if (matchingChildren.length >= minimumCount) {
|
|
81
|
+
return matchingChildren;
|
|
82
|
+
}
|
|
83
|
+
return reporter_default.fail("MINIMUM_CHILDREN_WHERE_PREDICATE_FAILED", {
|
|
84
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
85
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : "",
|
|
86
|
+
actualCount: matchingChildren.length,
|
|
87
|
+
actualCountPluralSuffix: matchingChildren.length === 1 ? "" : "ren",
|
|
88
|
+
minimumCount
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// src/useRequiredChildWhere.ts
|
|
93
|
+
function useRequiredChildWhere(children, predicate, options) {
|
|
94
|
+
const child = useChildWhere(children, predicate);
|
|
95
|
+
if (child !== null) {
|
|
96
|
+
return child;
|
|
97
|
+
}
|
|
98
|
+
return reporter_default.fail("REQUIRED_CHILD_WHERE_PREDICATE_FAILED", {
|
|
99
|
+
traceCodePrefix: options?.traceCode ? `[${options.traceCode}] ` : "",
|
|
100
|
+
childNameSegment: options?.childName ? ` for ${options.childName}` : ""
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
export {
|
|
104
|
+
childrenToElements,
|
|
105
|
+
isElementOfType,
|
|
106
|
+
isReactElement,
|
|
107
|
+
useChildByType,
|
|
108
|
+
useChildWhere,
|
|
109
|
+
useChildrenByType,
|
|
110
|
+
useChildrenWhere,
|
|
111
|
+
useHasChildWhere,
|
|
112
|
+
useMinimumChildrenWhere,
|
|
113
|
+
useRequiredChildWhere
|
|
114
|
+
};
|
|
115
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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/useHasChildWhere.ts","../src/reporter.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 { 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 { 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 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};\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 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,SAAS,IAAI;AAAA,EACjC;AACJ;;;ACpBA,SAAS,WAAAA,gBAAkD;AAmBpD,SAAS,iBACZ,UACA,WACc;AACd,SAAOC;AAAA,IACH,MAAM,mBAAmB,QAAQ,EAAE,OAAO,SAAS;AAAA,IACnD,CAAC,UAAU,SAAS;AAAA,EACxB;AACJ;;;ACdO,SAAS,kBACZ,UACA,MACkB;AAClB,SAAO;AAAA,IAAiB;AAAA,IAAU,CAAC,YAC/B,gBAAgB,SAAS,IAAI;AAAA,EACjC;AACJ;;;ACpBA,SAAS,WAAAC,gBAAkD;AAmBpD,SAAS,iBACZ,UACA,WACO;AACP,SAAOC;AAAA,IACH,MAAM,mBAAmB,QAAQ,EAAE,KAAK,SAAS;AAAA,IACjD,CAAC,UAAU,SAAS;AAAA,EACxB;AACJ;;;AC3BA,SAAS,sBAAoD;AAE7D,IAAM,WAgBF;AAAA,EACA,uCACI;AAAA,EACJ,yCACI;AACR;AAGA,IAAM,WAAW;AAAA,EACb,QAAQ,IAAI,aAAa,eAAgB,CAAC,IAAwB;AAAA,EAClE,EAAE,eAAe,CAAC,YAAY,QAAQ;AAC1C;AAEA,IAAO,mBAAQ;;;ACMR,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"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-children-hooks",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React hooks for inspecting, traversing, querying, and validating props.children.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"children",
|
|
8
|
+
"hooks",
|
|
9
|
+
"components",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/marksmccann/react-children-hooks#readme",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/marksmccann/react-children-hooks.git"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/marksmccann/react-children-hooks/issues"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "Mark McCann",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./dist/index.cjs",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js",
|
|
30
|
+
"require": "./dist/index.cjs"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=18"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup",
|
|
42
|
+
"commit": "cz",
|
|
43
|
+
"dev": "tsup --watch",
|
|
44
|
+
"format": "prettier --write .",
|
|
45
|
+
"lint": "eslint .",
|
|
46
|
+
"prepublishOnly": "npm run build",
|
|
47
|
+
"release": "release-it",
|
|
48
|
+
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"test:watch": "vitest",
|
|
51
|
+
"prepare": "husky"
|
|
52
|
+
},
|
|
53
|
+
"config": {
|
|
54
|
+
"commitizen": {
|
|
55
|
+
"path": "cz-conventional-changelog"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@commitlint/cli": "^19.8.0",
|
|
63
|
+
"@commitlint/config-conventional": "^19.8.0",
|
|
64
|
+
"@eslint/js": "^9.27.0",
|
|
65
|
+
"@release-it/conventional-changelog": "^10.0.2",
|
|
66
|
+
"@testing-library/react": "^16.3.2",
|
|
67
|
+
"@types/node": "^22.15.30",
|
|
68
|
+
"@types/react": "^19.1.5",
|
|
69
|
+
"@vitest/coverage-v8": "^3.1.4",
|
|
70
|
+
"commitizen": "^4.3.1",
|
|
71
|
+
"cz-conventional-changelog": "^3.3.0",
|
|
72
|
+
"eslint": "^9.27.0",
|
|
73
|
+
"eslint-config-prettier": "^10.1.5",
|
|
74
|
+
"eslint-plugin-react-hooks": "^5.2.0",
|
|
75
|
+
"globals": "^16.1.0",
|
|
76
|
+
"husky": "^9.1.7",
|
|
77
|
+
"jsdom": "^26.1.0",
|
|
78
|
+
"lint-staged": "^15.5.2",
|
|
79
|
+
"prettier": "^3.5.3",
|
|
80
|
+
"react": "^19.2.6",
|
|
81
|
+
"react-dom": "^19.2.6",
|
|
82
|
+
"release-it": "^19.0.4",
|
|
83
|
+
"tsup": "^8.5.0",
|
|
84
|
+
"typescript": "^5.8.3",
|
|
85
|
+
"typescript-eslint": "^8.32.1",
|
|
86
|
+
"vitest": "^3.1.4"
|
|
87
|
+
},
|
|
88
|
+
"dependencies": {
|
|
89
|
+
"runtime-reporter": "^0.7.0"
|
|
90
|
+
}
|
|
91
|
+
}
|