Skip to content

Commit 3fcec91

Browse files
authored
feat(module:popover,popconfirm,tooltip): overlayClassName supports space delimited classes string (#8972)
1 parent aa1ade9 commit 3fcec91

File tree

5 files changed

+83
-5
lines changed

5 files changed

+83
-5
lines changed

components/popconfirm/popconfirm.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,29 @@ describe('NzPopconfirm', () => {
241241
fixture.detectChanges();
242242
expect(overlayContainerElement.children[0].classList).toContain('cdk-overlay-backdrop');
243243
}));
244+
245+
it('should change overlayClass when the nzPopconfirmOverlayClassName is changed', fakeAsync(() => {
246+
const triggerElement = component.stringTemplate.nativeElement;
247+
248+
dispatchMouseEvent(triggerElement, 'click');
249+
waitingForTooltipToggling();
250+
251+
component.class = 'testClass2';
252+
fixture.detectChanges();
253+
254+
expect(overlayContainerElement.querySelector<HTMLElement>('.testClass')).toBeNull();
255+
expect(overlayContainerElement.querySelector<HTMLElement>('.testClass2')).not.toBeNull();
256+
}));
257+
258+
it('should nzPopconfirmOverlayClassName support classes listed in the string (space delimited)', fakeAsync(() => {
259+
const triggerElement = component.stringTemplate.nativeElement;
260+
component.class = 'testClass1 testClass2';
261+
262+
dispatchMouseEvent(triggerElement, 'click');
263+
waitingForTooltipToggling();
264+
265+
expect(overlayContainerElement.querySelector('.testClass1.testClass2')).not.toBeNull();
266+
}));
244267
});
245268

246269
@Component({
@@ -259,6 +282,7 @@ describe('NzPopconfirm', () => {
259282
[nzBeforeConfirm]="beforeConfirm"
260283
[nzPopconfirmShowArrow]="nzPopconfirmShowArrow"
261284
[nzPopconfirmBackdrop]="nzPopconfirmBackdrop"
285+
[nzPopconfirmOverlayClassName]="class"
262286
(nzOnConfirm)="confirm()"
263287
(nzOnCancel)="cancel()"
264288
>
@@ -297,4 +321,5 @@ export class NzPopconfirmTestNewComponent {
297321
@ViewChild('iconTemplate', { static: false }) iconTemplate!: ElementRef;
298322

299323
visible = false;
324+
class = 'testClass';
300325
}

components/popover/popover.spec.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe('NzPopover', () => {
105105
expect(overlayContainerElement.children[0].classList).toContain('cdk-overlay-backdrop');
106106
}));
107107

108-
it('nzPopoverOverlayClickable: false is to prohibit hiding', fakeAsync(() => {
108+
it('should prohibit hiding popover when nzPopoverOverlayClickable is false', fakeAsync(() => {
109109
const triggerElement = component.hideTemplate.nativeElement;
110110

111111
dispatchMouseEvent(triggerElement, 'click');
@@ -116,12 +116,42 @@ describe('NzPopover', () => {
116116
waitingForTooltipToggling();
117117
expect(overlayContainerElement.textContent).toContain('content-string');
118118
}));
119+
120+
it('should change overlayClass when the nzPopoverOverlayClassName is changed', fakeAsync(() => {
121+
const triggerElement = component.stringPopover.nativeElement;
122+
123+
dispatchMouseEvent(triggerElement, 'mouseenter');
124+
waitingForTooltipToggling();
125+
126+
component.class = 'testClass2';
127+
fixture.detectChanges();
128+
129+
expect(overlayContainerElement.querySelector<HTMLElement>('.testClass')).toBeNull();
130+
expect(overlayContainerElement.querySelector<HTMLElement>('.testClass2')).not.toBeNull();
131+
}));
132+
133+
it('should nzPopoverOverlayClassName support classes listed in the string (space delimited)', fakeAsync(() => {
134+
const triggerElement = component.stringPopover.nativeElement;
135+
component.class = 'testClass1 testClass2';
136+
137+
dispatchMouseEvent(triggerElement, 'mouseenter');
138+
waitingForTooltipToggling();
139+
140+
expect(overlayContainerElement.querySelector('.testClass1.testClass2')).not.toBeNull();
141+
}));
119142
});
120143

121144
@Component({
122145
imports: [NzPopoverModule],
123146
template: `
124-
<a #stringPopover nz-popover nzPopoverTitle="title-string" nzPopoverContent="content-string">Show</a>
147+
<a
148+
#stringPopover
149+
nz-popover
150+
nzPopoverTitle="title-string"
151+
nzPopoverContent="content-string"
152+
[nzPopoverOverlayClassName]="class"
153+
>Show</a
154+
>
125155
126156
<a #templatePopover nz-popover [nzPopoverTitle]="templateTitle" [nzPopoverContent]="templateContent">Show</a>
127157
@@ -172,4 +202,5 @@ export class NzPopoverTestComponent {
172202

173203
content = 'content';
174204
visible = false;
205+
class = 'testClass';
175206
}

components/tooltip/base.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -494,11 +494,21 @@ export abstract class NzTooltipBaseComponent implements OnDestroy, OnInit {
494494

495495
protected updateStyles(): void {
496496
this._classMap = {
497-
[this.nzOverlayClassName]: true,
497+
...this.transformClassListToMap(this.nzOverlayClassName),
498498
[`${this._prefix}-placement-${this.preferredPlacement}`]: true
499499
};
500500
}
501501

502+
protected transformClassListToMap(klass: string): Record<string, boolean> {
503+
const result: Record<string, boolean> = {};
504+
/**
505+
* @see https://github.com/angular/angular/blob/f6e97763cfab9fa2bea6e6b1303b64f1b499c3ef/packages/common/src/directives/ng_class.ts#L92
506+
*/
507+
const classes = klass !== null ? klass.split(/\s+/) : [];
508+
classes.forEach(className => (result[className] = true));
509+
return result;
510+
}
511+
502512
/**
503513
* Empty component cannot be opened.
504514
*/

components/tooltip/tooltip.spec.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ describe('nz-tooltip', () => {
203203
expect(overlayContainerElement.querySelector<HTMLElement>('.ant-tooltip')!.style.color).toBe('rgb(0, 0, 0)');
204204
}));
205205

206-
it('should change overlayClass when the overlayClass is changed', fakeAsync(() => {
206+
it('should change overlayClass when the nzTooltipOverlayClassName is changed', fakeAsync(() => {
207207
const triggerElement = component.titleString.nativeElement;
208208

209209
dispatchMouseEvent(triggerElement, 'mouseenter');
@@ -212,9 +212,21 @@ describe('nz-tooltip', () => {
212212
component.class = 'testClass2';
213213
fixture.detectChanges();
214214

215+
expect(overlayContainerElement.querySelector<HTMLElement>('.testClass')).toBeNull();
215216
expect(overlayContainerElement.querySelector<HTMLElement>('.testClass2')).not.toBeNull();
216217
}));
217218

219+
it('should nzTooltipOverlayClassName support classes listed in the string (space delimited)', fakeAsync(() => {
220+
const triggerElement = component.titleString.nativeElement;
221+
component.class = 'testClass1 testClass2';
222+
fixture.detectChanges();
223+
224+
dispatchMouseEvent(triggerElement, 'mouseenter');
225+
waitingForTooltipToggling();
226+
227+
expect(overlayContainerElement.querySelector<HTMLElement>('.testClass1.testClass2')).not.toBeNull();
228+
}));
229+
218230
it('should hide when the title is changed to null', fakeAsync(() => {
219231
const title = 'title-string';
220232
const triggerElement = component.titleString.nativeElement;

components/tooltip/tooltip.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class NzToolTipComponent extends NzTooltipBaseComponent {
133133
const isColorPreset = this.nzColor && isPresetColor(this.nzColor);
134134

135135
this._classMap = {
136-
[this.nzOverlayClassName]: true,
136+
...this.transformClassListToMap(this.nzOverlayClassName),
137137
[`${this._prefix}-placement-${this.preferredPlacement}`]: true,
138138
[`${this._prefix}-${this.nzColor}`]: isColorPreset
139139
};

0 commit comments

Comments
 (0)