-
Notifications
You must be signed in to change notification settings - Fork 1
/
widget.ts
276 lines (268 loc) · 6.73 KB
/
widget.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { ReactNode } from 'react';
import { JsonSerializable } from '../json-serializable';
/**
* Defines the basic prop-type of Widget component.
*
* @see {@link JsonSerializable}
*/
export type BasePropType = JsonSerializable;
/**
* Specifies the most generic properties that a Widget component can have.
*
* @see {@link BasePropType}
* @see {@link BaseRendererProps}
*
* @example
* Initialization of basic, advanced and invalid properties
* ```ts
* const myProps: GenericProps = {
* basic: 'Where is the box?',
* advanced: {
* part1: 'Test it!',
* part2: false,
* part3: 3.14
* },
* invalid: () => console.log('Ohh...') // not allowed
* };
* ```
*/
export interface GenericProps {
[key: string]: BasePropType | undefined;
}
/**
* Specifies the most generic type
* a Widget component and a ConfigControls component can have.
*
* @typeParam P - additional props for the Widget renderer component
* (see example below)
*
* @see {@link ReactNode}
* @see {@link Widget}
*
* @example
* Common usage
* ```ts
* import { ReactNode } from 'react';
*
* interface WidgetProps extends GenericProps {
* type: 'basic' | 'advanced';
* }
*
* interface RendererProps extends BaseRendererProps<WidgetProps> {}
*
* function Widget({ title, type }: RendererProps): ReactNode {
* return (
* <div>
* <p>Title: {title}</p>
* <p>Type: {type}</p>
* </div>
* );
* }
*
* export const myWidget: Widget<WidgetProps> = {
* name: 'My Widget',
* Widget: Widget
* };
* ```
*/
export type GenericComponent<
P extends Record<string, unknown> = Record<string, unknown>
> = (props: P) => ReactNode;
/**
* Defines the props that are always available to Widget component
* and are not changeable via the ConfigControls component.
*
* @see {@link BaseRendererProps}
* @see {@link Widget}
*/
export interface GlobalRendererProps extends GenericProps {
/**
* the title of the Widget instance on the dashboard
*
* @see {@link WidgetDefinition.title}
*
* @example
* A very simple widget
* ```ts
* import { ReactNode } from 'react';
*
* function Widget({ title, type }: BaseRendererProps): ReactNode {
* return <p>Title: {title}</p>;
* }
*
* export const verySimpleWidget: Widget<WidgetProps> = {
* name: 'Very simple Widget',
* Widget: Widget
* };
* ```
*/
title: string;
}
/**
* These are the most basic renderer props
* that a Widget renderer component can have.
* It is extensible via a generic type
* to add more props to the Widget renderer component.
*
* @typeParam P - additional props for the Widget renderer component
*
* @see {@link GenericProps}
* @see {@link GlobalRendererProps}
* @see {@link Widget}
*
* @example
* A very simple widget
* ```ts
* import { ReactNode } from 'react';
*
* function Widget({ title, type }: BaseRendererProps): ReactNode {
* return <p>Title: {title}</p>;
* }
*
* export const verySimpleWidget: Widget<WidgetProps> = {
* name: 'Very simple Widget',
* Widget: Widget
* };
* ```
*
* @example
* Extended widget
* ```ts
* import { ReactNode } from 'react';
*
* interface WidgetProps extends GenericProps {
* type: 'basic' | 'advanced';
* }
*
* interface RendererProps extends BaseRendererProps<WidgetProps> {}
*
* function Widget({ title, type }: RendererProps): ReactNode {
* return (
* <div>
* <p>Title: {title}</p>
* <p>Type: {type}</p>
* </div>
* );
* }
*
* export const myWidget: Widget<WidgetProps> = {
* name: 'My Widget',
* Widget: Widget
* };
* ```
*/
export type BaseRendererProps<P extends GenericProps = GenericProps> = P &
GlobalRendererProps;
/**
* These are props for the ConfigControls widget.
* that a Widget renderer component can have.
* It is extensible via a generic type
* to add more props that are also used at the Widget renderer component.
*
* @typeParam P - additional props for the Widget renderer component
*
* @see {@link GenericProps}
* @see {@link BaseRendererProps}
* @see {@link Widget}
*
* @example
* Very simple ConfigControls
* ```ts
* import { ReactNode } from 'react';
*
* interface WidgetProps extends GenericProps {
* value: string;
* }
*
* interface RendererProps extends BaseRendererProps<WidgetProps> {}
*
* interface ConfigControlsProps extends BaseConfigControlsProps<WidgetProps> {}
*
* function Widget({ value }: RendererProps): ReactNode {
* return <p>Value: {value}</p>;
* }
*
* function ConfigControls({ }
* ```
*/
export interface BaseConfigControlsProps<P extends GenericProps = GenericProps>
extends Record<string, unknown> {
/**
* the current props values of the widget
* which can be changed with
* {@link BaseConfigControlsProps.onUpdate | onUpdate}
*/
currentProps: P;
/**
* Updates the values of the props of the widget.
* @param newProps - new props that are merged or replaced
* with the current props to build the new props for the widget to use
*
* @example
* Update the props in a ConfigControls component
* ```ts
* function ConfigControls({
* currentProps, onUpdate
* }: ConfigControlsProps): ReactNode {
* // local state to not "flood" global store with changes (performance)
* const [value, setValue] = useState(currentProps.value);
*
* return (
* <div>
* <input type="text" value={value} onChange={event => setValue(event.target.value)} />
* <button onClick={() => onUpdate({ value })} />
* </div>
* );
* }
* ```
*/
onUpdate: (newProps: Partial<P>) => void;
}
/**
* An "entire" widget definition ready to export and use.
* It contains a name for reference, a Widget component that will be rendered
* and an optional ConfigControls component to change Widget component props.
*
* @typeParam P - additional props for the Widget renderer component
*
* @see {@link Widget.name}
* @see {@link Widget.Widget}
* @see {@link Widget.ConfigControls}
*
* @example
* A very simple widget
* ```ts
* import { Widget } from './widget';
* import { ConfigControls } from './config-controls';
*
* export const myWidget: Widget = {
* name: 'My Awesome Widget',
* Widget: Widget,
* ConfigControls: ConfigControls
* }
* ```
*/
export interface Widget<P extends GenericProps = GenericProps> {
/**
* the name of the widget
*
* It is a selector to reference the widget
* and will be shown in the application.
*/
name: string;
/**
* the Widget component implementation as React Component
*
* @see {@link BaseRendererProps}
* @see {@link GlobalRendererProps}
* @see {@link GenericComponent}
*/
Widget: GenericComponent<BaseRendererProps<P>>;
/**
* the ConfigControls component implementation as React Component
*
* @see {@link BaseConfigControlsProps}
* @see {@link GenericComponent}
*/
ConfigControls?: GenericComponent<BaseConfigControlsProps<P>>;
}