Skip to content

Commit 5e6892b

Browse files
authored
Merge pull request #10334 from IgniteUI/valadzhov/toast-position-not-working-10305-master
fix(toast): Adding checks for position settings #10305 - master
2 parents daa8d75 + a10cf08 commit 5e6892b

File tree

3 files changed

+99
-32
lines changed

3 files changed

+99
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ All notable changes for each version of this project will be documented in this
5353
- **Breaking Change** - The following input has been removed
5454
- Input `columns`. Use `igxGrid` `columns` input instead.
5555
- `IgxCarousel`
56-
- **Breaking Changes** -The carousel animation type `CarouselAnimationType` is renamed to `HorizontalAnimationType`.
56+
- **Breaking Changes** -The carousel animation type `CarouselAnimationType` is renamed to `HorizontalAnimationType`.
5757

5858
## 12.2.3
5959

projects/igniteui-angular/src/lib/toast/toast.component.spec.ts

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,49 @@ describe('IgxToast', () => {
4646
expect(domToast.id).toBe('customToast');
4747
});
4848

49-
it('should be able to set custom positionSettings', () => {
50-
const defaultPositionSettings = toast.positionSettings;
51-
expect(defaultPositionSettings.horizontalDirection).toBe(-0.5);
52-
expect(defaultPositionSettings.verticalDirection).toBe(0);
53-
const newPositionSettings: PositionSettings = {
54-
openAnimation: useAnimation(slideInLeft, { params: { duration: '1000ms' } }),
55-
closeAnimation: useAnimation(slideInRight, { params: { duration: '1000ms' } }),
56-
horizontalDirection: HorizontalAlignment.Center,
57-
verticalDirection: VerticalAlignment.Middle,
58-
horizontalStartPoint: HorizontalAlignment.Center,
59-
verticalStartPoint: VerticalAlignment.Middle,
60-
minSize: { height: 100, width: 100 }
61-
};
62-
toast.positionSettings = newPositionSettings;
49+
it('should properly change verical position', () => {
50+
expect(toast.position).toBe('bottom');
51+
expect(toast.positionSettings.verticalDirection).toBe(0);
52+
53+
toast.position = 'top';
54+
fixture.detectChanges();
55+
expect(toast.positionSettings.verticalDirection).toBe(-1);
56+
});
57+
});
58+
59+
describe('IgxToast with positionSettings', () => {
60+
configureTestSuite();
61+
beforeAll(waitForAsync(() => {
62+
TestBed.configureTestingModule({
63+
declarations: [ToastPositionSettingsTestComponent],
64+
imports: [NoopAnimationsModule, IgxToastModule]
65+
}).compileComponents();
66+
}));
67+
68+
let fixture: ComponentFixture<ToastPositionSettingsTestComponent>;
69+
let toast: IgxToastComponent;
70+
71+
beforeEach(() => {
72+
fixture = TestBed.createComponent(ToastPositionSettingsTestComponent);
73+
toast = fixture.componentInstance.toast;
74+
fixture.detectChanges();
75+
});
76+
77+
it('should be able to change positionSettings', () => {
78+
expect(toast.positionSettings.horizontalDirection).toBe(-1);
79+
expect(toast.positionSettings.verticalDirection).toBe(-0.5);
80+
toast.positionSettings = fixture.componentInstance.secondPositionSettings;
81+
fixture.detectChanges();
82+
expect(toast.positionSettings.horizontalDirection).toBe(-0.5);
83+
expect(toast.positionSettings.verticalDirection).toBe(-0.5);
84+
});
85+
86+
it('positionSettings passed in the open method should be applied', () => {
87+
const positions = fixture.componentInstance.secondPositionSettings;
88+
toast.open(undefined, positions);
6389
fixture.detectChanges();
64-
const customPositionSettings = toast.positionSettings;
65-
expect(customPositionSettings.horizontalDirection).toBe(-0.5);
66-
expect(customPositionSettings.verticalDirection).toBe(-0.5);
67-
expect(customPositionSettings.openAnimation.options.params).toEqual({duration: '1000ms'});
68-
expect(customPositionSettings.minSize).toEqual({height: 100, width: 100});
90+
expect(toast.positionSettings.horizontalDirection).toBe(-0.5);
91+
expect(toast.positionSettings.verticalDirection).toBe(-0.5);
6992
});
7093
});
7194

@@ -77,3 +100,23 @@ class ToastInitializeTestComponent {
77100
public toast: IgxToastComponent;
78101
}
79102

103+
@Component({
104+
template: `<igx-toast #toast [positionSettings]="firstPositionSettings"></igx-toast>`,
105+
})
106+
class ToastPositionSettingsTestComponent {
107+
@ViewChild(IgxToastComponent, { static: true })
108+
public toast: IgxToastComponent;
109+
public firstPositionSettings: PositionSettings = {
110+
horizontalDirection: HorizontalAlignment.Left,
111+
verticalDirection: VerticalAlignment.Middle,
112+
horizontalStartPoint: HorizontalAlignment.Left,
113+
verticalStartPoint: VerticalAlignment.Middle
114+
};
115+
public secondPositionSettings: PositionSettings = {
116+
horizontalDirection: HorizontalAlignment.Center,
117+
verticalDirection: VerticalAlignment.Middle,
118+
horizontalStartPoint: HorizontalAlignment.Center,
119+
verticalStartPoint: VerticalAlignment.Middle
120+
};
121+
}
122+

projects/igniteui-angular/src/lib/toast/toast.component.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import {
88
Inject,
99
Input,
1010
NgModule,
11+
OnChanges,
1112
OnInit,
1213
Optional,
1314
Output,
15+
SimpleChanges
1416
} from '@angular/core';
1517
import { takeUntil } from 'rxjs/operators';
1618
import { IgxNavigationService } from '../core/navigation';
@@ -26,6 +28,7 @@ import { IgxNotificationsDirective } from '../directives/notification/notificati
2628
import { ToggleViewEventArgs } from '../directives/toggle/toggle.directive';
2729
import { useAnimation } from '@angular/animations';
2830
import { fadeIn, fadeOut } from '../animations/fade';
31+
import { DeprecateProperty } from '../core/deprecateDecorators';
2932

3033
let NEXT_ID = 0;
3134

@@ -63,8 +66,7 @@ export type IgxToastPosition = (typeof IgxToastPosition)[keyof typeof IgxToastPo
6366
selector: 'igx-toast',
6467
templateUrl: 'toast.component.html'
6568
})
66-
export class IgxToastComponent extends IgxNotificationsDirective
67-
implements OnInit {
69+
export class IgxToastComponent extends IgxNotificationsDirective implements OnInit, OnChanges {
6870
/**
6971
* @hidden
7072
*/
@@ -119,8 +121,18 @@ export class IgxToastComponent extends IgxNotificationsDirective
119121
*
120122
* @memberof IgxToastComponent
121123
*/
124+
@DeprecateProperty('`position` is deprecated. We suggest using `positionSettings` property instead.')
122125
@Input()
123-
public position: IgxToastPosition = 'bottom';
126+
public get position(): IgxToastPosition {
127+
return this._position;
128+
}
129+
130+
public set position(position: IgxToastPosition) {
131+
if (position) {
132+
this._position = position;
133+
this._positionSettings.verticalDirection = this.calculatePosition();
134+
}
135+
}
124136

125137
/**
126138
* Get the position and animation settings used by the toast.
@@ -158,17 +170,13 @@ export class IgxToastComponent extends IgxNotificationsDirective
158170
this._positionSettings = settings;
159171
}
160172

173+
private _position: IgxToastPosition = 'bottom';
161174
private _positionSettings: PositionSettings = {
162175
horizontalDirection: HorizontalAlignment.Center,
163-
verticalDirection:
164-
this.position === 'bottom'
165-
? VerticalAlignment.Bottom
166-
: this.position === 'middle'
167-
? VerticalAlignment.Middle
168-
: VerticalAlignment.Top,
176+
verticalDirection: VerticalAlignment.Bottom,
169177
openAnimation: useAnimation(fadeIn),
170178
closeAnimation: useAnimation(fadeOut),
171-
};
179+
};
172180

173181
/**
174182
* Gets the nativeElement of the toast.
@@ -199,11 +207,13 @@ export class IgxToastComponent extends IgxNotificationsDirective
199207
* this.toast.open();
200208
* ```
201209
*/
202-
public open(message?: string) {
210+
public open(message?: string, settings?: PositionSettings) {
203211
if (message !== undefined) {
204212
this.textMessage = message;
205213
}
206-
214+
if (settings !== undefined) {
215+
this.positionSettings = settings;
216+
}
207217
this.strategy = new GlobalPositionStrategy(this.positionSettings);
208218
super.open();
209219
}
@@ -237,6 +247,20 @@ export class IgxToastComponent extends IgxNotificationsDirective
237247
this.isVisibleChange.emit(closedEventArgs);
238248
});
239249
}
250+
251+
public ngOnChanges(changes: SimpleChanges) {
252+
if (changes['position'] && this._positionSettings) {
253+
this._positionSettings.verticalDirection = this.calculatePosition();
254+
}
255+
}
256+
257+
private calculatePosition() {
258+
return this.position === 'bottom'
259+
? VerticalAlignment.Bottom
260+
: this.position === 'middle'
261+
? VerticalAlignment.Middle
262+
: VerticalAlignment.Top;
263+
}
240264
}
241265

242266
/**

0 commit comments

Comments
 (0)