-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dialog.tsx
41 lines (31 loc) · 963 Bytes
/
Dialog.tsx
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
import { Defer } from 'iterable-observer';
import { observable } from 'mobx';
import { observer } from 'mobx-react';
import { FC } from 'react';
export class DialogClose extends Error {
constructor(message = 'Dialog closed') {
super(message);
}
}
export interface DialogProps<T = any> {
defer?: Defer<T>;
}
export class Dialog<I extends object = {}, O = any> {
@observable
accessor input = {} as I;
@observable
accessor defer: Defer<O> | undefined;
Component: FC<I>;
constructor(Factory: FC<I & DialogProps<O>>) {
this.Component = observer(props => (
<Factory {...props} {...this.input} defer={this.defer} />
));
this.Component.displayName = 'DialogComponent';
}
open(input?: I) {
if (input) this.input = input;
this.defer = new Defer();
this.defer.promise.finally(() => (this.defer = undefined));
return this.defer.promise;
}
}