Cannot focus an input right after enabling it

Abierto
#3,772 4 comentarios 1 reacción 1 asignado Ver en GitHub

Los mantenedores suelen responder en 1 día

@alicewriteswrongs ya está trabajando en esto.

Desde el 27/10/2022.

Evaluación

Este issue todavía no se ha evaluado.

Descripción

Bug: Validated

Stencil version:

 @stencil/[email protected]

Current behavior:
We have a wrapper around an HTML <input> , essentially this (most of the implementation elided for brevity):

export class WrapperTextInput  {
  @Prop({ mutable: true }) value: string;
  @Prop() readonly disabled: boolean = false;
  render() {
    return (
      <Host role="textbox" contenteditable="true">
        <div>
            <input type="text" value={this.value} disabled={this.disabled}></input>
        </div>
      </Host>
    );
  }

Now let's say the WrapperTextInput is disabled, and you run the following code:

wrapperTextInput.disabled= false;
wrapperTextInput.focus();

The call to focus() does not work. The component does not receive focus.
The following is a workaround:

wrapperTextInput.disabled= false;
setTimeout(() => wrapperTextInput.focus(), 50);

This is surprising to users of our components, considering a native <input> handles this without the need for a delay. This is even more awkward in React, as users expect a change in properties to be reflected in the DOM by the time the next useEffect runs, for instance.
I assume this workaround works because it gives the component time to render in its non-disabled state so it can be focused. Does this analysis make sense?

Expected behavior:
It should be possible to focus the component right after setting the disabled property to true.

Repro steps:
Repro here: https://github.com/asik/stencil-focus-issue-repro

You'll be able to see that the native input receives focus, but not the one wrapped in a stencil component.
image
(Clicked the Toggle native button, focus is given to the native input)

image
(Clicked the Toggle Stencil button, focus is not given to the stencil component.)

Other information:
Somewhat related https://github.com/ionic-team/stencil/issues/3246 - although here, it's about the user of a StencilJS component, using it as a black box, so they could not leverage its lifecycle methods like componentDidRender.

Lenguaje dominante
TypeScript
Estrellas
13.2k
Forks
865
Merge medio
2 d 16 h
PR fusionados (30 d)
29

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de stenciljs/core

Todos los issues de stenciljs/core

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.