Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #16133 - pBadge | Add missing badgeStyle & badgeStyleClass to d… #16134

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/app/components/badge/badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,21 @@ export class BadgeDirective implements OnChanges, AfterViewInit {
* @group Props
*/
@Input() public value: string | number;
/**
* Inline style of the element.
* @group Props
*/
@Input() badgeStyle: { [klass: string]: any } | null | undefined;
/**
* Class of the element.
* @group Props
*/
@Input() badgeStyleClass: string;

private id!: string;

badgeEl: HTMLElement;

private get activeElement(): HTMLElement {
return this.el.nativeElement.nodeName.indexOf('-') != -1 ? this.el.nativeElement.firstChild : this.el.nativeElement;
}
Expand All @@ -60,7 +72,7 @@ export class BadgeDirective implements OnChanges, AfterViewInit {

constructor(@Inject(DOCUMENT) private document: Document, public el: ElementRef, private renderer: Renderer2) {}

public ngOnChanges({ value, size, severity, disabled }: SimpleChanges): void {
public ngOnChanges({ value, size, severity, disabled, badgeStyle, badgeStyleClass }: SimpleChanges): void {
if (disabled) {
this.toggleDisableState();
}
Expand All @@ -80,6 +92,10 @@ export class BadgeDirective implements OnChanges, AfterViewInit {
if (value) {
this.setValue();
}

if (badgeStyle || badgeStyleClass) {
this.applyStyles();
}
}

public ngAfterViewInit(): void {
Expand Down Expand Up @@ -165,6 +181,19 @@ export class BadgeDirective implements OnChanges, AfterViewInit {
this.setValue(badge);
DomHandler.addClass(el, 'p-overlay-badge');
this.renderer.appendChild(el, badge);
this.badgeEl = badge;
this.applyStyles();
}

private applyStyles(): void {
if (this.badgeEl && this.badgeStyle && typeof this.badgeStyle === 'object') {
for (const [key, value] of Object.entries(this.badgeStyle)) {
this.renderer.setStyle(this.badgeEl, key, value);
}
}
if (this.badgeEl && this.badgeStyleClass) {
this.badgeEl.classList.add(...this.badgeStyleClass.split(' '));
}
}

private setSeverity(oldSeverity?: 'success' | 'info' | 'warning' | 'danger' | null, element?: HTMLElement): void {
Expand Down
Loading