1
- import type { BooleanInput } from '@angular/cdk/coercion'
2
- import { coerceBooleanProperty } from '@angular/cdk/coercion'
3
- import type { OnChanges , OnDestroy , OnInit , SimpleChanges } from '@angular/core'
1
+ import type { OnDestroy , OnInit } from '@angular/core'
4
2
import {
3
+ booleanAttribute ,
5
4
ChangeDetectionStrategy ,
6
5
ChangeDetectorRef ,
7
6
Component ,
8
- EventEmitter ,
9
7
HostBinding ,
10
8
inject ,
11
- Input ,
12
- Output ,
9
+ input ,
10
+ model ,
11
+ output ,
13
12
ViewEncapsulation ,
14
13
} from '@angular/core'
15
14
import { MatButtonModule } from '@angular/material/button'
@@ -30,75 +29,45 @@ import type { AlertAppearance, AlertType } from './alert.types'
30
29
exportAs : 'seedAlert' ,
31
30
imports : [ MatButtonModule , MatIconModule ] ,
32
31
} )
33
- export class AlertComponent implements OnChanges , OnInit , OnDestroy {
34
- static ngAcceptInputType_dismissible : BooleanInput
35
- static ngAcceptInputType_dismissed : BooleanInput
36
- static ngAcceptInputType_showIcon : BooleanInput
37
-
38
- private _changeDetectorRef = inject ( ChangeDetectorRef )
32
+ export class AlertComponent implements OnInit , OnDestroy {
39
33
private _alertService = inject ( AlertService )
34
+ private _changeDetectorRef = inject ( ChangeDetectorRef )
40
35
41
- @ Input ( ) appearance : AlertAppearance = 'soft'
42
- @ Input ( ) dismissed = false
43
- @ Input ( ) dismissible = false
44
- @ Input ( ) name : string = randomId ( )
45
- @ Input ( ) showIcon = true
46
- @ Input ( ) type : AlertType = 'primary'
47
- @ Output ( ) readonly dismissedChanged : EventEmitter < boolean > = new EventEmitter < boolean > ( )
36
+ appearance = input < AlertAppearance > ( 'soft' )
37
+ dismissed = model ( false )
38
+ dismissible = input ( false , { transform : booleanAttribute } )
39
+ name = input ( randomId ( ) )
40
+ showIcon = input ( true , { transform : booleanAttribute } )
41
+ type = input < AlertType > ( 'primary' )
42
+ dismissedChanged = output < boolean > ( )
48
43
49
44
private readonly _unsubscribeAll$ = new Subject < void > ( )
50
45
51
- /**
52
- * Host binding for component classes
53
- */
54
46
@HostBinding ( 'class' ) get classList ( ) : Record < string , boolean > {
55
47
return {
56
- 'seed-alert-appearance-border' : this . appearance === 'border' ,
57
- 'seed-alert-appearance-fill' : this . appearance === 'fill' ,
58
- 'seed-alert-appearance-outline' : this . appearance === 'outline' ,
59
- 'seed-alert-appearance-soft' : this . appearance === 'soft' ,
60
- 'seed-alert-dismissed' : this . dismissed ,
61
- 'seed-alert-dismissible' : this . dismissible ,
62
- 'seed-alert-show-icon' : this . showIcon ,
63
- 'seed-alert-type-primary' : this . type === 'primary' ,
64
- 'seed-alert-type-accent' : this . type === 'accent' ,
65
- 'seed-alert-type-warn' : this . type === 'warn' ,
66
- 'seed-alert-type-basic' : this . type === 'basic' ,
67
- 'seed-alert-type-info' : this . type === 'info' ,
68
- 'seed-alert-type-success' : this . type === 'success' ,
69
- 'seed-alert-type-warning' : this . type === 'warning' ,
70
- 'seed-alert-type-error' : this . type === 'error' ,
71
- }
72
- }
73
-
74
- ngOnChanges ( changes : SimpleChanges ) : void {
75
- // Dismissed
76
- if ( 'dismissed' in changes ) {
77
- // Coerce the value to a boolean
78
- this . dismissed = coerceBooleanProperty ( changes . dismissed . currentValue )
79
-
80
- // Dismiss/show the alert
81
- this . _toggleDismiss ( this . dismissed )
82
- }
83
-
84
- // Dismissible
85
- if ( 'dismissible' in changes ) {
86
- // Coerce the value to a boolean
87
- this . dismissible = coerceBooleanProperty ( changes . dismissible . currentValue )
88
- }
89
-
90
- // Show icon
91
- if ( 'showIcon' in changes ) {
92
- // Coerce the value to a boolean
93
- this . showIcon = coerceBooleanProperty ( changes . showIcon . currentValue )
48
+ 'seed-alert-appearance-border' : this . appearance ( ) === 'border' ,
49
+ 'seed-alert-appearance-fill' : this . appearance ( ) === 'fill' ,
50
+ 'seed-alert-appearance-outline' : this . appearance ( ) === 'outline' ,
51
+ 'seed-alert-appearance-soft' : this . appearance ( ) === 'soft' ,
52
+ 'seed-alert-dismissed' : this . dismissed ( ) ,
53
+ 'seed-alert-dismissible' : this . dismissible ( ) ,
54
+ 'seed-alert-show-icon' : this . showIcon ( ) ,
55
+ 'seed-alert-type-primary' : this . type ( ) === 'primary' ,
56
+ 'seed-alert-type-accent' : this . type ( ) === 'accent' ,
57
+ 'seed-alert-type-warn' : this . type ( ) === 'warn' ,
58
+ 'seed-alert-type-basic' : this . type ( ) === 'basic' ,
59
+ 'seed-alert-type-info' : this . type ( ) === 'info' ,
60
+ 'seed-alert-type-success' : this . type ( ) === 'success' ,
61
+ 'seed-alert-type-warning' : this . type ( ) === 'warning' ,
62
+ 'seed-alert-type-error' : this . type ( ) === 'error' ,
94
63
}
95
64
}
96
65
97
66
ngOnInit ( ) : void {
98
67
// Subscribe to the dismiss calls
99
68
this . _alertService . onDismiss
100
69
. pipe (
101
- filter ( ( name ) => this . name === name ) ,
70
+ filter ( ( name ) => this . name ( ) === name ) ,
102
71
takeUntil ( this . _unsubscribeAll$ ) ,
103
72
)
104
73
. subscribe ( ( ) => {
@@ -109,7 +78,7 @@ export class AlertComponent implements OnChanges, OnInit, OnDestroy {
109
78
// Subscribe to the show calls
110
79
this . _alertService . onShow
111
80
. pipe (
112
- filter ( ( name ) => this . name === name ) ,
81
+ filter ( ( name ) => this . name ( ) === name ) ,
113
82
takeUntil ( this . _unsubscribeAll$ ) ,
114
83
)
115
84
. subscribe ( ( ) => {
@@ -128,7 +97,7 @@ export class AlertComponent implements OnChanges, OnInit, OnDestroy {
128
97
*/
129
98
dismiss ( ) : void {
130
99
// Return if the alert is already dismissed
131
- if ( this . dismissed ) {
100
+ if ( this . dismissed ( ) ) {
132
101
return
133
102
}
134
103
@@ -141,7 +110,7 @@ export class AlertComponent implements OnChanges, OnInit, OnDestroy {
141
110
*/
142
111
show ( ) : void {
143
112
// Return if the alert is already showing
144
- if ( ! this . dismissed ) {
113
+ if ( ! this . dismissed ( ) ) {
145
114
return
146
115
}
147
116
@@ -151,21 +120,18 @@ export class AlertComponent implements OnChanges, OnInit, OnDestroy {
151
120
152
121
/**
153
122
* Dismiss/show the alert
154
- *
155
- * @param dismissed
156
- * @private
157
123
*/
158
124
private _toggleDismiss ( dismissed : boolean ) : void {
159
125
// Return if the alert is not dismissible
160
- if ( ! this . dismissible ) {
126
+ if ( ! this . dismissible ( ) ) {
161
127
return
162
128
}
163
129
164
130
// Set the dismissed
165
- this . dismissed = dismissed
131
+ this . dismissed . set ( dismissed )
166
132
167
- // Execute the observable
168
- this . dismissedChanged . next ( this . dismissed )
133
+ // Emit the event
134
+ this . dismissedChanged . emit ( this . dismissed ( ) )
169
135
170
136
// Notify the change detector
171
137
this . _changeDetectorRef . markForCheck ( )
0 commit comments