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

Custom Directive. Inserted hook. Element moved after inserted hook ended. #7761

Closed
jacobgoh101 opened this issue Mar 7, 2018 · 3 comments
Closed

Comments

@jacobgoh101
Copy link

jacobgoh101 commented Mar 7, 2018

Version

2.5.3

Reproduction link

https://jsfiddle.net/jacobgoh101/y1L2v9d4/

Steps to reproduce

  1. Click show button

What is expected?

input#timing should be focused

What is actually happening?

input#timing was focused, but something else cause it to blur immediately


Bug discovered from this Stack Overflow question together with acdcjunior.

from this example https://jsfiddle.net/acdcjunior/srfse9oe/1/ , after clicking on show button, the console log these

beforeUpdate

inserted <div id="app"><div><p>a</p> <input type="number" id="timing"> <p>b</p> <button type="button">show</button> <p>c</p> <!----> <p>d</p></div></div>

updated

nextTick <div id="app"><div><p>a</p> <!----> <p>b</p> <!----> <p>c</p> <input type="number" id="timing"> <p>d</p></div>  </div>

in inserted hook, input#timing is between <p>a</p> and <p>b</p>. But in nextTick callback, input#timing has been moved to between <p>c</p> and <p>d</p>, for no particular reason. This caused the element to blur out.

See more details in this SO answer

@jacobgoh101 jacobgoh101 changed the title Custom Directive. Inserted hook. Element is moved after inserted hook ended. Custom Directive. Inserted hook. Element moved after inserted hook ended. Mar 7, 2018
@posva
Copy link
Member

posva commented Mar 7, 2018

Seems related to #4362

The element gets moved more than necessary causing it to blur out, but I wonder if inserted could be called at the end rather than exactly when inserted...

As a valid workaround, you can do Vue.nextTick(() => el.focus()) to focus the element

@jacobgoh101
Copy link
Author

Vue.nextTick(() => el.focus()) does work. Confirmed.

@yyx990803
Copy link
Member

This is because in your case the same input element was reused, and the inserted hook fired before it was moved.

Technically, the diffing could probably be improved since there no need to really move it, but this cannot be guaranteed in all cases when no keys are provided, so it's safer to always do focus in a nextTick.

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

3 participants