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

Manual Focus #22

Closed
jameshhood opened this issue Dec 2, 2020 · 3 comments
Closed

Manual Focus #22

jameshhood opened this issue Dec 2, 2020 · 3 comments

Comments

@jameshhood
Copy link

So I'm using this inside a dialog in a vuetify app to prevent a user from entering in an admin page.
Functionally everything works great but because the way vuetify mounts dialogs the autofocus doesn't work right.
Is there a ref or a way to manually invoke a focus to the first element?
The following code does not work

<v-dialog v-model="pinDlg fullscreen>
    <CodeInput ref="pincode" .../>
</v-dialog>

if(this.pinDlg) {
    this.$refs.pincode.focus()
}
@Seokky
Copy link
Owner

Seokky commented Dec 2, 2020

You trying to focus the wrapper element. Try to get first cell element instead and apply focus to it.

@jameshhood
Copy link
Author

jameshhood commented Dec 8, 2020

@Seokky The issue is that I don't declare any children elements.

<CodeInput v-if="adminDlg" v-model="adminPin" :length="4" secure />

This is the extent of my code and I let the component do the rest as far as the UI is concerned.
This is the breakdown of my elements and the component does not seem to generate dynamic refs for manual use so I'm not sure this is possible.
image

I would imagine it would work something like this example based on the number provided in the length prop
https://stackoverflow.com/questions/45563329/how-to-add-dynamic-ref-in-vue-js/45563531

So if each pin input generated had something like :ref="pin_input_${i}"
I could manually focus on this.$refs.pin_input_1.focus()

Edit

It appears you're already doing all that in the code but for whatever reason vue-pincode-input0 doesn't exist
this.$refs['vue-pincode-input0'].focus() results in an error because the ref is undefined.

@Seokky
Copy link
Owner

Seokky commented Dec 8, 2020

Dude, I cannot prepare some refs inside the component for use by the end user. Vue doesn't work that way. Refs are available within a single component level. Yes, of course, you can come up with all sorts of tricks with passing an object by reference, but this is a non-vue way. You need to use native JavaScript to access the cell. You can search for an element with the desired number order inside the wrapper and then focus on it, as native JavaScript can do.

const wrapper = this.$refs.pincode.$el;
const cells = wrapper.getElementsByClassName('vue-pincode-input');
      
cells[0].focus();

@Seokky Seokky closed this as completed Dec 8, 2020
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

2 participants