primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Dialog should focus first element on open

Open cagataycivici opened this issue 8 years ago • 12 comments

cagataycivici avatar Dec 19 '17 07:12 cagataycivici

There is a problem with this. When a dropdown is the first component, this autofocus causes an error

Expression has changed after it was checked. Previous value: 'ui-inputwrapper-filled: undefined'. Current value: 'ui-inputwrapper-filled: true'

vigalo avatar Feb 08 '18 10:02 vigalo

I have the same problem, my first element is p-calendar. I think that this feature should be optional.

markovicboban avatar Feb 16 '18 12:02 markovicboban

Same here

bobbydowling avatar Feb 21 '18 01:02 bobbydowling

I'm having the same problem with the p-calendar. Any word on a resolution?

jboothe avatar Feb 21 '18 18:02 jboothe

I just ran into this as well. A hacky workaround: add this before your first element: <input [style.display]="'none'" />

Obviously, a real fix is needed.

andyg3 avatar Feb 26 '18 15:02 andyg3

check this link https://plnkr.co/edit/g7ureOse1EQlRwUpriHt?p=preview added autofocus attribute but it works only once , only chrome browser

sridhark158 avatar Apr 05 '18 05:04 sridhark158

Here's my version of the "autofocus" directive. It refers to my custom components but you can replace it with whatever you need

@Directive({ selector: '[appAutoFocus]' })
export class AutoFocusDirective implements AfterViewInit {
    private _show = true;

    constructor(
        private _ref: ElementRef,
        @Host() @Self() @Optional() private _it: InputTextComponent,
        @Host() @Self() @Optional() private _cb: ComboboxComponent,
        @Host() @Self() @Optional() private _cs: CompanySearchComponent,
        @Host() @Self() @Optional() private _fs: FileSearchComponent,
    ) {}

    @Input() set appAutoFocus(show: boolean | string) {
        if (typeof(show) === 'boolean') //typeof is string if parameter not specified
            this._show = show;
    }

    ngAfterViewInit(): void {
        if (!this._show) return;

        let el: HTMLElement;
        if (this._it)
            el = this._it.nativeElement;
        else if (this._cb)
            el = this._cb.nativeInputElement;
        else if (this._cs)
            el = this._cs.nativeInputElement;
        else if (this._fs)
            el = this._fs.nativeInputElement;
        else
            el = this._ref.nativeElement;

        el.focus();
    }
}

ctrl-brk avatar Apr 05 '18 15:04 ctrl-brk

Hi @ctrl-brk InputTextComponent,ComboboxComponent,CompanySearchComponent,FileSearchComponent, i got error with this components should i import these components?

sridhark158 avatar Apr 11 '18 09:04 sridhark158

@cagataycivici Commit https://github.com/primefaces/primeng/commit/5687458e77ded2848c09b7c668533ab707f28791 is tagged with 6.1, but the release does not contain this commit. Is there a timeline for this fix/feature?

nk6 avatar Aug 09 '18 07:08 nk6

By default focusOnShow is TRUE. i.e first button receives focus on show. set [focusOnShow]="false". Cheers!

rohkoli94 avatar Apr 08 '19 07:04 rohkoli94

The Commit which 'closed' this issue never made it to the master, as you can see in Line 226 of dialog.ts.

From my point of view the desired behaviour should be that the first element of type 'button, input, textarea, select' should be focused (as in the commit). So if the first focusable element in the dialog is an input field it should get the focus.

@cagataycivici Is there a chance that this fix is going to the master?

nk6 avatar Aug 01 '19 14:08 nk6

I have problem with nested dialogs. When I open and close second dialog, the first element of first dialog gets focus. No matter focusOnShow is set to true or false!

@cagataycivici Is it related to this issue?

ngsaeed avatar Mar 03 '25 13:03 ngsaeed