Skip to content

Commit b7d852f

Browse files
authored
Merge pull request #10404 from IgniteUI/dTsvetkov/toast-position-not-working-10305-12.2.x
2 parents f21b2f6 + f357773 commit b7d852f

File tree

3 files changed

+100
-32
lines changed

3 files changed

+100
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ All notable changes for each version of this project will be documented in this
1212

1313
### General
1414
- **Breaking Change** - `IgxPercentSummaryOperand` and `IgxCurrencySummaryOperand` have been removed and `IgxNumberSummaryOperand` should be used instead. If you have used the percent or currency summary operands to extend a custom summary operand from them, then change the custom operand to extend from the number summary operand.
15+
- `IgxToastComponent`
16+
- **Deprecated** - The `position` input property has been deprecated. Use `positionSettings` input instead.
1517

1618
## 12.2.1
1719

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

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
IgxToastModule,
1212
} from './toast.component';
1313
import { configureTestSuite } from '../test-utils/configure-suite';
14-
import { useAnimation } from '@angular/animations';
15-
import { HorizontalAlignment, PositionSettings, slideInLeft, slideInRight, VerticalAlignment } from 'igniteui-angular';
14+
import { HorizontalAlignment, PositionSettings, VerticalAlignment } from 'igniteui-angular';
1615

1716
describe('IgxToast', () => {
1817
configureTestSuite();
@@ -46,26 +45,49 @@ describe('IgxToast', () => {
4645
expect(domToast.id).toBe('customToast');
4746
});
4847

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;
48+
it('should properly change verical position', () => {
49+
expect(toast.position).toBe('bottom');
50+
expect(toast.positionSettings.verticalDirection).toBe(0);
51+
52+
toast.position = 'top';
53+
fixture.detectChanges();
54+
expect(toast.positionSettings.verticalDirection).toBe(-1);
55+
});
56+
});
57+
58+
describe('IgxToast with positionSettings', () => {
59+
configureTestSuite();
60+
beforeAll(waitForAsync(() => {
61+
TestBed.configureTestingModule({
62+
declarations: [ToastPositionSettingsTestComponent],
63+
imports: [NoopAnimationsModule, IgxToastModule]
64+
}).compileComponents();
65+
}));
66+
67+
let fixture: ComponentFixture<ToastPositionSettingsTestComponent>;
68+
let toast: IgxToastComponent;
69+
70+
beforeEach(() => {
71+
fixture = TestBed.createComponent(ToastPositionSettingsTestComponent);
72+
toast = fixture.componentInstance.toast;
73+
fixture.detectChanges();
74+
});
75+
76+
it('should be able to change positionSettings', () => {
77+
expect(toast.positionSettings.horizontalDirection).toBe(-1);
78+
expect(toast.positionSettings.verticalDirection).toBe(-0.5);
79+
toast.positionSettings = fixture.componentInstance.secondPositionSettings;
80+
fixture.detectChanges();
81+
expect(toast.positionSettings.horizontalDirection).toBe(-0.5);
82+
expect(toast.positionSettings.verticalDirection).toBe(-0.5);
83+
});
84+
85+
it('positionSettings passed in the open method should be applied', () => {
86+
const positions = fixture.componentInstance.secondPositionSettings;
87+
toast.open(undefined, positions);
6388
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});
89+
expect(toast.positionSettings.horizontalDirection).toBe(-0.5);
90+
expect(toast.positionSettings.verticalDirection).toBe(-0.5);
6991
});
7092
});
7193

@@ -77,3 +99,23 @@ class ToastInitializeTestComponent {
7799
public toast: IgxToastComponent;
78100
}
79101

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

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

Lines changed: 35 additions & 11 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,14 +170,10 @@ 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),
171179
};
@@ -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)