-
Notifications
You must be signed in to change notification settings - Fork 2
/
dialog-helper.d.ts
363 lines (341 loc) · 13 KB
/
dialog-helper.d.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* The `xd-dialog-helper` module
*
* @author Pablo Klaschka <[email protected]>
*/
declare module 'xd-dialog-helper' {
/**
* A list of actions that can get performed for the dialog
*/
export interface ActionList {
/**
* close submit dialog if valid
*/
close(): void;
/**
* cancel cancel the dialog
*/
cancel(): void;
/**
* change value of ContentElement has changed
*/
change(): void;
/**
* get dialog values
*/
values(): object;
/**
* register content ContentElement, e.g., child of a section ContentElement
* @param element child element
*/
registerContentElement(element: ContentElement): void;
}
/**
* The parsed element, coming from the element types render function. Contains view and model information about the element.
*/
export interface ContentElement {
/**
* The wrapper (or outer-most) HTML element specific to the content element
*/
readonly wrapper: HTMLElement;
/**
* The content element type of the element
*/
readonly type: ContentElementType;
/**
* The properties with which the element got declared
*/
readonly props: ContentElementDeclaration;
/**
* The input HTML element, if applicable
*/
readonly input?: HTMLElement;
}
/**
* A declaration for a content element which gets passed to the `showDialog()` function.
*/
export interface ContentElementDeclaration {
/**
* The unique id of the element. Will also get used for HTML element ids
*
* **Required for all types**
*/
id: string;
/**
* The type of the content element
*
* **Required for all types**
*/
type: ContentElementType;
/**
* The label of the element (may that be the text in case of a `types.TEXT` or the slider label in case of a `types.SLIDER`
*
* **Required for all types**
*/
label: string;
/**
* Initial value, if applicable
*
* **Required for:** n/a
* **Optional for:** `CHECKBOX`, `SELECT`, `SLIDER`, `TEXT_AREA`, `TEXT_INPUT`
*/
value?: any;
/**
* HTML attributes in a key-value-pair object form that get applied to an element – dependent on the `type`. See documentation for the `type` for more information about how the attributes get applied for the specific type
*
* **Required for:** `SLIDER`
* **Optional for:** `CHECKBOX`, `HEADER`, `HR`, `SELECT`, `TEXT`, `TEXT_AREA`, `TEXT_INPUT`
*/
htmlAttributes?: {
/**
* The minimum numeric value
*
* **Required for:** `SLIDER`
* **Optional for:** n/a
*/
min?: number;
/**
* The maximum numeric value
*
* **Required for:** `SLIDER`
* **Optional for:** n/a
*/
max?: number;
[property: string]: string | number | boolean | undefined;
};
/**
* The unit of the input.
*
* **Required for:** n/a
* **Optional for:** `SLIDER`
*/
unit?: string;
/**
* Whether an input is required (or a checkbox must be checked) in this field
*
* **Required for:** n/a
* **Optional for:** `CHECKBOX`, `TEXT_AREA`, `TEXT_INPUT`
*/
required?: boolean;
[property: string]: any;
}
/**
* A content element type. Defines how an element should behave, render etc.
*/
export interface ContentElementType {
/**
* Parses the content element declaration – passed as `props` and converts it into an actual content element (including its UI elements like `wrapper` etc.)
* @param dialogId The dialogs id, should get used as a prefix for elements: `[dialog-id]-[element-id]-[...]`
* @param props The element declaration which got passed to `showDialog(..., contents, ...)`
* @param actions A list of actions which can get triggered (like close, cancel etc.). The most important one is `actions.change()`, which should get triggered whenever the element's value changes.
* @returns The parsed content element, including UI elements like `wrapper`, the passed properties etc.
*/
render(dialogId: string, props: ContentElementDeclaration, actions: ActionList): ContentElement;
/**
* Determines the current value of the content element
* @param element The content element
*/
value(element: ContentElement): any;
/**
* Determines if the element's inputs – based on the props passed in the declaration (e.g., `required`), are valid
* @param element The content element
*/
valid(element: ContentElement): any;
/**
* The element type's name (gets used for console output etc.)
*/
readonly type: string;
}
/**
* Show a dialog and await its results
* @param id The dialog id. Must be unique to identify elements by their element ids.
* @param title The title of the dialog. Gets displayed at the top.
* @param contents Content elements of the dialog
* @param options Additional options
* @returns {Promise<object>} Promise that resolves with the dialog values in a key-value-pair form (as an object) or rejects if the user cancels the dialog
*/
export function showDialog(
id: string,
title: string,
contents?: ContentElementDeclaration[],
options?: {
/**
* The text in the "ok" button (e.g., "ok", "insert" or similar)
* @default 'Ok'
*/
okButtonText?: string;
/**
* The text in the "cancel" button (e.g., "cancel", "abort" or similar)
* @default 'Cancel'
*/
cancelButtonText?: string;
/**
* CSS code that gets injected into the style
* @default ''
*/
css?: string;
/**
* The dialog width in px
* @default 360
*/
width?: number;
/**
* A function that gets triggered before the dialog gets shown.
* You can – e.g. – inject custom code here.
* @param dialogElement The dialog element that gets shown
* @param elements The dialog's elements in a key-value based manner (the key corresponds to the name of an input).,
* @param actions Actions which can get performed like closing or canceling the dialog
*/
onBeforeShow?: (dialogElement: HTMLDialogElement, elements: ContentElement[], actions: ActionList) => void;
/**
* A function that gets triggered when inputs change. Its return value
* determines if the inputs are value and therefore, if the ok button is clickable
* @param values The dialog values in a key-value-pair form (as an object)
* @return {boolean} true if the values are valid, false if they're not
*/
onValidate?: (values: object) => boolean;
}
): Promise<any>;
/**
* @deprecated Using the types from the `dialog-helper` class directly is deprecated and will get removed in v1.0.
* Use `require('xd-dialog-helper').types.CHECKBOX` instead.
*/
export const CHECKBOX: ContentElementType;
/**
* @deprecated Using the types from the `dialog-helper` class directly is deprecated and will get removed in v1.0.
* Use `require('xd-dialog-helper').types.HEADER` instead.
*/
export const HEADER: ContentElementType;
/**
* @deprecated Using the types from the `dialog-helper` class directly is deprecated and will get removed in v1.0.
* Use `require('xd-dialog-helper').types.HR` instead.
*/
export const HR: ContentElementType;
/**
* @deprecated Using the types from the `dialog-helper` class directly is deprecated and will get removed in v1.0.
* Use `require('xd-dialog-helper').types.SELECT` instead.
*/
export const SELECT: ContentElementType;
/**
* @deprecated Using the types from the `dialog-helper` class directly is deprecated and will get removed in v1.0.
* Use `require('xd-dialog-helper').types.SLIDER` instead.
*/
export const SLIDER: ContentElementType;
/**
* @deprecated Using the types from the `dialog-helper` class directly is deprecated and will get removed in v1.0.
* Use `require('xd-dialog-helper').types.TEXT` instead.
*/
export const TEXT: ContentElementType;
/**
* @deprecated Using the types from the `dialog-helper` class directly is deprecated and will get removed in v1.0.
* Use `require('xd-dialog-helper').types.TEXT_AREA` instead.
*/
export const TEXT_AREA: ContentElementType;
/**
* @deprecated Using the types from the `dialog-helper` class directly is deprecated and will get removed in v1.0.
* Use `require('xd-dialog-helper').types.TEXT_INPUT` instead.
*/
export const TEXT_INPUT: ContentElementType;
interface types {
/**
* A checkbox element
*
* ### Required props
* - `id: string`
* - `label: string`
*
* ### Supported props
* - `value: boolean` – Initial value (default: `false`)
* - `htmlAttributes: object` – get applied to the `<input type="checkbox">` element
* - `required: boolean` – determines if checkbox must be checked for the element to be valid (default: `false`)
*/
readonly CHECKBOX: ContentElementType;
/**
* A headline element
*
* ### Required props
* - `id: string`
* - `label: string`
*
* ### Supported props
* - `htmlAttributes: object` – get applied to the `<h2>` element
*/
readonly HEADER: ContentElementType;
/**
* A horizontal rule (`<hr>`) element
*
* ### Required props
* - `id: string`
*
* ### Supported props
* - `htmlAttributes: object` – get applied to the `<hr>` element
*/
readonly HR: ContentElementType;
/**
* A selection/dropdown element
*
* ### Required props
* - `id: string`
* - `label: string`
* - `options: Array<{value: string, label: string}>`
*
* ### Supported props
* - `value: string` – Initial value (value of the initially selected option in `options`)
* - `htmlAttributes: object` – get applied to the `<select>` element
*/
readonly SELECT: ContentElementType;
/**
* A numeric value slider element
*
* ### Required props
* - `id: string`
* - `label: string`
* - `value: number` – Initial value of the slider
* - `htmlAttributes: object` – get applied to the `<input type="range">` element
* - `htmlAttributes.min: number` – minimum slider value
* - `htmlAttributes.max: number` – maximum slider value
*
* ### Supported props
* - `unit: string` – Unit of the slider – gets displayed on the right-hand side above the slider
*/
readonly SLIDER: ContentElementType;
/**
* A (static) text element
*
* ### Required props
* - `id: string`
* - `label: string`
*
* ### Supported props
* - `htmlAttributes: object` – get applied to the `<p>` element
*/
readonly TEXT: ContentElementType;
/**
* A text area input element
*
* ### Required props
* - `id: string`
* - `label: string`
*
* ### Supported props
* - `value: string` – Initial value (default: `''`)
* - `htmlAttributes: object` – get applied to the `<textarea>` element
* - `required: boolean` – determines if text is required (i.e., the field must not be empty) for the element to be valid (default: `false`)
*/
readonly TEXT_AREA: ContentElementType;
/**
* A (single-line) text input element
*
* ### Required props
* - `id: string`
* - `label: string`
*
* ### Supported props
* - `value: string` – Initial value (default: `''`)
* - `htmlAttributes: object` – get applied to the `<input type="text">` element
* - `required: boolean` – determines if text is required (i.e., the field must not be empty) for the element to be valid (default: `false`)
*/
readonly TEXT_INPUT: ContentElementType;
}
export const types: types;
}