Skip to content

Commit

Permalink
fix(dynamic-form): ajusta evento validate com método clean
Browse files Browse the repository at this point in the history
O evento `validate` apenas é disparado se o usuário tocar no campo
antes de clicar no `X` do `clean`.

Foi feito um ajuste para que o evento `validate` seja disparado mesmo
o usuário clicar diretamente no `X` do `clean` sem tocar no campo.

Fixes #1837
  • Loading branch information
wsteixeira committed Oct 26, 2023
1 parent f92633d commit 319cb86
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { PoCleanBaseComponent } from './po-clean-base.component';

@Directive()
class PoClean extends PoCleanBaseComponent {
focus(): void {}

blur(): void {}

setInputValue(value: string): void {}

getInputValue(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export abstract class PoCleanBaseComponent {
@Output('p-change-event') changeEvent: EventEmitter<any> = new EventEmitter<any>();

clear() {
this.focus();
this.blur();
this.setInputValue(this.defaultValue);
this.changeEvent.emit(this.defaultValue);
}
Expand All @@ -37,6 +39,10 @@ export abstract class PoCleanBaseComponent {
return this.defaultValue !== this.getInputValue();
}

abstract focus(): void;

abstract blur(): void;

abstract setInputValue(value: string): void;

abstract getInputValue(): string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ describe('PoCleanComponent', () => {
expect(fixture.nativeElement.querySelector('.po-icon.po-field-icon.po-icon-close')).toBeDefined();
});

it('focus: should call `focus` of input', () => {
component.inputRef = {
nativeElement: {
focus: () => {}
}
};

spyOn(component.inputRef.nativeElement, 'focus');

component.focus();

expect(component.inputRef.nativeElement.focus).toHaveBeenCalled();
});

it('focus: should call `blur` of input', () => {
component.inputRef = {
nativeElement: {
blur: () => {}
}
};

spyOn(component.inputRef.nativeElement, 'blur');

component.blur();

expect(component.inputRef.nativeElement.blur).toHaveBeenCalled();
});

it('should set value to input', () => {
const fakeThis = {
inputRef: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ import { PoCleanBaseComponent } from './po-clean-base.component';
templateUrl: './po-clean.component.html'
})
export class PoCleanComponent extends PoCleanBaseComponent {
focus() {
this.inputRef.nativeElement.focus();
}

blur() {
this.inputRef.nativeElement.blur();
}

setInputValue(value?: string) {
if (this.inputRef && this.inputRef.nativeElement) {
this.inputRef.nativeElement.value = value;
Expand Down

0 comments on commit 319cb86

Please sign in to comment.