Skip to content

v-bind="$attrs" and problems with "value" attribute for multi-checkboxes #7914

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
pearofducks opened this issue Mar 28, 2018 · 8 comments
Closed

Comments

@pearofducks
Copy link

pearofducks commented Mar 28, 2018

Version

2.5.16

Reproduction link

https://codepen.io/anon/pen/aYqoYL?editors=1010

Steps to reproduce

  • Check the checkbox
  • See that the value is set to "null" even though the input element has its value (correctly) set to "foo"

What is expected?

  • The value passed through "$attrs" should be the checkbox's value used

(or, the value never hits the DOM element, the fact that it is present on the DOM element but isn't used in the model is a conflict IMO)

What is actually happening?

  • Null is set
@pearofducks pearofducks changed the title v-bind="$attrs" ignores "value" attribute for multi-checkboxes v-bind="$attrs" and problems with "value" attribute for multi-checkboxes Mar 29, 2018
@emanuelmutschlechner
Copy link

Check the modified codepen. I changed the component logic to match default native multi checkbox behavior.

https://codepen.io/anon/pen/NYQWzo?editors=1011

@pearofducks
Copy link
Author

@emanuelmutschlechner I don't think your codepen is relevant here (though I appreciate you working toward a solution!). Adding an explicit value prop to my original pen and putting that prop on the input field works around the bug - as can be seen in this modified pen. The point is that that shouldn't need to be done.

v-bind=$attrs should be passing value to the input already in my original pen, and it isn't doing so in a way v-model is picking up on.

@emanuelmutschlechner
Copy link

@pearofducks If you want v-model to pick up the value defined on your component, explicitly bind value in your custom component

https://codepen.io/anon/pen/GxVJzW?editors=1010

@pearofducks
Copy link
Author

@emanuelmutschlechner - please read my previous response. With $attrs this shouldn't be necessary. I'm pretty confident this is a valid bug, but I appreciate you trying to help.

@emanuelmutschlechner
Copy link

I get your point, but I'm not sure if this is a bug or not. We need some feedback from the core team.

My solution is partly based on this example code, where value is bound explicit and used with disabled attribute inheritance. source

Vue.component('base-input', {
  inheritAttrs: false,
  props: ['label', 'value'],
  computed: {
    inputListeners: function () {
      var vm = this
      // `Object.assign` merges objects together to form a new object
      return Object.assign({},
        // We add all the listeners from the parent
        this.$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) {
            vm.$emit('input', event.target.value)
          }
        }
      )
    }
  },
  template: `
    <label>
      {{ label }}
      <input
        v-bind="$attrs"
        v-bind:value="value"
        v-on="inputListeners"
      >
    </label>
  `
})

@emanuelmutschlechner
Copy link

Maybe this issue is somewhat related to #6216?

@ghost
Copy link

ghost commented May 14, 2019

I think both of you are right. And, @emanuelmutschlechner, you are correct, right now this is the way of doing it: @chrisvfritz explains the state of the art and future approaches here:

Vue 3 presentation by Chris Fritz

(https://www.youtube.com/watch?v=b554XmaTWrw)

@jloa
Copy link

jloa commented Feb 16, 2020

Just wanted to that i've also faced this in my project.
Basically the problem can be narrowed to this:

// this works with emit('input')
v-bind:value="value"

// this fails to work with emit('input')
v-bind="{ value:value }"

So yeah, we have to explicitly set the :value to make things work, as @emanuelmutschlechner said earlier.
Imho, this seems more like a bug.

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