-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDemo.ts
37 lines (28 loc) · 810 Bytes
/
Demo.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
import { ReactElement } from 'react';
import { Configure } from './configContext';
import { DemosGroup } from './DemoGroup';
type DemoSpec<P> = {
render: (props: P) => ReactElement;
configure?: Configure<P>;
description?: string;
keywords?: string[];
};
export class Demo<P> {
static isDemo(thing: unknown): thing is Demo<unknown> {
return thing instanceof Demo;
}
parent?: DemosGroup<P>;
constructor(public readonly label: string, private spec: DemoSpec<P>) {}
get description() {
return this.spec.description;
}
async configure(props: P): Promise<P> {
if (this.parent) {
props = await this.parent.configure({ ...props });
}
return this.spec.configure?.(props) ?? props;
}
render(props: P): ReactElement {
return this.spec.render(props);
}
}