-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Should v-model work on components using both v-bind="$attrs" and v-on="$listeners"? #6216
Comments
I would say for the first question, I don't care if it works automatically, but that it at least doesn't require another abstraction to accomplish. It should basically work close to how it would work if there was no component. Literal ValuesThis is a huge part of the problem for me. I haven't been able to solve it because of a few cases where I want to use enum constants (I know these don't exist in JS, but they do in TypeScript, and I'm using a similar convention in a JS module) in option values.
<select>
<option :value="someExpression">One</option>
<option :value="someExpression">Two</option>
<option :value="someExpression">Three</option>
</select> My proposalMaybe it would be possible to make |
@RobertBColton in your example |
@nickmessing Thanks so much, that does seem to work, but I can't seem to make the following work without a vue warning about directly mutating the <template>
<select class="combo" v-bind:value="value" @input="update">
<option v-for="(item, key) in items" :value="item">{{key}}</option>
</select>
</template>
<script>
export default {
props: {
items: {
type: Object,
required: true
},
value: {}
},
methods: {
update(event) {
console.log(event.target._value);
this.$emit('input', event.target._value);
}
}
}
</script> Ideally I wouldn't want to and do not want to add another |
@RobertBColton, Github issues are not for asking questions, I am most of the time online in official chat (you can try stack overflow or forum too). |
My first impulse when 2.4 got out was to try using It would be an amazing feature imo 👍 As for the two options, I'm more inclined to agree with the first (seems simpler as far as I understood). |
@victor-am You could experiment with implementing option 2 just for your component, like in this simple example. |
I'm not sure this is desirable. First off, Making this work with Secondly, If we somehow make this work - that you can use Yes, under the hood it's technically still sending events and updating in the parent, but what happened under the hood never was the problem with Do we re-create this weekness by allowing for a seamless |
@LinusBorg I think you might misunderstand the proposal. There's no state syncing between components, as This comment suggested what you'd like to avoid, with state being maintained in both parent and child, but his use case and proposal isn't related to this feature request. |
Oh right, sorry. I misunderstood completely. |
Therefore, I guess making In my app, I'm wrapping a third party component and just adding custom style. I thought it should work with I think it is a bit weird that a component with a declared However, |
@frandiox I might have misunderstood what you mean, but I'm seeing |
Well, if the child properly declared the prop, I would expect to see it in $props, not $attrs, and that's what's happening. |
@LinusBorg I think @frandiox is suggesting he's seen |
@LinusBorg @chrisvfritz You are passing If it is declared as a prop, we all agree it should be included in |
I made some time this afternoon to fix this in #6327. 🎉 Reviews welcome. 🙂 |
Definitely something I want/need. Looks like the increasing popularity of HOC has made this a necessity. |
So, I finally gave this proposal a proper investigation, and it ends up being more complicated than it seems. The problem lies within consistency between different input types. The current implementation in #6327 assumes a text input and ignores non-input events. While this simplifies wrapping plain text input fields, it makes the whole thing inconsistent and feels like a special-case convenience hack. I managed to make it work with At this point I feel the implementation cost and the inconsistency outweighs the simplification this change would bring. Taking a step back, the whole idea of component |
All we want is to be able to create "fully transparent wrapper" components and add some functionality/customization on top... to have some semblance of inheritance in Vue. |
What problem does this feature solve?
Following this discussion, given a
CustomInput
component with this template:The following currently does not work:
The problem is that
v-model
on a component expects a value oninput
events, but a DOMEvent
is passed to the listener instead.What does the proposed API look like?
Option 1: Make
v-model
smarterSince I think it's unlikely that anyone will want to use
v-model
with a DOMEvent
, perhaps whenv-model
is used on components, it could check if the argument passed to the listener isinstanceof window.Event
. Then if it is, useevent.target.value
instead.Option 2: Make
v-on
smarterMaybe a non-enumerable property could be added to
$listeners
(e.g.__$listeners__: true
, to helpv-on
detect uses ofv-on="$listeners"
. Then in cases where the$listeners
object is passed tov-on
and it's used on an element, listeners relevant tov-model
could be wrapped:But now we're throwing away data. If someone wants to access a
keyCode
- e.g. with@input
instead ofv-model
- they're out of luck. However, if modifiers were also supported forv-on
's object syntax, we could fix this by making.native
disable the automatic wrapping behavior.Thoughts? Are there strategies or implications I'm not considering? I'm definitely open to alternatives.
The text was updated successfully, but these errors were encountered: