Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoFocus: Does not work inside dialog #12720

Closed
Urtgard opened this issue Mar 7, 2023 · 13 comments
Closed

AutoFocus: Does not work inside dialog #12720

Urtgard opened this issue Mar 7, 2023 · 13 comments

Comments

@Urtgard
Copy link
Contributor

Urtgard commented Mar 7, 2023

Describe the bug

If AutoFocus is used inside a dialog, focus is not applied to the element.

Environment

Stackblitz

Reproducer

https://stackblitz.com/edit/primeng-dialog-demo-u6rtzk?file=src%2Fapp%2Fapp.module.ts,src%2Fapp%2Fapp.component.spec.ts,src%2Fapp%2Fapp.component.html

Angular version

15.2.1

PrimeNG version

15.2.0

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

16.17.1

Browser(s)

No response

Steps to reproduce the behavior

Use autofocus inside a dialog.

<p-dialog [(visible)]="displayBasic">
  <input
    type="text"
    pInputText
    placeholder="Automatically focused"
    pAutoFocus
    [autofocus]="true"
  />
</p-dialog>

Expected behavior

Dialog opens and focus is applied to the element.

@Urtgard Urtgard added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Mar 7, 2023
@dluzhin
Copy link

dluzhin commented Apr 11, 2023

I confirm we have the same problem in our team. The problem also accures in dynamic dialog component

@nvkuznetsova
Copy link

I have the same issue inside dynamic dialog

@jgaston5
Copy link

I believe that this issue is related to the fix for #12319. It nolonger looks for the autofocus element, but rather just grabs the first focusable item.

@alvarosinmarca
Copy link

Same error with version primeng 16.0.2

@cetincakiroglu
Copy link
Contributor

cetincakiroglu commented Jul 21, 2023

This happens because autoFocus is applied on the initial load of the page rather than the component initialization, that's how the autofocus attribute works in a webpage. Directive's sole purpose is to give autofocus property in the correct way, some html attributes need to be assigned in a different way because of virtual dom. In your example, autofocus does the job and focuses on the first element, as I mentioned it does not work in component-wise, component initialization is not the case with HTML autofocus attribute it only works on the initial load of the webpage.

Because of this, autofocus will not work when the component is initialized and that's why the input inside the dialog doesn't receive focus since autofocus is already applied at the initial load to a different element. Even removing autofocus from the other input will not make it work as you wanted. Instead, you can implement it manually as below, please see the refactored example.
Closing the issue since it's a misunderstanding of the autofocus attribute, thanks for reporting!

https://stackblitz.com/edit/primeng-dialog-demo-hruxst?file=src%2Fapp%2Fapp.module.ts,src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html

@cetincakiroglu cetincakiroglu removed the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Jul 21, 2023
@alvarosinmarca
Copy link

alvarosinmarca commented Jul 22, 2023

@cetincakiroglu Thx for you example, but your example is used in html. I need use dynamic dialog. https://primeng.org/dynamicdialog

@VolodymyrKornetskyi
Copy link

@alvarosinmarca I have the same issue with dynamic dialog and don't know how to deal with this issue. Have you found any solutions already?

@VolodymyrKornetskyi
Copy link

@alvarosinmarca I found 2 not good ways to handle this issue. I removed focus css for first focusable item. But from source code it seems you can provide tab-index -1 and then they will skip your items. Not sure it can help you somehow.

@alvarosinmarca
Copy link

@VolodymyrKornetskyi I didn't find a solution, i will try your solution. Thank you!

@jonesisg1
Copy link

There is still an issue with autofocus="true" inside a dialog.
My bodge:

    myDialog.value = true;
    await nextTick();
    document.getElementById("myInputText").children[0].focus();

@angelo-v
Copy link

I can confirm this is still an issue, please reopen. Here is a reproducer:

https://stackblitz.com/edit/stackblitz-starters-98gaxj?file=src%2Fmain.ts

@gianluca-moro
Copy link

gianluca-moro commented May 14, 2024

I am currently facing the same issue and it would be really nice if this would work without having to implement a weird workaround. Thanks.

@graficon
Copy link

Facing the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests