-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
createHook.ts
119 lines (105 loc) · 3.63 KB
/
createHook.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { toArray } from "reakit-utils/toArray";
import { deepEqual } from "./__utils/deepEqual";
import { useOptions } from "./useOptions";
import { useProps } from "./useProps";
type Hook<O = any, P = any> = {
(options?: O, htmlProps?: P): P;
__keys: ReadonlyArray<any>;
__useOptions: (options: O, htmlProps: P) => O;
__propsAreEqual?: (prev: O & P, next: O & P) => boolean;
};
type CreateHookOptions<O, P> = {
name?: string;
compose?: Hook | Hook[];
useState?: { (): any; __keys: ReadonlyArray<any> };
useOptions?: (options: O, htmlProps: P) => O;
useProps?: (options: O, htmlProps: P) => P;
useComposeOptions?: (options: O, htmlProps: P) => O;
propsAreEqual?: (prev: O & P, next: O & P) => boolean | undefined | null;
keys?: ReadonlyArray<keyof O>;
};
export function createHook<O, P>(options: CreateHookOptions<O, P>) {
const composedHooks = toArray(options.compose) as Hook[];
const __useOptions = (hookOptions: O, htmlProps: P) => {
// Call the current hook's useOptions first
if (options.useOptions) {
hookOptions = options.useOptions(hookOptions, htmlProps);
}
// If there's name, call useOptions from the system context
if (options.name) {
hookOptions = useOptions(options.name, hookOptions, htmlProps);
}
return hookOptions;
};
const useHook: Hook<O, P> = (
hookOptions = {} as O,
htmlProps = {} as P,
unstable_ignoreUseOptions = false
) => {
// This won't execute when useHook was called from within another useHook
if (!unstable_ignoreUseOptions) {
hookOptions = __useOptions(hookOptions, htmlProps);
}
// We're already calling composed useOptions here
// That's why we ignoreUseOptions for composed hooks
if (options.compose) {
composedHooks.forEach(hook => {
hookOptions = hook.__useOptions(hookOptions, htmlProps);
});
}
// Call the current hook's useProps
if (options.useProps) {
htmlProps = options.useProps(hookOptions, htmlProps);
}
// If there's name, call useProps from the system context
if (options.name) {
htmlProps = useProps(options.name, hookOptions, htmlProps) as P;
}
if (options.compose) {
if (options.useComposeOptions) {
hookOptions = options.useComposeOptions(hookOptions, htmlProps);
}
composedHooks.forEach(hook => {
// @ts-ignore The third option is only used internally
htmlProps = hook(hookOptions, htmlProps, true);
});
}
return htmlProps;
};
if (process.env.NODE_ENV !== "production" && options.name) {
Object.defineProperty(useHook, "name", {
value: `use${options.name}`
});
}
useHook.__useOptions = __useOptions;
// It's used by createComponent to split option props (keys) and html props
useHook.__keys = [
...composedHooks.reduce((allKeys, hook) => {
allKeys.push(...(hook.__keys || []));
return allKeys;
}, [] as string[]),
...(options.useState ? options.useState.__keys : []),
...(options.keys || [])
];
const hasPropsAreEqual = Boolean(
options.propsAreEqual ||
composedHooks.find(hook => Boolean(hook.__propsAreEqual))
);
if (hasPropsAreEqual) {
useHook.__propsAreEqual = (prev, next) => {
const result = options.propsAreEqual && options.propsAreEqual(prev, next);
if (result != null) {
return result;
}
for (const hook of composedHooks) {
const propsAreEqual = hook.__propsAreEqual;
const hookResult = propsAreEqual && propsAreEqual(prev, next);
if (hookResult != null) {
return hookResult;
}
}
return deepEqual(prev, next);
};
}
return useHook;
}