diff --git a/README.md b/README.md index 8a270679..d028f627 100644 --- a/README.md +++ b/README.md @@ -264,4 +264,4 @@ Copyright 2026 creativeLabs Łukasz Holeczek. Code released under the [MIT Licen [npm-coreui-angular-badge-next]: https://img.shields.io/npm/v/@coreui/angular/next?style=flat-square&color=red [npm-coreui-angular]: https://www.npmjs.com/package/@coreui/angular [npm-coreui-angular-download]: https://img.shields.io/npm/dm/@coreui/angular.svg?style=flat-square -[angular-badge]: https://img.shields.io/badge/angular-^22.2.0-lightgrey.svg?style=flat-square&logo=angular +[angular-badge]: https://img.shields.io/badge/angular-^22.3.0-lightgrey.svg?style=flat-square&logo=angular diff --git a/projects/coreui-angular-chartjs/README.md b/projects/coreui-angular-chartjs/README.md index 50e1b3a9..66d9b998 100644 --- a/projects/coreui-angular-chartjs/README.md +++ b/projects/coreui-angular-chartjs/README.md @@ -46,7 +46,7 @@ Featured CoreUI for Angular libraries: [npm-badge-next]: https://img.shields.io/npm/v/@coreui/angular-chartjs/next?style=flat-square&color=red [npm]: https://www.npmjs.com/package/@coreui/angular-chartjs [npm-download]: https://img.shields.io/npm/dm/@coreui/angular-chartjs.svg?style=flat-square -[angular-badge]: https://img.shields.io/badge/angular-^22.2.0-lightgrey.svg?style=flat-square&logo=angular +[angular-badge]: https://img.shields.io/badge/angular-^22.3.0-lightgrey.svg?style=flat-square&logo=angular ##### install: diff --git a/projects/coreui-angular-chartjs/package.json b/projects/coreui-angular-chartjs/package.json index 13b45f7f..106d368a 100644 --- a/projects/coreui-angular-chartjs/package.json +++ b/projects/coreui-angular-chartjs/package.json @@ -25,7 +25,7 @@ "url": "https://github.com/coreui/coreui-angular/issues" }, "peerDependencies": { - "@angular/core": "^22.2.0", + "@angular/core": "^22.3.0", "@coreui/chartjs": "^4.2.0", "chart.js": "^4.5.1" }, diff --git a/projects/coreui-angular/README.md b/projects/coreui-angular/README.md index 8a270679..d028f627 100644 --- a/projects/coreui-angular/README.md +++ b/projects/coreui-angular/README.md @@ -264,4 +264,4 @@ Copyright 2026 creativeLabs Łukasz Holeczek. Code released under the [MIT Licen [npm-coreui-angular-badge-next]: https://img.shields.io/npm/v/@coreui/angular/next?style=flat-square&color=red [npm-coreui-angular]: https://www.npmjs.com/package/@coreui/angular [npm-coreui-angular-download]: https://img.shields.io/npm/dm/@coreui/angular.svg?style=flat-square -[angular-badge]: https://img.shields.io/badge/angular-^22.2.0-lightgrey.svg?style=flat-square&logo=angular +[angular-badge]: https://img.shields.io/badge/angular-^22.3.0-lightgrey.svg?style=flat-square&logo=angular diff --git a/projects/coreui-angular/package.json b/projects/coreui-angular/package.json index b38bf118..81cdfe72 100644 --- a/projects/coreui-angular/package.json +++ b/projects/coreui-angular/package.json @@ -23,11 +23,12 @@ }, "sideEffects": false, "peerDependencies": { - "@angular/aria": "^22.2.0", - "@angular/cdk": "^22.2.0", - "@angular/common": "^22.2.0", - "@angular/core": "^22.2.0", - "@angular/router": "^22.2.0", + "@angular/aria": "^22.3.0", + "@angular/cdk": "^22.3.0", + "@angular/common": "^22.3.0", + "@angular/core": "^22.3.0", + "@angular/forms": "^22.3.0", + "@angular/router": "^22.3.0", "@coreui/coreui": "^5.9.1", "@coreui/icons-angular": "~5.7.43", "rxjs": "^7.8.2" diff --git a/projects/coreui-angular/src/lib/chip-input/chip-input.component.ts b/projects/coreui-angular/src/lib/chip-input/chip-input.component.ts index aa04f460..92a8cbf0 100644 --- a/projects/coreui-angular/src/lib/chip-input/chip-input.component.ts +++ b/projects/coreui-angular/src/lib/chip-input/chip-input.component.ts @@ -14,7 +14,7 @@ import { viewChild } from '@angular/core'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; -import { AsyncValidator, NG_ASYNC_VALIDATORS, NG_VALIDATORS, NgModel, Validator } from '@angular/forms'; +import { NgModel } from '@angular/forms'; import { take } from 'rxjs'; import { ChipComponent } from '../chip/chip.component'; import { ChipSetRef } from '../chip/chip-set-ref'; @@ -45,8 +45,6 @@ import { UIDService } from '../services/uid.service'; export class ChipInputComponent implements AfterContentInit { readonly #elementRef = inject>(ElementRef); readonly #ngModel = inject(NgModel, { self: true, optional: true }); - readonly #asyncValidators = inject<(AsyncValidator | unknown)[]>(NG_ASYNC_VALIDATORS, { self: true, optional: true }); - readonly #validators = inject<(Validator | unknown)[]>(NG_VALIDATORS, { self: true, optional: true }); readonly #rtl = inject(RtlService); readonly #service = inject(ChipSetService); @@ -305,12 +303,10 @@ export class ChipInputComponent implements AfterContentInit { focusFallback: () => this.focus(), removeRequested: (value) => this.#removeChip(value) }); - this.#attachValidators(); this.#ngModel?.control.valueChanges.pipe(take(1), takeUntilDestroyed()).subscribe(() => this.#service.resync()); } ngAfterContentInit(): void { - this.#watchValidators(); if (this.projectedChips().length > 0) { console.warn('CoreUI [c-chip] placed inside c-chip-input is ignored - chips come from its value'); } @@ -447,28 +443,6 @@ export class ChipInputComponent implements AfterContentInit { this.#setText(this.#addChips(text.split(separator)).join(separator)); } - #attachValidators(): void { - const control = this.#ngModel?.control; - const validator = this.#ngModel?.validator; - const asyncValidator = this.#ngModel?.asyncValidator; - if (validator && control && !control.hasValidator(validator)) { - control.addValidators(validator); - } - if (asyncValidator && control && !control.hasAsyncValidator(asyncValidator)) { - control.addAsyncValidators(asyncValidator); - } - } - - #watchValidators(): void { - const control = this.#ngModel?.control; - if (!control) { - return; - } - for (const item of [...(this.#validators ?? []), ...(this.#asyncValidators ?? [])]) { - (item as Validator | AsyncValidator).registerOnValidatorChange?.(() => control.updateValueAndValidity()); - } - } - #leave(): void { if (this.createOnBlur()) { this.#createFromText(); diff --git a/projects/coreui-angular/src/lib/chip-input/chip-input.validators.spec.ts b/projects/coreui-angular/src/lib/chip-input/chip-input.validators.spec.ts index b14409a4..385a9d65 100644 --- a/projects/coreui-angular/src/lib/chip-input/chip-input.validators.spec.ts +++ b/projects/coreui-angular/src/lib/chip-input/chip-input.validators.spec.ts @@ -5,8 +5,10 @@ import { AsyncValidator, FormsModule, NG_ASYNC_VALIDATORS, + NG_VALIDATORS, NgModel, - ValidationErrors + ValidationErrors, + Validator } from '@angular/forms'; import { By } from '@angular/platform-browser'; import { AnnouncerService } from '../services/announcer.service'; @@ -30,6 +32,7 @@ class TemplateValidatorsHostComponent { }) class TakenValidatorDirective implements AsyncValidator, OnChanges { readonly cTaken = input(''); + calls = 0; #onChange?: () => void; ngOnChanges(): void { @@ -41,6 +44,7 @@ class TakenValidatorDirective implements AsyncValidator, OnChanges { } validate(control: AbstractControl): Promise { + this.calls++; return Promise.resolve(control.value?.includes(this.cTaken()) ? { taken: true } : null); } } @@ -68,6 +72,27 @@ class FirstRenderHostComponent { tags = ['a']; } +@Directive({ + selector: '[cCounted]', + providers: [{ provide: NG_VALIDATORS, useExisting: CountedValidatorDirective, multi: true }] +}) +class CountedValidatorDirective implements Validator { + calls = 0; + + validate(): ValidationErrors | null { + this.calls++; + return null; + } +} + +@Component({ + imports: [FormsModule, ChipInputComponent, CountedValidatorDirective], + template: `` +}) +class CountedValidatorHostComponent { + tags = ['a', 'b']; +} + describe('ChipInputComponent template validators', () => { beforeEach(() => { vi.spyOn(TestBed.inject(AnnouncerService), 'announce').mockReturnValue(vi.fn()); @@ -114,6 +139,19 @@ describe('ChipInputComponent template validators', () => { expect(fixture.nativeElement.querySelector('form > button').disabled).toBe(true); }); + it('runs a validator written next to ngModel once per value change', async () => { + const fixture = TestBed.createComponent(CountedValidatorHostComponent); + await fixture.whenStable(); + const validator = fixture.debugElement + .query(By.directive(CountedValidatorDirective)) + .injector.get(CountedValidatorDirective); + validator.calls = 0; + + fixture.nativeElement.querySelector('c-chip button.chip-remove').click(); + await fixture.whenStable(); + expect(validator.calls).toBe(1); + }); + it('applies an async validator written next to ngModel and follows its changes', async () => { const fixture = TestBed.createComponent(AsyncValidatorHostComponent); const model = fixture.debugElement.query(By.directive(NgModel)).injector.get(NgModel); @@ -126,4 +164,19 @@ describe('ChipInputComponent template validators', () => { await new Promise((resolve) => setTimeout(resolve)); expect(model.control.errors).toBeNull(); }); + + it('runs an async validator written next to ngModel once per value change', async () => { + const fixture = TestBed.createComponent(AsyncValidatorHostComponent); + await fixture.whenStable(); + await new Promise((resolve) => setTimeout(resolve)); + const validator = fixture.debugElement + .query(By.directive(TakenValidatorDirective)) + .injector.get(TakenValidatorDirective); + validator.calls = 0; + + fixture.nativeElement.querySelector('c-chip button.chip-remove').click(); + await fixture.whenStable(); + await new Promise((resolve) => setTimeout(resolve)); + expect(validator.calls).toBe(1); + }); }); diff --git a/projects/coreui-icons-angular/README.md b/projects/coreui-icons-angular/README.md index 9f01e8f4..b8c9e3ea 100644 --- a/projects/coreui-icons-angular/README.md +++ b/projects/coreui-icons-angular/README.md @@ -51,7 +51,7 @@ Featured CoreUI for Angular libraries: [npm-coreui-icons-angular-v5-ng22]: https://img.shields.io/npm/v/@coreui/icons-angular/v5-ng22?style=flat-square&color=brightgreen [npm-coreui-icons-angular-latest]: https://img.shields.io/npm/v/@coreui/icons-angular/latest?style=flat-square&color=brightgreen [npm-coreui-icons-angular-next]: https://img.shields.io/npm/v/@coreui/icons-angular/next?style=flat-square&color=red -[angular-badge]: https://img.shields.io/badge/angular-^22.2.0-lightgrey.svg?style=flat-square&logo=angular +[angular-badge]: https://img.shields.io/badge/angular-^22.3.0-lightgrey.svg?style=flat-square&logo=angular ## `cIcon` directive diff --git a/projects/coreui-icons-angular/package.json b/projects/coreui-icons-angular/package.json index f0b89b68..65cd5f12 100644 --- a/projects/coreui-icons-angular/package.json +++ b/projects/coreui-icons-angular/package.json @@ -25,9 +25,9 @@ "url": "https://github.com/coreui/coreui-angular/issues" }, "peerDependencies": { - "@angular/common": "^22.2.0", - "@angular/core": "^22.2.0", - "@angular/platform-browser": "^22.2.0" + "@angular/common": "^22.3.0", + "@angular/core": "^22.3.0", + "@angular/platform-browser": "^22.3.0" }, "dependencies": { "tslib": "^2.3.0"