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

Proxying events with v-on="computed" #128

Closed
timshannon opened this issue Sep 11, 2019 · 5 comments · Fixed by #133
Closed

Proxying events with v-on="computed" #128

timshannon opened this issue Sep 11, 2019 · 5 comments · Fixed by #133
Labels
enhancement New feature or request

Comments

@timshannon
Copy link

I'm trying to replicate the proxying of events as described here: https://vuejs.org/v2/guide/components-custom-events.html#Binding-Native-Events-to-Components in the composition api. But am unable to get the events to proxy like they do without the plugin.

<label>
{{ label }}
    <input
        v-bind:value="value"
        v-on="inputListeners"
    >
</label>
export default createComponent({
    props: ["label", "value"],
    setup(props, context) {
        const inputListeners = computed(() => {
            // `Object.assign` merges objects together to form a new object
            return Object.assign({},
                // We add all the listeners from the parent
                context.root.$listeners,
                // Then we can add custom listeners or override the
                // behavior of some listeners.
                {
                    // This ensures that the component works with v-model
                    input: (event) => {
                        context.emit("input", event.target.value);
                    },
                },
            )
        });
        return {
            inputListeners,
        };
    },
});

Full fiddle here: https://jsfiddle.net/4wfzh0u6/

@liximomo
Copy link
Member

The event proxy works file in the fiddle. What's your expectation?

@timshannon
Copy link
Author

The blur event doesn't fire for me. Otherwise it would write "blur event works" in the console. Are you seeing that?

If it's proxying the blur event from the encapsulated component to the input element, then it should trigger that blur event.

@liximomo
Copy link
Member

Object.assign({},
  // We add all the listeners from the parent
  context.root.$listeners, 
  // Then we can add custom listeners or override the
  // behavior of some listeners.
  {
    // This ensures that the component works with v-model
    input: function (event) {
      context.emit('input', event.target.value)
    },
  },
)

You should not use context.root.$listeners.

image

Unfortunately, we can't do this now because this is not available in setup().

@liximomo liximomo added the enhancement New feature or request label Sep 16, 2019
@LinusBorg
Copy link
Member

LinusBorg commented Sep 16, 2019

I think the problem is that there's not context.listeners. That's ok insofar as the RFC spec says that there is none, but that's only because, according to another RFC, this.$listeners and this.$attrs will be merged in Vue 3.

@liximomo So I think we should introduce a context.listeners property for this plugin - that would be in line with the spirit of the RFCs, as context should allow users to access these properties.

@liximomo
Copy link
Member

@LinusBorg I agreed.

LinusBorg added a commit that referenced this issue Sep 17, 2019
close #128, #132

so far missing tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants