-
Notifications
You must be signed in to change notification settings - Fork 0
/
createAccessors.ts
44 lines (43 loc) · 1.49 KB
/
createAccessors.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
import type { ComponentType, ReactElement } from "react";
import { get } from "./get";
import { getAll } from "./getAll";
import { getProps } from "./getProps";
import { query } from "./query";
import { queryAll } from "./queryAll";
import type { AccessorsType, ParamsType, QueryType } from "./types";
/**
* Create an object of bound query functions
* @param element Target react element
* @param query Query for search (type of react element, props, className)
* @param params Params for search inside elements of exotic components
* @returns an object of bound functions that similar to helper functions of the library
*/
export const createAccessors = <
// biome-ignore lint/suspicious/noExplicitAny: supports any component
Component extends keyof JSX.IntrinsicElements | ComponentType<any>,
>(
element: ReactElement,
queryParam: QueryType<Component>,
params?: ParamsType<Component>,
): AccessorsType<Component> => ({
/**
* @returns result of `get` function of the library
*/
get: () => get(element, queryParam, params),
/**
* @returns result of `getAll` function of the library
*/
getAll: () => getAll(element, queryParam, params),
/**
* @returns result of `getProps` function of the library
*/
getProps: () => getProps(element, queryParam, params),
/**
* @returns result of `query` function of the library
*/
query: () => query(element, queryParam, params),
/**
* @returns result of `queryAll` function of the library
*/
queryAll: () => queryAll(element, queryParam, params),
});