Skip to content

v-model with custom radio component replace onChange event #3516

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

Closed
clegendre opened this issue Aug 24, 2016 · 6 comments
Closed

v-model with custom radio component replace onChange event #3516

clegendre opened this issue Aug 24, 2016 · 6 comments
Labels

Comments

@clegendre
Copy link

Hello,

I'm writing a data-driven custom component that renders different input types from its data.
This component supports types like radio / checkbox / textarea etc.

All these types works fine with v-model directive on the component except of the radio type.

I found why: Vue2 hooks up the onChange event by adding :
'onChange': function(event){ myProp = undefined }

The component looks like this:

<dyn-component type="radio" ... ></dyn-component>

and renders something like this:

<div class="form-group">
  <label><input type="radio" /></label> 
  <label><input type="radio" /></label>
</div>

In fact, Vue2 considers that my component is an input type="radio" but this component renders internaly a radio-button list...

The workaround I used is to change the type from radio to radio-list, but maybe Vue should also check the tag name to be input in addition to the type="radio" attribute check.

@LinusBorg
Copy link
Member

I don't understand the problem exactly, can you provide a replication or at least the actual code (the relevant part)?

I found why: Vue2 hooks up the onChange event by adding :
'onChange': function(event){ myProp = undefined }

This does not tell us much. Vue generally does not set your prop to undefined, so it must have something to do with how you make the v-model aproach work.

@clegendre
Copy link
Author

Repro here: https://jsfiddle.net/4953qkkz/

Try changing radio with radio-list or anything else but radio, it works.

@LinusBorg
Copy link
Member

Thanks. This indeed seems like a bug. The $emitted input event is not correctly received by v-model on the component...

@clegendre
Copy link
Author

When you look at the generated render function of the component, the change event looks like:

_h('ck-form-group-input', {
        attrs: {
          ...
        },
        domProps: {
          ...
        },
        on: {
          "input": compute,
          "change": function($event) {
            typeProdChaleur = undefined
          }
        }
      }) 

The bug is here:
typeProdChaleur = undefined

Maybe it is a normal behavior for an input type="radio", but for another component, it is not.

@petejohanson
Copy link

With this change, some work I was doing on vue-mdl now won't work. In particular, for things like md-switch which behave like I would like the exposed properties to match what is expected, e.g.:

<mdl-switch v-model="checked" :value="Blue"></mdl-switch

And as I workaround, I was able to do the following:

<mdl-switch type="checkbox" v-model="checked" :value="Blue"></mdl-switch

And get the v-model working as expected with "checked" on the component, not value. With the change committed, this will no longer work. By all means, if there's some other solution for using v-model with a property other than "value", I'm all for it, but right now, there's no way with the last change.

Thoughts?

PS: The in progress vue-mdl code I had working against vue-2.0.0-rc.3 is here: https://github.com/petejohanson/vue-mdl/tree/ftr_v2

@posva
Copy link
Member

posva commented Aug 29, 2016

@petejohanson That's something we should look on vue-mdl end, not here 😄

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

No branches or pull requests

5 participants