-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultMatch.ts
46 lines (37 loc) · 1.07 KB
/
defaultMatch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { ComponentType, ReactElement } from "react";
import type { QueryType } from "./types";
/**
* Default function that checks if the target element is matching for search, e.g.
* @param element Target react element
* @param query Current query
* @returns Is element matching for current query
*/
export const defaultMatch = <
// biome-ignore lint/suspicious/noExplicitAny: supports any component
Component extends ComponentType<any> | keyof JSX.IntrinsicElements,
>(
element: ReactElement,
query: QueryType<Component>,
) => {
const { className, component, props } = query;
if (component && component !== element.type) {
return false;
}
const elementProps = element.props as Record<string, unknown>;
if (className) {
const { className: classNameProp } = elementProps;
if (!classNameProp || typeof classNameProp !== "string") {
return false;
}
if (!classNameProp.split(" ").includes(className)) {
return false;
}
}
if (
props &&
Object.entries(props).some(([key, value]) => elementProps[key] !== value)
) {
return false;
}
return true;
};