forked from sweetalert2/sweetalert2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweetalert2.d.ts
725 lines (618 loc) · 19.6 KB
/
sweetalert2.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
declare module 'sweetalert2' {
/**
* Shorthand function to display a simple SweetAlert modal.
*
* ex.
* import swal from 'sweetalert2';
* swal('The Internet?', 'That thing is still around?', 'question');
*/
function swal(title: string, message?: string, type?: SweetAlertType): Promise<SweetAlertResult>;
/**
* Function to display a SweetAlert modal, with an object of options, all being optional.
* See the SweetAlertOptions interface for the list of accepted fields and values.
*
* ex.
* import swal from 'sweetalert2';
* swal({
* title: 'Auto close alert!',
* text: 'I will close in 2 seconds.',
* timer: 2000
* })
*/
function swal(settings: SweetAlertOptions & { useRejections?: false }): Promise<SweetAlertResult>;
/**
* @deprecated
* swal() overload for legacy alerts that use { useRejections: true }.
*/
function swal(settings: SweetAlertOptions & { useRejections: true }): Promise<any>;
/**
* A namespace inside the default function, containing utility function for controlling the currently-displayed
* modal.
*
* ex.
* import swal from 'sweetalert2';
*
* swal('Hey user!', 'I don\'t like you.', 'warning');
*
* if(swal.isVisible()) { // instant regret
* swal.close();
* }
*/
namespace swal {
/**
* Determine if modal is shown.
* Be aware that the library returns a trueish or falsy value, not a real boolean.
*/
function isVisible(): boolean;
/**
* If you end up using a lot of the same settings when calling SweetAlert2,
* you can use setDefaults at the start of your program to set them once and for all!
*/
function setDefaults(defaultOptions: SweetAlertOptions): void;
/**
* Resets settings to their default value.
*/
function resetDefaults(): void;
/**
* Close the currently open SweetAlert2 modal programmatically.
*/
function close(onComplete?: (modalElement: HTMLElement) => void): void;
/**
* Get the modal title.
*/
function getTitle(): HTMLElement;
/**
* Get the modal content.
*/
function getContent(): HTMLElement;
/**
* Get the image.
*/
function getImage(): HTMLElement;
/**
* Get the "Confirm" button.
*/
function getConfirmButton(): HTMLElement;
/**
* Get the "Cancel" button.
*/
function getCancelButton(): HTMLElement;
/**
* Get the buttons wrapper.
*/
function getButtonsWrapper(): HTMLElement;
/**
* Get actions (buttons) wrapper.
*/
function getActions(): HTMLElement;
/**
* Get the modal footer.
*/
function getFooter(): HTMLElement;
/**
* Enable "Confirm" and "Cancel" buttons.
*/
function enableButtons(): void;
/**
* Disable "Confirm" and "Cancel" buttons.
*/
function disableButtons(): void;
/**
* Enable the "Confirm"-button only.
*/
function enableConfirmButton(): void;
/**
* Disable the "Confirm"-button only.
*/
function disableConfirmButton(): void;
/**
* Disable buttons and show loader. This is useful with AJAX requests.
*/
function showLoading(): void;
/**
* Enable buttons and hide loader.
*/
function hideLoading(): void;
/**
* Determine if modal is in the loading state.
*/
function isLoading(): boolean;
/**
* Click the "Confirm"-button programmatically.
*/
function clickConfirm(): void;
/**
* Click the "Cancel"-button programmatically.
*/
function clickCancel(): void;
/**
* Show validation error message.
*/
function showValidationError(error: string): void;
/**
* Hide validation error message.
*/
function resetValidationError(): void;
/**
* Get the input DOM node, this method works with input parameter.
*/
function getInput(): HTMLElement;
/**
* Disable input. A disabled input element is unusable and un-clickable.
*/
function disableInput(): void;
/**
* Enable input.
*/
function enableInput(): void;
/**
* Provide array of SweetAlert2 parameters to show multiple modals, one modal after another.
*/
function queue(steps: Array<SweetAlertOptions | string>): Promise<any>;
/**
* Get the index of current modal in queue. When there's no active queue, null will be returned.
*/
function getQueueStep(): string | null;
/**
* Insert a modal to queue, you can specify modal positioning with second parameter.
* By default a modal will be added to the end of a queue.
*/
function insertQueueStep(step: SweetAlertOptions, index?: number): number;
/**
* Delete a modal at index from queue.
*/
function deleteQueueStep(index: number): void;
/**
* Progress steps getter.
*/
function getProgressSteps(): string[];
/**
* Progress steps setter.
*/
function setProgressSteps(steps: string[]): void;
/**
* Show progress steps.
*/
function showProgressSteps(): void;
/**
* Hide progress steps.
*/
function hideProgressSteps(): void;
/**
* Determine if parameter name is valid.
*/
function isValidParameter(paramName: string): boolean;
/**
* An utility function to make SweetAlert rejections silencious (no error in the console when clicking Cancel).
* ex. swal(...).catch(swal.noop)
*/
function noop(): void;
}
export enum SweetAlertDismissReason {
cancel = 'cancel',
backdrop = 'overlay',
close = 'close',
esc = 'esc',
timer = 'timer',
}
export type SweetAlertType = 'success' | 'error' | 'warning' | 'info' | 'question';
export interface SweetAlertResult {
value?: any;
dismiss?: SweetAlertDismissReason;
}
type SyncOrAsync<T> = T | Promise<T>;
type ValueOrThunk<T> = T | (() => T);
export interface SweetAlertOptions {
/**
* The title of the modal, as HTML.
* It can either be added to the object under the key "title" or passed as the first parameter of the function.
*
* @default null
*/
title?: string;
/**
* The title of the modal, as text. Useful to avoid HTML injection.
*
* @default null
*/
titleText?: string;
/**
* A description for the modal.
* It can either be added to the object under the key "text" or passed as the second parameter of the function.
*
* @default null
*/
text?: string;
/**
* A HTML description for the modal.
* If "text" and "html" parameters are provided in the same time, "text" will be used.
*
* @default null
*/
html?: string | JQuery;
/**
* The footer of the modal, as HTML.
*
* @default null
*/
footer?: string | JQuery;
/**
* The type of the modal.
* SweetAlert2 comes with 5 built-in types which will show a corresponding icon animation: 'warning', 'error',
* 'success', 'info' and 'question'.
* It can either be put in the array under the key "type" or passed as the third parameter of the function.
*
* @default null
*/
type?: SweetAlertType;
/**
* Whether or not SweetAlert2 should show a full screen click-to-dismiss backdrop.
* Either a boolean value or a css background value (hex, rgb, rgba, url, etc.)
*
* @default true
*/
backdrop?: boolean | string;
/**
* Whether or not an alert should be treated as a toast notification.
* This option is normally coupled with the `position` parameter and a timer.
* Toasts are NEVER autofocused.
*
* @default false
*/
toast?: boolean;
/**
* The container element for adding modal into (query selector only).
*
* @default 'body'
*/
target?: string;
/**
* Input field type, can be text, email, password, number, tel, range, textarea, select, radio, checkbox, file
* and url.
*
* @default null
*/
input?:
'text' | 'email' | 'password' | 'number' | 'tel' | 'range' | 'textarea' | 'select' | 'radio' | 'checkbox' |
'file' | 'url';
/**
* Modal window width, including paddings (box-sizing: border-box). Can be in px or %.
*
* @default null
*/
width?: number | string;
/**
* Modal window padding.
*
* @default null
*/
padding?: number;
/**
* Modal window background (CSS background property).
*
* @default '#fff'
*/
background?: string;
/**
* Modal window position
*
* @default 'center'
*/
position?:
'top' | 'top-start' | 'top-end' | 'top-left' | 'top-right' |
'center' | 'center-start' | 'center-end' | 'center-left' | 'center-right' |
'bottom' | 'bottom-start' | 'bottom-end' | 'bottom-left' | 'bottom-right';
/**
* Modal window grow direction
*
* @default false
*/
grow?: 'row' | 'column' | 'fullscreen' | false;
/**
* A custom CSS class for the modal.
*
* @default null
*/
customClass?: string;
/**
* Auto close timer of the modal. Set in ms (milliseconds).
*
* @default null
*/
timer?: number;
/**
* If set to false, modal CSS animation will be disabled.
*
* @default true
*/
animation?: ValueOrThunk<boolean>;
/**
* If set to false, the user can't dismiss the modal by clicking outside it.
* You can also pass a custom function returning a boolean value, e.g. if you want
* to disable outside clicks for the loading state of a modal.
*
* @default true
*/
allowOutsideClick?: ValueOrThunk<boolean>;
/**
* If set to false, the user can't dismiss the modal by pressing the Escape key.
* You can also pass a custom function returning a boolean value, e.g. if you want
* to disable the escape key for the loading state of a modal.
*
* @default true
*/
allowEscapeKey?: ValueOrThunk<boolean>;
/**
* If set to false, the user can't confirm the modal by pressing the Enter or Space keys,
* unless they manually focus the confirm button.
* You can also pass a custom function returning a boolean value.
*
* @default true
*/
allowEnterKey?: ValueOrThunk<boolean>;
/**
* If set to false, a "Confirm"-button will not be shown.
* It can be useful when you're using custom HTML description.
*
* @default true
*/
showConfirmButton?: boolean;
/**
* If set to true, a "Cancel"-button will be shown, which the user can click on to dismiss the modal.
*
* @default false
*/
showCancelButton?: boolean;
/**
* Use this to change the text on the "Confirm"-button.
*
* @default 'OK'
*/
confirmButtonText?: string;
/**
* Use this to change the text on the "Cancel"-button.
*
* @default 'Cancel'
*/
cancelButtonText?: string;
/**
* Use this to change the background color of the "Confirm"-button (must be a HEX value).
*
* @default '#3085d6'
*/
confirmButtonColor?: string;
/**
* Use this to change the background color of the "Cancel"-button (must be a HEX value).
*
* @default '#aaa'
*/
cancelButtonColor?: string;
/**
* A custom CSS class for the "Confirm"-button.
*
* @default null
*/
confirmButtonClass?: string;
/**
* A custom CSS class for the "Cancel"-button.
*
* @default null
*/
cancelButtonClass?: string;
/**
* Use this to change the aria-label for the "Confirm"-button.
*
* @default ''
*/
confirmButtonAriaLabel?: string;
/**
* Use this to change the aria-label for the "Cancel"-button.
*
* @default ''
*/
cancelButtonAriaLabel?: string;
/**
* Whether to apply the default swal2 styling to buttons.
* If you want to use your own classes (e.g. Bootstrap classes) set this parameter to false.
*
* @default true
*/
buttonsStyling?: boolean;
/**
* Set to true if you want to invert default buttons positions.
*
* @default false
*/
reverseButtons?: boolean;
/**
* Set to false if you want to focus the first element in tab order instead of "Confirm"-button by default.
*
* @default true
*/
focusConfirm?: boolean;
/**
* Set to true if you want to focus the "Cancel"-button by default.
*
* @default false
*/
focusCancel?: boolean;
/**
* Set to true to show close button in top right corner of the modal.
*
* @default false
*/
showCloseButton?: boolean;
/**
* Use this to change the `aria-label` for the close button.
*
* @default 'Close this dialog'
*/
closeButtonAriaLabel?: string;
/**
* Set to true to disable buttons and show that something is loading. Useful for AJAX requests.
*
* @default false
*/
showLoaderOnConfirm?: boolean;
/**
* Function to execute before confirm, may be async (Promise-returning) or sync.
*
* ex.
* swal({
* title: 'Multiple inputs',
* html:
* '<input id="swal-input1" class="swal2-input">' +
* '<input id="swal-input2" class="swal2-input">',
* focusConfirm: false,
* preConfirm: () => [$('#swal-input1').val(), $('#swal-input2').val()]
* }).then(result => swal(JSON.stringify(result));
*
* @default null
*/
preConfirm?: (inputValue: any) => SyncOrAsync<any | void>;
/**
* Add a customized icon for the modal. Should contain a string with the path or URL to the image.
*
* @default null
*/
imageUrl?: string;
/**
* If imageUrl is set, you can specify imageWidth to describes image width in px.
*
* @default null
*/
imageWidth?: number;
/**
* If imageUrl is set, you can specify imageHeight to describes image height in px.
*
* @default null
*/
imageHeight?: number;
/**
* An alternative text for the custom image icon.
*
* @default ''
*/
imageAlt?: string;
/**
* A custom CSS class for the customized icon.
*
* @default null
*/
imageClass?: string;
/**
* Input field placeholder.
*
* @default ''
*/
inputPlaceholder?: string;
/**
* Input field initial value.
*
* @default ''
*/
inputValue?: any;
/**
* If input parameter is set to "select" or "radio", you can provide options.
* Object keys will represent options values, object values will represent options text values.
*/
inputOptions?: SyncOrAsync<Map<string, string> | { [inputValue: string]: string }>;
/**
* Automatically remove whitespaces from both ends of a result string.
* Set this parameter to false to disable auto-trimming.
*
* @default true
*/
inputAutoTrim?: boolean;
/**
* HTML input attributes (e.g. min, max, step, accept...), that are added to the input field.
*
* ex.
* swal({
* title: 'Select a file',
* input: 'file',
* inputAttributes: {
* accept: 'image/*'
* }
* })
*
* @default null
*/
inputAttributes?: { [attribute: string]: string; };
/**
* Validator for input field, may be async (Promise-returning) or sync.
*
* ex.
* swal({
* title: 'Select color',
* input: 'radio',
* inputValidator: result => !result && 'You need to select something!'
* })
*
* @default null
*/
inputValidator?: (inputValue: any) => SyncOrAsync<string | null>;
/**
* A custom CSS class for the input field.
*
* @default null
*/
inputClass?: string;
/**
* Progress steps, useful for modal queues, see usage example.
*
* @default []
*/
progressSteps?: string[];
/**
* Current active progress step.
*
* @default swal.getQueueStep()
*/
currentProgressStep?: string;
/**
* Distance between progress steps.
*
* @default null
*/
progressStepsDistance?: string;
/**
* Function to run when modal built, but not shown yet. Provides modal DOM element as the first argument.
*
* @default null
*/
onBeforeOpen?: (modalElement: HTMLElement) => void;
/**
* Function to run when modal opens, provides modal DOM element as the first argument.
*
* @default null
*/
onOpen?: (modalElement: HTMLElement) => void;
/**
* Function to run when modal closes, provides modal DOM element as the first argument.
*
* @default null
*/
onClose?: (modalElement: HTMLElement) => void;
/**
* Determines whether given `inputValidator` and `preConfirm` functions should be expected to to signal
* validation errors by rejecting, or by their respective means (see documentation for each option).
*
* @default false
* @deprecated
*/
expectRejections?: boolean;
}
export default swal;
}
/**
* These interfaces aren't provided by SweetAlert2, but its definitions use them.
* They will be merged with 'true' definitions.
*/
// tslint:disable:no-empty-interface
interface JQuery {
}
interface Promise<T> {
}
interface Map<K, V> {
}