Skip to content

Commit

Permalink
feat(module:popover,popconfirm,tooltip): overlayClassName supports sp…
Browse files Browse the repository at this point in the history
…ace delimited classes string (#8972)
  • Loading branch information
Laffery authored Jan 17, 2025
1 parent aa1ade9 commit 3fcec91
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
25 changes: 25 additions & 0 deletions components/popconfirm/popconfirm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,29 @@ describe('NzPopconfirm', () => {
fixture.detectChanges();
expect(overlayContainerElement.children[0].classList).toContain('cdk-overlay-backdrop');
}));

it('should change overlayClass when the nzPopconfirmOverlayClassName is changed', fakeAsync(() => {
const triggerElement = component.stringTemplate.nativeElement;

dispatchMouseEvent(triggerElement, 'click');
waitingForTooltipToggling();

component.class = 'testClass2';
fixture.detectChanges();

expect(overlayContainerElement.querySelector<HTMLElement>('.testClass')).toBeNull();
expect(overlayContainerElement.querySelector<HTMLElement>('.testClass2')).not.toBeNull();
}));

it('should nzPopconfirmOverlayClassName support classes listed in the string (space delimited)', fakeAsync(() => {
const triggerElement = component.stringTemplate.nativeElement;
component.class = 'testClass1 testClass2';

dispatchMouseEvent(triggerElement, 'click');
waitingForTooltipToggling();

expect(overlayContainerElement.querySelector('.testClass1.testClass2')).not.toBeNull();
}));
});

@Component({
Expand All @@ -259,6 +282,7 @@ describe('NzPopconfirm', () => {
[nzBeforeConfirm]="beforeConfirm"
[nzPopconfirmShowArrow]="nzPopconfirmShowArrow"
[nzPopconfirmBackdrop]="nzPopconfirmBackdrop"
[nzPopconfirmOverlayClassName]="class"
(nzOnConfirm)="confirm()"
(nzOnCancel)="cancel()"
>
Expand Down Expand Up @@ -297,4 +321,5 @@ export class NzPopconfirmTestNewComponent {
@ViewChild('iconTemplate', { static: false }) iconTemplate!: ElementRef;

visible = false;
class = 'testClass';
}
35 changes: 33 additions & 2 deletions components/popover/popover.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('NzPopover', () => {
expect(overlayContainerElement.children[0].classList).toContain('cdk-overlay-backdrop');
}));

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

dispatchMouseEvent(triggerElement, 'click');
Expand All @@ -116,12 +116,42 @@ describe('NzPopover', () => {
waitingForTooltipToggling();
expect(overlayContainerElement.textContent).toContain('content-string');
}));

it('should change overlayClass when the nzPopoverOverlayClassName is changed', fakeAsync(() => {
const triggerElement = component.stringPopover.nativeElement;

dispatchMouseEvent(triggerElement, 'mouseenter');
waitingForTooltipToggling();

component.class = 'testClass2';
fixture.detectChanges();

expect(overlayContainerElement.querySelector<HTMLElement>('.testClass')).toBeNull();
expect(overlayContainerElement.querySelector<HTMLElement>('.testClass2')).not.toBeNull();
}));

it('should nzPopoverOverlayClassName support classes listed in the string (space delimited)', fakeAsync(() => {
const triggerElement = component.stringPopover.nativeElement;
component.class = 'testClass1 testClass2';

dispatchMouseEvent(triggerElement, 'mouseenter');
waitingForTooltipToggling();

expect(overlayContainerElement.querySelector('.testClass1.testClass2')).not.toBeNull();
}));
});

@Component({
imports: [NzPopoverModule],
template: `
<a #stringPopover nz-popover nzPopoverTitle="title-string" nzPopoverContent="content-string">Show</a>
<a
#stringPopover
nz-popover
nzPopoverTitle="title-string"
nzPopoverContent="content-string"
[nzPopoverOverlayClassName]="class"
>Show</a
>
<a #templatePopover nz-popover [nzPopoverTitle]="templateTitle" [nzPopoverContent]="templateContent">Show</a>
Expand Down Expand Up @@ -172,4 +202,5 @@ export class NzPopoverTestComponent {

content = 'content';
visible = false;
class = 'testClass';
}
12 changes: 11 additions & 1 deletion components/tooltip/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,21 @@ export abstract class NzTooltipBaseComponent implements OnDestroy, OnInit {

protected updateStyles(): void {
this._classMap = {
[this.nzOverlayClassName]: true,
...this.transformClassListToMap(this.nzOverlayClassName),
[`${this._prefix}-placement-${this.preferredPlacement}`]: true
};
}

protected transformClassListToMap(klass: string): Record<string, boolean> {
const result: Record<string, boolean> = {};
/**
* @see https://github.com/angular/angular/blob/f6e97763cfab9fa2bea6e6b1303b64f1b499c3ef/packages/common/src/directives/ng_class.ts#L92
*/
const classes = klass !== null ? klass.split(/\s+/) : [];
classes.forEach(className => (result[className] = true));
return result;
}

/**
* Empty component cannot be opened.
*/
Expand Down
14 changes: 13 additions & 1 deletion components/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('nz-tooltip', () => {
expect(overlayContainerElement.querySelector<HTMLElement>('.ant-tooltip')!.style.color).toBe('rgb(0, 0, 0)');
}));

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

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

expect(overlayContainerElement.querySelector<HTMLElement>('.testClass')).toBeNull();
expect(overlayContainerElement.querySelector<HTMLElement>('.testClass2')).not.toBeNull();
}));

it('should nzTooltipOverlayClassName support classes listed in the string (space delimited)', fakeAsync(() => {
const triggerElement = component.titleString.nativeElement;
component.class = 'testClass1 testClass2';
fixture.detectChanges();

dispatchMouseEvent(triggerElement, 'mouseenter');
waitingForTooltipToggling();

expect(overlayContainerElement.querySelector<HTMLElement>('.testClass1.testClass2')).not.toBeNull();
}));

it('should hide when the title is changed to null', fakeAsync(() => {
const title = 'title-string';
const triggerElement = component.titleString.nativeElement;
Expand Down
2 changes: 1 addition & 1 deletion components/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class NzToolTipComponent extends NzTooltipBaseComponent {
const isColorPreset = this.nzColor && isPresetColor(this.nzColor);

this._classMap = {
[this.nzOverlayClassName]: true,
...this.transformClassListToMap(this.nzOverlayClassName),
[`${this._prefix}-placement-${this.preferredPlacement}`]: true,
[`${this._prefix}-${this.nzColor}`]: isColorPreset
};
Expand Down

0 comments on commit 3fcec91

Please sign in to comment.