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

Unable to give same key to different elements example : v-shortkey="['arrowdown']" on diff elements in same component #78

Open
madhuadbanker opened this issue Mar 13, 2019 · 8 comments
Assignees

Comments

@madhuadbanker
Copy link

No description provided.

@madhuadbanker
Copy link
Author

this issue is similar to issue #24
example only the last element with arrowdown works before elements with v-shortkey="['arrowdown']" not works is this feature not released or bug?

@fgr-araujo fgr-araujo self-assigned this Mar 20, 2019
@fgr-araujo
Copy link
Owner

Hi!
I will point it as a enhancement needed but I don't know if it's a good approach or not.

I think that you should have only one triggered shortcut and this shortcut trigger other actions inside his method.
The same shortcut in more then one element sounds weird to me. Thinking about documentation it'll be something like "Use Ctrl+I to Save and Print"... u know..

So, can you show me your use case?

@madhuadbanker
Copy link
Author

https://codesandbox.io/embed/z26y14lny3

I am getting "failed to resolve directive:shortkey" after i added vue-shortkey in this codesandbox. Before i got in my local i have restarted the system or reopned the node command prompt the error disappeared but this is the simple use case. Arrow down should work on both divs but works last event div"in this case second div list" i have tried many ways is there anyway to remove that event after passing first div. But as u mentioned "Use Ctrl+I to Save and Print" this is whole page level or document but like this same combination keys I am unable to give on different elements.

@fgr-araujo
Copy link
Owner

Let me know one thing:
Did you try to trigger the arrow down only in the "div" with the focus?

@madhuadbanker
Copy link
Author

madhuadbanker commented Mar 20, 2019 via email

@madhuadbanker
Copy link
Author

madhuadbanker commented Mar 20, 2019 via email

@elodieburtin
Copy link

Hello!

+1 for this issue, I would definitely consider it an improvement.

Since a use case was requested, here is mine:
I have several components in a page, each associated with a button, that can be triggered either by a click or by a shortkey. When the user uses a shortkey, it should only trigger a change in the active component. However, the active component does not necessarily have the focus (the situation is a bit more complex, and I store a variable in Vuex allowing to easily know if a component is active or not).

What I would like to do:
Define my buttons in the following way:
<button @click="buttonHandler" v-shortkey.once="['s']" @shortkey="buttonHandler">Click me</button>
Then, in my buttonHandler method, check if the component is active, and if so, perform the desired action.

With the current behaviour of vue-shortkey, I could define a global element catching the desired shortkey, and then in its handler, emit an event or use Vuex in order to transmit the information to the components, but it seems more complicated to me, without added value (especially since I have in fact more than one button per component, each triggering their own changes, and should therefore handle several types of events). What do you think?

@tartinesKiller
Copy link

Hi!
I have quite similar needs. In my case, I have a form with multiple items (fields). I want to be able to perform an action (fetch previously filled value for this field) when a shortkey is triggered, and keep the "context" of the currently focussed element, or at least the first parent which have a shortkey directive.

<template>
    <div id="form-item" v-shortkey="['ctrl', 'arrowleft']" @shortkey="shortkey">
      <input type="number" value="value" @input="val => $emit('input', val)" />
    </div>
</template>

<script>
export default {
    name: "FormItem",
    props: {
        item: {
            type: Object,
            required: true,
        },
        value: {
            type: Number,
            default: undefined,
        },
    },
    methods: {
        async shortkey () {
            const prevVal = await getPreviousValue(this.item.id);
            if (prevVal !== undefined) this.$emit('input', prevVal);
        }
    },
}
</script>

When the input have the focus, trigger the ctrl + arrowleft should call the shortkey() function of that form-item component, and thus call my method getPreviousValue() to get the previously filled value, with the form-item ID. (not a complete example)
Do you think it would be an acceptable behaviour? I'm working on it and plan to make a PR if it's OK

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

No branches or pull requests

4 participants