This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentRegister.ts
98 lines (91 loc) · 2.66 KB
/
ComponentRegister.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
import { LinkComponentType } from "./LinkComponentType";
/**
* Components to register with {@link ComponentRegister} via
* {@link ComponentRegisterOptions.components}.
*/
export interface ComponentRegisterComponents {
/**
* The component to use internally for handling links.
*
* Defaults to `"a"`.
*/
Link?: LinkComponentType;
}
/**
* Options to pass to {@link ComponentRegister.init}.
*
* This is only generic to support sharing the type with
* {@link defaultComponentRegisterOptions}. You should never need to override
* the default generic parameter. It defaults to
* {@link ComponentRegisterComponents}.
*/
export interface ComponentRegisterOptions<
C extends ComponentRegisterComponents = ComponentRegisterComponents
> {
/**
* The components to register.
*
* Defaults to be of type {@link ComponentRegisterComponents}.
*/
components?: C;
}
/**
* The default options for {@link ComponentRegister}.
*/
export const defaultComponentRegisterOptions: Required<ComponentRegisterOptions<
Required<ComponentRegisterComponents>
>> = {
components: {
Link: "a",
},
};
/**
* A register of components to use within library components, with sensible
* defaults. Many users will never need to interact with this. It exists to
* support customization of library component behaviour.
*/
export class ComponentRegister {
/**
* The {@link LinkComponentType} to allow this library to hook into routers or
* other custom linking libraries.
*
* Defaults to `"a"`. See {@link ComponentRegisterComponents.Link} and
* {@link defaultComponentRegisterOptions}.
*/
static Link: LinkComponentType =
defaultComponentRegisterOptions.components.Link;
/**
* Resets the component register to its defaults. See
* {@link defaultComponentRegisterOptions} for those defaults.
*/
static reset(): void {
this.init(defaultComponentRegisterOptions);
}
/**
* Initializes {@link ComponentRegister} with the provided options.
*
* This is how you set component options en masse, and is the preferred method
* for providing custom components.
*
* ```ts
* // Using `Link` from React Router.
* ComponentRegister.init({
* components.
* Link: ({ className, href, children }) => (
* <ReactRouter.Link className={className} to={href}>
* {children}
* </ReactRouter.Link>
* )
* });
* ```
*
* @param options - Options to customize the underlying components used by
* library components.
*/
static init(options: ComponentRegisterOptions = {}): void {
const { components } = options;
if (components && components.Link) {
this.Link = components.Link;
}
}
}