1
1
import { DIALOG_DATA , DialogRef } from '@angular/cdk/dialog' ;
2
2
import { ChangeDetectionStrategy , Component , inject } from '@angular/core' ;
3
+ import { toSignal } from '@angular/core/rxjs-interop' ;
3
4
import { FormControl , ReactiveFormsModule , Validators } from '@angular/forms' ;
4
- import { TranslateModule } from '@ngx-translate/core' ;
5
+ import { TranslateModule , TranslateService } from '@ngx-translate/core' ;
6
+ import { map , skip } from 'rxjs' ;
5
7
import { DialogConfig } from '../../types/dialog-config' ;
6
8
7
9
@Component ( {
@@ -14,7 +16,11 @@ import { DialogConfig } from '../../types/dialog-config';
14
16
export class DialogComponent {
15
17
protected readonly config = inject < DialogConfig > ( DIALOG_DATA ) ;
16
18
private readonly _dialogRef = inject ( DialogRef ) ;
19
+ private readonly _translate = inject ( TranslateService ) ;
17
20
21
+ /**
22
+ * Label for confirm button.
23
+ */
18
24
protected get confirmLabel ( ) : string {
19
25
if ( this . config . type === 'alert' ) {
20
26
return 'common.ok' ;
@@ -23,13 +29,69 @@ export class DialogComponent {
23
29
return this . config . confirmLabel ;
24
30
}
25
31
32
+ /**
33
+ * Input control for prompt dialog.
34
+ */
26
35
protected readonly input = new FormControl ( '' , {
27
36
nonNullable : true ,
28
37
validators : [ Validators . required ]
29
38
} ) ;
30
39
40
+ /**
41
+ * Subject that emits true if input is invalid after the first change.
42
+ */
43
+ readonly #invalid$ = this . input . valueChanges . pipe (
44
+ // Skip the first value since it's the initial value
45
+ skip ( 1 ) ,
46
+ // Check if input is invalid
47
+ map ( ( ) => this . input . invalid )
48
+ ) ;
49
+
50
+ /**
51
+ * Signal containing the invalid state of the input.
52
+ */
53
+ protected readonly invalid = toSignal ( this . #invalid$, { initialValue : false } ) ;
54
+
55
+ /**
56
+ * Signal containing the validation error message.
57
+ */
58
+ protected readonly validationError = toSignal < string , string > (
59
+ this . #invalid$. pipe (
60
+ map ( ( invalid ) => {
61
+ // Check if input is valid or dialog is not prompt
62
+ if ( ! invalid || this . config . type !== 'prompt' ) {
63
+ return '' ;
64
+ }
65
+
66
+ // Use default error message if no custom validation is set
67
+ if ( ! this . config . validation ) {
68
+ return this . _translate . instant ( 'common.required' ) ;
69
+ }
70
+
71
+ // Find the first error key
72
+ const errorKey = Object . keys ( this . config . validation . errorMessageKeys ) . find ( ( key ) => this . input . hasError ( key ) ) ;
73
+ if ( ! errorKey ) {
74
+ return '' ;
75
+ }
76
+
77
+ // Get the error message
78
+ const error = this . input . getError ( errorKey ) ;
79
+ return this . _translate . instant ( this . config . validation . errorMessageKeys [ errorKey ] , { value : error . value } ) ;
80
+ } )
81
+ ) ,
82
+ {
83
+ initialValue : ''
84
+ }
85
+ ) ;
86
+
31
87
public constructor ( ) {
32
88
if ( this . config . type === 'prompt' ) {
89
+ // Set validators to input
90
+ if ( this . config . validation ) {
91
+ this . input . setValidators ( this . config . validation . validators ) ;
92
+ }
93
+
94
+ // Set initial value to input
33
95
this . input . setValue ( this . config . initialValue ?? '' ) ;
34
96
}
35
97
}
0 commit comments