-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.ts
116 lines (104 loc) · 2.91 KB
/
builder.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
/**
* Defines the options for a Dialog component.
*/
interface DialogOptions {
title: string;
description: string;
modal?: boolean;
/**
* Defines optional callback functions to be invoked when the dialog is opened or closed.
*/
onClose?: () => void;
onOpen?: () => void;
}
/**
* Represents a Dialog component with configurable options.
*/
class Dialog {
constructor(public options: DialogOptions) {}
/**
* Returns a string representation of the Dialog options, including the title, description, and modal status.
* @returns {string} A string representation of the Dialog options.
*/
toString() {
return `Title: ${this.options.title}, Description: ${this.options.description}, Modal: ${this.options.modal}`;
}
/**
* Opens the dialog and invokes the `onOpen` callback if it is defined.
*/
open() {
if (this.options.onOpen) {
this.options.onOpen();
}
}
/**
* Closes the dialog and invokes the `onClose` callback if it is defined.
*/
close() {
if (this.options.onClose) {
this.options.onClose();
}
}
}
/**
* Provides a fluent interface for building a `Dialog` component with configurable options.
*/
class DialogBuilder {
private options: DialogOptions = {
title: "",
description: "",
};
/**
* Sets the title of the dialog.
* @param title - The title of the dialog.
* @returns The DialogBuilder instance, for chaining.
*/
setTitle(title: string) {
this.options.title = title;
return this;
}
/**
* Sets the description of the dialog.
* @param description - The description of the dialog.
* @returns The DialogBuilder instance, for chaining.
*/
setDescription(description: string) {
this.options.description = description;
return this;
}
/**
* Sets whether the dialog should be displayed as a modal.
* @param value - `true` to display the dialog as a modal, `false` to display it as a regular dialog.
* @returns The `DialogBuilder` instance, for chaining.
*/
setModal(value: boolean) {
this.options.modal = value;
return this;
}
/**
* Sets the callback function to be invoked when the dialog is closed.
* @param callback - The function to be called when the dialog is closed.
* @returns The `DialogBuilder` instance, for chaining.
*/
onClose(callback: () => void) {
this.options.onClose = callback;
return this;
}
/**
* Sets the callback function to be invoked when the dialog is opened.
* @param callback - The function to be called when the dialog is opened.
* @returns The `DialogBuilder` instance, for chaining.
*/
onOpen(callback: () => void) {
this.options.onOpen = callback;
return this;
}
/**
* Builds a new `Dialog` instance with the configured options.
* @returns A new `Dialog` instance with the configured options.
*/
build() {
return new Dialog(this.options);
}
}
export { Dialog, DialogBuilder };