Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e4b944e

Browse files
committedSep 19, 2024
refactor(toast): use ComponentRef setInput() api, input signals
1 parent 4a8a291 commit e4b944e

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed
 

‎projects/coreui-angular/src/lib/toast/toast/toast.component.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import {
33
ChangeDetectorRef,
44
Component,
55
ElementRef,
6-
EventEmitter,
76
HostBinding,
87
HostListener,
98
Input,
9+
input,
1010
numberAttribute,
1111
OnDestroy,
1212
OnInit,
13-
Output,
13+
output,
1414
Renderer2
1515
} from '@angular/core';
1616

@@ -50,44 +50,45 @@ type AnimateType = 'hide' | 'show';
5050
host: { class: 'toast show' }
5151
})
5252
export class ToastComponent implements OnInit, OnDestroy {
53-
public dynamic!: boolean;
54-
public placement!: TToasterPlacement;
55-
5653
constructor(
5754
public hostElement: ElementRef,
5855
public renderer: Renderer2,
5956
public toasterService: ToasterService,
6057
public changeDetectorRef: ChangeDetectorRef
6158
) {}
6259

60+
readonly dynamic = input<boolean>();
61+
readonly placement = input<TToasterPlacement>();
62+
6363
/**
6464
* Auto hide the toast.
6565
* @type boolean
6666
*/
67-
@Input() autohide: boolean = true;
67+
readonly autohide = input(true);
6868

6969
/**
7070
* Sets the color context of the component to one of CoreUI’s themed colors.
7171
* @type Colors
7272
*/
73-
@Input() color?: Colors = '';
73+
readonly color = input<Colors>('');
7474

7575
/**
7676
* Delay hiding the toast (ms).
7777
* @type number
7878
*/
79-
@Input({ transform: numberAttribute }) delay: number = 5000;
79+
readonly delay = input(5000, { transform: numberAttribute });
8080

8181
/**
8282
* Apply fade transition to the toast.
8383
* @type boolean
8484
*/
85-
@Input() fade: boolean = true;
85+
readonly fade = input(true);
8686

8787
/**
8888
* Toggle the visibility of component.
8989
* @type boolean
9090
*/
91+
9192
@Input({ transform: booleanAttribute })
9293
set visible(value: boolean) {
9394
const newValue = value;
@@ -108,19 +109,19 @@ export class ToastComponent implements OnInit, OnDestroy {
108109
/**
109110
* @ignore
110111
*/
111-
@Input({ transform: numberAttribute }) index?: number;
112+
readonly index = input(0, { transform: numberAttribute });
112113

113114
/**
114115
* Event emitted on visibility change. [docs]
115-
* @type EventEmitter<boolean>
116+
* @type <boolean>
116117
*/
117-
@Output() visibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
118+
readonly visibleChange = output<boolean>();
118119

119120
/**
120121
* Event emitted on timer tick. [docs]
121122
* @type number
122123
*/
123-
@Output() timer: EventEmitter<number> = new EventEmitter();
124+
readonly timer = output<number>();
124125

125126
private timerId: ReturnType<typeof setTimeout> | undefined;
126127
private clockId: ReturnType<typeof setInterval> | undefined;
@@ -140,7 +141,7 @@ export class ToastComponent implements OnInit, OnDestroy {
140141

141142
@HostBinding('@.disabled')
142143
get animationDisabled(): boolean {
143-
return !this.fade;
144+
return !this.fade();
144145
}
145146

146147
@HostBinding('@fadeInOut')
@@ -161,8 +162,8 @@ export class ToastComponent implements OnInit, OnDestroy {
161162
return {
162163
toast: true,
163164
show: true,
164-
[`bg-${this.color}`]: !!this.color,
165-
'border-0': !!this.color
165+
[`bg-${this.color()}`]: !!this.color(),
166+
'border-0': !!this.color()
166167
};
167168
}
168169

@@ -171,7 +172,7 @@ export class ToastComponent implements OnInit, OnDestroy {
171172
this.toasterService.setState({
172173
toast: this,
173174
show: this.visible,
174-
placement: this.placement
175+
placement: this.placement()
175176
});
176177
this.clearTimer();
177178
this.setTimer();
@@ -184,8 +185,8 @@ export class ToastComponent implements OnInit, OnDestroy {
184185

185186
setTimer(): void {
186187
this.clearTimer();
187-
if (this.autohide && this.visible) {
188-
this.timerId = this.delay > 0 ? setTimeout(() => this.onClose(), this.delay) : undefined;
188+
if (this.autohide() && this.visible) {
189+
this.timerId = this.delay() > 0 ? setTimeout(() => this.onClose(), this.delay()) : undefined;
189190
this.setClock();
190191
}
191192
}
@@ -201,7 +202,7 @@ export class ToastComponent implements OnInit, OnDestroy {
201202
this.toasterService.setState({
202203
toast: this,
203204
show: false,
204-
placement: this.placement
205+
placement: this.placement()
205206
});
206207
}
207208

@@ -214,7 +215,7 @@ export class ToastComponent implements OnInit, OnDestroy {
214215
}, 1000);
215216
this.clockTimerId = setTimeout(() => {
216217
this.clearClock();
217-
}, this.delay);
218+
}, this.delay());
218219
}
219220

220221
clearClock(): void {

‎projects/coreui-angular/src/lib/toast/toaster/toaster.component.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,30 @@ export class ToasterComponent implements OnInit, AfterContentChecked {
124124
this.toastsDynamic.push(componentRef);
125125
const index = this.toastsDynamic.indexOf(componentRef);
126126
for (const [key, value] of Object.entries(props)) {
127-
componentRef.instance[key] = value;
127+
componentRef.setInput(key, value);
128128
}
129-
componentRef.instance['placement'] = this.placement;
130-
componentRef.instance['dynamic'] = true;
131-
componentRef.instance['index'] = index;
132-
componentRef.instance['visible'] = true;
129+
componentRef.setInput('placement', this.placement);
130+
componentRef.setInput('dynamic', true);
131+
componentRef.setInput('index', index);
132+
componentRef.setInput('visible', true);
133133
componentRef.instance['visibleChange'].emit(true);
134134
componentRef.changeDetectorRef?.detectChanges();
135135
return componentRef;
136136
}
137137

138138
public removeToast(state: IToasterAction): void {
139139
this.toastsDynamic?.forEach((item) => {
140-
if (state.toast?.dynamic && item.instance === state.toast) {
141-
item.instance.visible = false;
140+
if (state.toast?.dynamic() && item.instance === state.toast) {
141+
item.setInput('visible', false);
142142
item.instance['visibleChange'].emit(false);
143143
item.destroy();
144144
}
145145
});
146146

147147
this.toasts?.forEach((item) => {
148148
if (state.toast && item.element.nativeElement === state.toast.hostElement.nativeElement) {
149-
if (!state.toast.dynamic) {
150-
state.toast.visible = false;
149+
if (!state.toast.dynamic()) {
150+
state.toast['visible'] = false;
151151
}
152152
}
153153
});
@@ -158,9 +158,9 @@ export class ToasterComponent implements OnInit, AfterContentChecked {
158158
if (state.show === false) {
159159
this.removeToast(state);
160160
}
161-
if (state.show === true && state.toast?.dynamic === undefined) {
162-
/* empty */
163-
}
161+
// if (state.show === true && state.toast?.dynamic() === undefined) {
162+
// /* empty */
163+
// }
164164
});
165165
}
166166
}

0 commit comments

Comments
 (0)
Please sign in to comment.