diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b4c285371e..9c2b3d7f94a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ All notable changes for each version of this project will be documented in this - Replacing the data and the columns of a percentage-height grid no longer runs a redundant sizing pass (#17792). - `IgxHierarchicalGridComponent` - Replacing the root columns with the same number of columns no longer leaves the old headers and column widths on screen when `summaryCalculationMode` is `childLevelsOnly`. +- `IgxInputGroupComponent` + - After a runtime theme change through `THEME_TOKEN`, an input group in an OnPush view, such as a grid cell editor or a combo in an OnPush component, updates its theme classes with its template. Previously it kept the classes of the theme it was first rendered in (#17991). ## 22.3.0 diff --git a/projects/igniteui-angular/input-group/src/input-group/input-group.component.spec.ts b/projects/igniteui-angular/input-group/src/input-group/input-group.component.spec.ts index 3e57e7e7f40..575f5d61afd 100644 --- a/projects/igniteui-angular/input-group/src/input-group/input-group.component.spec.ts +++ b/projects/igniteui-angular/input-group/src/input-group/input-group.component.spec.ts @@ -1,4 +1,4 @@ -import { Component, ViewChild, ElementRef, inject, ChangeDetectionStrategy, createComponent, EnvironmentInjector } from '@angular/core'; +import { Component, ViewChild, ElementRef, inject, ChangeDetectionStrategy, createComponent, EnvironmentInjector, AfterViewInit } from '@angular/core'; import { NgTemplateOutlet } from '@angular/common'; import { TestBed } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; @@ -7,7 +7,7 @@ import { UIInteractions } from '../../../test-utils/ui-interactions.spec'; import { IgxHintDirective, IgxInputDirective, IgxLabelDirective, IgxPrefixDirective, IgxSuffixDirective } from '../public_api'; import { IgxCardComponent, IgxCardContentDirective } from 'igniteui-angular/card'; import { IGX_INPUT_GROUP_TYPE, IgxInputGroupType } from './inputGroupType'; -import { InputResourceStringsEN, changei18n, THEME_TOKEN } from 'igniteui-angular/core'; +import { InputResourceStringsEN, changei18n, IgxTheme, THEME_TOKEN, ThemeToken } from 'igniteui-angular/core'; import { config } from 'rxjs'; import { describe, it, expect, beforeEach, vi } from 'vitest'; @@ -15,6 +15,7 @@ const INPUT_GROUP_CSS_CLASS = 'igx-input-group'; const INPUT_GROUP_BOX_CSS_CLASS = 'igx-input-group--box'; const INPUT_GROUP_BORDER_CSS_CLASS = 'igx-input-group--border'; const INPUT_GROUP_SEARCH_CSS_CLASS = 'igx-input-group--search'; +const INPUT_GROUP_BOOTSTRAP_CSS_CLASS = 'igx-input-group--bootstrap'; describe('IgxInputGroup', () => { beforeEach(async () => { @@ -29,7 +30,8 @@ describe('IgxInputGroup', () => { InputGroupDisabledByDefaultComponent, InputGroupDisabledWithoutValueComponent, InputGroupNestedThemeScopeComponent, - InputGroupFormRowComponent + InputGroupFormRowComponent, + InputGroupOnPushThemeComponent ] }).compileComponents(); }); @@ -281,6 +283,40 @@ describe('IgxInputGroup', () => { } }); + describe('IgxInputGroup runtime theme change in an OnPush view (#17991)', () => { + // The theme classes are on the host and the notch is in the template, both must show the material theme + const expectMaterialBorder = (fixture) => { + const inputGroupElement = fixture.debugElement.query(By.css('igx-input-group')).nativeElement; + expect(inputGroupElement.classList.contains(INPUT_GROUP_BOOTSTRAP_CSS_CLASS)).toBe(false); + expect(inputGroupElement.classList.contains(INPUT_GROUP_BORDER_CSS_CLASS)).toBe(true); + expect(inputGroupElement.querySelector('.igx-input-group__notch')).not.toBeNull(); + }; + + it('should update its theme classes when THEME_TOKEN changes', async () => { + const fixture = TestBed.createComponent(InputGroupOnPushThemeComponent); + fixture.detectChanges(); + await fixture.whenStable(); + + const inputGroupElement = fixture.debugElement.query(By.css('igx-input-group')).nativeElement; + expect(inputGroupElement.classList.contains(INPUT_GROUP_BOOTSTRAP_CSS_CLASS)).toBe(true); + + fixture.componentInstance.themeToken.set('material'); + fixture.detectChanges(); + await fixture.whenStable(); + + expectMaterialBorder(fixture); + }); + + it('should apply a theme that an OnPush parent sets in ngAfterViewInit', async () => { + const fixture = TestBed.createComponent(InputGroupOnPushThemeComponent); + fixture.componentInstance.savedTheme = 'material'; + fixture.detectChanges(); + await fixture.whenStable(); + + expectMaterialBorder(fixture); + }); + }); + describe('Resource Strings', () => { it('should return full resource strings when partial resourceStrings are set', () => { const fix = TestBed.createComponent(InputGroupComponent); @@ -386,6 +422,24 @@ class InputGroupNestedThemeScopeComponent { @ViewChild('materialGroup', { static: true }) public materialGroup: IgxInputGroupComponent; } +@Component({ + template: ``, + providers: [{ provide: THEME_TOKEN, useFactory: () => new ThemeToken('bootstrap') }], + changeDetection: ChangeDetectionStrategy.OnPush, + imports: [IgxInputGroupComponent, IgxInputDirective] +}) +class InputGroupOnPushThemeComponent implements AfterViewInit { + public themeToken = inject(THEME_TOKEN); + /** A theme to restore after the first render, e.g. one the user saved */ + public savedTheme?: IgxTheme; + + public ngAfterViewInit() { + if (this.savedTheme) { + this.themeToken.set(this.savedTheme); + } + } +} + @Component({ template: ` PREFIX diff --git a/projects/igniteui-angular/input-group/src/input-group/input-group.component.ts b/projects/igniteui-angular/input-group/src/input-group/input-group.component.ts index 661d74c5270..dfcbd18f2ae 100644 --- a/projects/igniteui-angular/input-group/src/input-group/input-group.component.ts +++ b/projects/igniteui-angular/input-group/src/input-group/input-group.component.ts @@ -15,6 +15,7 @@ import { AfterContentChecked, ChangeDetectionStrategy, ViewEncapsulation, + signal, } from '@angular/core'; import { IInputResourceStrings, InputResourceStringsEN } from 'igniteui-angular/core'; import { getComponentTheme } from 'igniteui-angular/core'; @@ -148,7 +149,7 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh private _destroyRef = inject(DestroyRef); private _type: IgxInputGroupType | null = null; private _filled = false; - private _theme: IgxTheme; + private readonly _theme = signal(this.themeToken.theme); private _resourceStrings: IInputResourceStrings | null = null; private _customResourceStrings: IInputResourceStrings = null!; private _defaultResourceStrings = getCurrentResourceStrings(InputResourceStringsEN); @@ -228,7 +229,7 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh */ @Input() public set theme(value: IgxTheme) { - this._theme = value; + this._theme.set(value); } /** @@ -242,14 +243,13 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh * } */ public get theme(): IgxTheme { - return this._theme; + return this._theme(); } constructor() { - this._theme = this.themeToken.theme; const themeChange = this.themeToken.onChange((theme) => { - if (this._theme !== theme) { - this._theme = theme; + if (this._theme() !== theme) { + this._theme.set(theme); this.cdr.detectChanges(); } }); @@ -342,7 +342,7 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh public get hasBorder() { return ( (this.type === 'line' || this.type === 'box') && - this._theme === 'material' + this._theme() === 'material' ); } @@ -358,13 +358,13 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh */ @HostBinding('class.igx-input-group--line') public get isTypeLine(): boolean { - return this.type === 'line' && this._theme === 'material'; + return this.type === 'line' && this._theme() === 'material'; } /** @hidden @internal */ @HostBinding('class.igx-input-group--base') public get isNotBorder(): boolean { - return this.type !== 'border' && this._theme === 'material'; + return this.type !== 'border' && this._theme() === 'material'; } /** @@ -379,7 +379,7 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh */ @HostBinding('class.igx-input-group--box') public get isTypeBox() { - return this.type === 'box' && this._theme === 'material'; + return this.type === 'box' && this._theme() === 'material'; } /** @hidden @internal */ @@ -434,7 +434,7 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh */ @HostBinding('class.igx-input-group--border') public get isTypeBorder() { - return this.type === 'border' && this._theme === 'material'; + return this.type === 'border' && this._theme() === 'material'; } /** @@ -449,7 +449,7 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh */ @HostBinding('class.igx-input-group--fluent') public get isTypeFluent() { - return this._theme === 'fluent'; + return this._theme() === 'fluent'; } /** @@ -464,7 +464,7 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh */ @HostBinding('class.igx-input-group--bootstrap') public get isTypeBootstrap() { - return this._theme === 'bootstrap'; + return this._theme() === 'bootstrap'; } /** @@ -479,7 +479,7 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh */ @HostBinding('class.igx-input-group--indigo') public get isTypeIndigo() { - return this._theme === 'indigo'; + return this._theme() === 'indigo'; } /** @@ -513,7 +513,7 @@ export class IgxInputGroupComponent implements IgxInputGroupBase, AfterContentCh if (!this.themeToken.preferToken) { const theme = getComponentTheme(this.element.nativeElement); - if (theme && theme !== this._theme) { + if (theme && theme !== this._theme()) { this.theme = theme; this.cdr.markForCheck(); }