Skip to content

Commit 34bbb55

Browse files
fix(components): ajusta detecção do estado disable via FormControl
Ajuste na detecção do estado `disable` via `FormControl` para os componentes `po-checkbox`, `po-combo`, `po-datepicker`, `po-select`, `po-switch` e `po-upload`. Fixes DTHFUI-11949
1 parent 3bad8b5 commit 34bbb55

15 files changed

+55
-15
lines changed

projects/ui/src/lib/components/po-field/po-checkbox/po-checkbox-base.component.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ describe('PoCheckboxBaseComponent:', () => {
1414
let component: PoCheckboxBaseComponent;
1515

1616
beforeEach(() => {
17-
component = new PoCheckboxComponent();
17+
const changeDetector: any = { markForCheck: () => {} };
18+
component = new PoCheckboxComponent(changeDetector);
1819
component.propagateChange = (value: any) => {};
1920
});
2021

@@ -115,8 +116,12 @@ describe('PoCheckboxBaseComponent:', () => {
115116

116117
it('setDisabledState: should set `component.disabled` with boolean parameter', () => {
117118
const expectedValue = true;
119+
const markForCheck = spyOn(component['cd'], 'markForCheck');
120+
118121
component.setDisabledState(expectedValue);
122+
119123
expect(component.disabled).toBe(expectedValue);
124+
expect(markForCheck).toHaveBeenCalled();
120125
});
121126

122127
it('changeValue: should call only `change.emit` with `checkboxValue` if propagateChange is `null`', () => {

projects/ui/src/lib/components/po-field/po-checkbox/po-checkbox-base.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Directive, EventEmitter, Input, Output } from '@angular/core';
1+
import { ChangeDetectorRef, Directive, EventEmitter, Input, Output } from '@angular/core';
22
import { ControlValueAccessor } from '@angular/forms';
33

44
import { convertToBoolean, getDefaultSizeFn, uuid, validateSizeFn } from './../../../utils/util';
@@ -196,6 +196,8 @@ export abstract class PoCheckboxBaseComponent implements ControlValueAccessor {
196196
return this._size ?? getDefaultSizeFn(PoCheckboxSize);
197197
}
198198

199+
constructor(private readonly cd: ChangeDetectorRef) {}
200+
199201
changeValue() {
200202
if (this.propagateChange) {
201203
this.propagateChange(this.checkboxValue);
@@ -215,6 +217,7 @@ export abstract class PoCheckboxBaseComponent implements ControlValueAccessor {
215217
// Usada para interceptar os estados de habilitado via forms api
216218
setDisabledState(isDisabled: boolean) {
217219
this.disabled = isDisabled;
220+
this.cd.markForCheck();
218221
}
219222

220223
registerOnChange(fn: any): void {

projects/ui/src/lib/components/po-field/po-checkbox/po-checkbox.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { PoCheckboxBaseComponent } from './po-checkbox-base.component';
4949
standalone: false
5050
})
5151
export class PoCheckboxComponent extends PoCheckboxBaseComponent implements AfterViewInit {
52-
private changeDetector = inject(ChangeDetectorRef);
52+
private readonly changeDetector: ChangeDetectorRef;
5353

5454
private _iconToken: { [key: string]: string };
5555

@@ -60,7 +60,9 @@ export class PoCheckboxComponent extends PoCheckboxBaseComponent implements Afte
6060
[key: string]: string;
6161
}>(ICONS_DICTIONARY, { optional: true });
6262

63-
super();
63+
const changeDetector = inject(ChangeDetectorRef);
64+
super(changeDetector);
65+
this.changeDetector = changeDetector;
6466

6567
this._iconToken = value ?? AnimaliaIconDictionary;
6668
}

projects/ui/src/lib/components/po-field/po-combo/po-combo-base.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,7 @@ export abstract class PoComboBaseComponent implements ControlValueAccessor, OnIn
10231023
// Usada para interceptar os estados de habilitado via forms api
10241024
setDisabledState(isDisabled: boolean) {
10251025
this.disabled = isDisabled;
1026+
this.changeDetector.markForCheck();
10261027
}
10271028

10281029
registerOnChange(fn: any): void {

projects/ui/src/lib/components/po-field/po-datepicker-range/po-datepicker-range-base.component.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,14 @@ describe('PoDatepickerRangeBaseComponent:', () => {
411411
});
412412

413413
it('setDisabledState: should set `component.disabled` with boolean parameter', () => {
414+
component['changeDetector'] = { markForCheck: () => {} } as any;
414415
const expectedValue = true;
416+
const markForCheck = spyOn(component['changeDetector'], 'markForCheck');
417+
415418
component.setDisabledState(expectedValue);
419+
416420
expect(component.disabled).toBe(expectedValue);
421+
expect(markForCheck).toHaveBeenCalled();
417422
});
418423

419424
it(`should call 'dateRangeFormatFailed', set 'errorMessage' as 'literals.invalidFormat'

projects/ui/src/lib/components/po-field/po-datepicker-range/po-datepicker-range-base.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ export abstract class PoDatepickerRangeBaseComponent implements ControlValueAcce
528528
// Usada para interceptar os estados de habilitado via forms api
529529
setDisabledState(isDisabled: boolean) {
530530
this.disabled = isDisabled;
531+
this.changeDetector.markForCheck();
531532
}
532533

533534
// Função implementada do ControlValueAccessor

projects/ui/src/lib/components/po-field/po-datepicker/po-datepicker-base.component.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('PoDatepickerBaseComponent:', () => {
2828
const languageService: PoLanguageService = new PoLanguageService();
2929

3030
beforeEach(() => {
31-
const changeDetector: any = { detectChanges: () => {} };
31+
const changeDetector: any = { detectChanges: () => {}, markForCheck: () => {} };
3232

3333
component = new PoDatepickerComponent(languageService, changeDetector);
3434
component['shortLanguage'] = 'pt';
@@ -193,8 +193,12 @@ describe('PoDatepickerBaseComponent:', () => {
193193

194194
it('setDisabledState: should set `component.disabled` with boolean parameter', () => {
195195
const expectedValue = true;
196+
const markForCheck = spyOn(component['cd'], 'markForCheck');
197+
196198
component.setDisabledState(expectedValue);
199+
197200
expect(component.disabled).toBe(expectedValue);
201+
expect(markForCheck).toHaveBeenCalled();
198202
});
199203

200204
it('should be call callOnChange with minDate', () => {

projects/ui/src/lib/components/po-field/po-datepicker/po-datepicker-base.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ export abstract class PoDatepickerBaseComponent implements ControlValueAccessor,
581581
// Usada para interceptar os estados de habilitado via forms api
582582
setDisabledState(isDisabled: boolean) {
583583
this.disabled = isDisabled;
584+
this.cd.markForCheck();
584585
}
585586

586587
// Função implementada do ControlValueAccessor

projects/ui/src/lib/components/po-field/po-datepicker/po-datepicker.component.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,17 @@ describe('PoDatepickerComponent:', () => {
754754

755755
expect(component.togglePicker).toHaveBeenCalled();
756756
});
757+
758+
it('should emit event when field is focused', () => {
759+
const fakeEvent = new KeyboardEvent('keydown', { key: 'Enter' });
760+
761+
spyOn(component.keydown, 'emit');
762+
spyOnProperty(document, 'activeElement', 'get').and.returnValue(component.inputEl.nativeElement);
763+
764+
component.onKeyDown(fakeEvent);
765+
766+
expect(component.keydown.emit).toHaveBeenCalled();
767+
});
757768
});
758769

759770
it('togglePicker: should keep the component invisible when `disabled` and `readonly` is true', () => {

projects/ui/src/lib/components/po-field/po-field-validate.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export abstract class PoFieldValidateModel<T> extends PoFieldModel<T> implements
7575
private onValidatorChange;
7676

7777
constructor(public changeDetector: ChangeDetectorRef) {
78-
super();
78+
super(changeDetector);
7979
}
8080

8181
validate(abstractControl: AbstractControl): ValidationErrors {

0 commit comments

Comments
 (0)