Skip to content

Commit ba88d94

Browse files
committed
fix(#19010): [autofocus] update autofocus behavior for undefined values
1 parent 96fe184 commit ba88d94

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

packages/primeng/src/autofocus/autofocus.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ class TestAutofocusDynamicComponent {
3333
autofocusEnabled = false;
3434
}
3535

36+
@Component({
37+
standalone: false,
38+
selector: 'test-autofocus-undefined',
39+
template: `<input type="text" [pAutoFocus]="autofocus" />`
40+
})
41+
class TestAutofocusUndefinedComponent {
42+
autofocus: any = undefined;
43+
}
44+
3645
@Component({
3746
standalone: false,
3847
selector: 'test-autofocus-button',
@@ -198,6 +207,7 @@ describe('AutoFocus', () => {
198207
TestAutofocusDisabledComponent,
199208
TestAutofocusEnabledComponent,
200209
TestAutofocusDynamicComponent,
210+
TestAutofocusUndefinedComponent,
201211
TestAutofocusButtonComponent,
202212
TestAutofocusDivComponent,
203213
TestAutofocusMultipleElementsComponent,
@@ -300,6 +310,38 @@ describe('AutoFocus', () => {
300310
});
301311
});
302312

313+
describe('Undefined Binding', () => {
314+
it('should not set autofocus attribute when bound value is undefined', async () => {
315+
const fixture = TestBed.createComponent(TestAutofocusUndefinedComponent);
316+
const component = fixture.componentInstance;
317+
await fixture.whenStable();
318+
319+
const element = fixture.debugElement.query(By.css('input')).nativeElement;
320+
321+
spyOn(element, 'focus');
322+
323+
expect(element.hasAttribute('autofocus')).toBe(false);
324+
325+
await new Promise((resolve) => setTimeout(resolve, 10));
326+
expect(element.focus).not.toHaveBeenCalled();
327+
});
328+
329+
it('should set autofocus attribute and focus when property changes to true', async () => {
330+
const fixture = TestBed.createComponent(TestAutofocusUndefinedComponent);
331+
const component = fixture.componentInstance;
332+
await fixture.whenStable();
333+
334+
const element = fixture.debugElement.query(By.css('input')).nativeElement;
335+
336+
component.autofocus = true;
337+
fixture.changeDetectorRef.markForCheck();
338+
await fixture.whenStable();
339+
340+
expect(element.hasAttribute('autofocus')).toBe(true);
341+
expect(element.getAttribute('autofocus')).toBe('true');
342+
});
343+
});
344+
303345
describe('Focus Behavior - Browser Platform', () => {
304346
it('should focus input element when autofocus is enabled', async () => {
305347
const fixture = TestBed.createComponent(TestAutofocusEnabledComponent);

packages/primeng/src/autofocus/autofocus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class AutoFocus extends BaseComponent {
2828

2929
onAfterContentChecked() {
3030
// This sets the `attr.autofocus` which is different than the Input `autofocus` attribute.
31-
if (this.autofocus === false) {
31+
if (this.autofocus) {
3232
this.host.nativeElement.removeAttribute('autofocus');
3333
} else {
3434
this.host.nativeElement.setAttribute('autofocus', true);

0 commit comments

Comments
 (0)