-
-
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
Add custom v-model modifiers #3666
Comments
@posva computed properties are not reusable. |
Almost everything is reusable through a mixin. On Tue, 13 Sep 2016, 18:48 Francisco Lourenço, notifications@github.com
|
Put in another way, computed properties are not reusable. You can use factory functions + mixins as a work around, but the usability and readability doesn't compare. |
For my project, I badly needed this feature so I used the recommended custom input approach: InputCustom.js
InputText.js
In my opinion, this approach is very convenient and I decided not to use any v-model modifiers at all including |
For more customized use cases that built-in modifiers cannot support, what @ecmel mentioned is the recommended approach. We will document that in more details in the official guide. |
The idea of this feature proposal is to take advantage of the existing If it makes sense from the API perspective, would be interesting to know what are the technical limitations which are driving the decision of not implementing this feature. |
Any chance we can get this issue reopened? A common use case for me is the need to automatically format data in a field as it's being typed in. Something like taking '101216' and turning it into '10/12/16'. Being able to create a custom v-model modifier would greatly simplify my code since I could write v-model.date instead of having to build a custom input component with props and events. |
After using vue js for a while now in my project, I think this issue should indeed be reopened. At least we need an |
I agree that this issue should be reopened. Not sure what I'd love to be able to do that myself in a straightforward way. |
Functionality more redundant than this has been added for example with #5194 . From the outside, Vue appears to slowly be compromising some of its principles in favour of conventions and practices promoted by the react community. Slightly deviating from the qualities which made it stand out in the first place. Would be interesting to know if this is a conscious decision with the intention to make migration from react easier, or just coincidence. |
Writing custom components is fine but if you want to use a 3rd party custom component like https://github.com/text-mask/text-mask/tree/master/vue#readme there is no straight forward way to sanitize the masked input to model values except using computed properties. |
So, I just want to use an HTML standard input[type=date] field to edit a date type in my model and this wonderfully powerfull and extensible framework can't do that out of the box? Can't read the date into the field, overwrite my date with a string in my data after I select a date. This solution could be written in two lines with two-way filters or with modifier. But the best solution at all would be to just support it natively as they do for checkbox and other standard input field, why is "date" a special thing? |
+1 for custom modifiers. Seems like a no brainer, unless there is a good reason not to? Masking input and parsing the value for application use is a very common practice, and making some "syntatic sugar" like v-model.lazy.currency="amount" would be amazing! |
1+ for custom modifiers. |
+1 for custom modifiers but I agree with tobei -- input[type=date] should work automagically. |
+1 for custom modifiers. I come from an Angular background, and just started with vue, and saw this thread. I feel it would really help having something like Angular's parsers and formatters, in Vue too. If I could do something like v-model.dateFormat and result in something like mm/dd/yyyy, it would be really cool. EDIT: looks like it reiterated on what @restored18 said. +1 to you too |
+1 for custom v-model modifiers. In my case I loop over a few nested objects retrieved in JSON, and use a single HTML template (rather than a template per object). In this case I believe computed properties don't work? I'm currently putting in custom conversion methods between the server format and v-model format when fetching and sending data, but would love for something just "built-in" that I could pass the functions to. |
+1 to this. It used to be available before 2.2. You could access the property through,
It was removed with the addition of custom model input values, but was a very useful feature and should be back in the framework. |
+1 for this. Custom v-model modifiers would be great! |
#metoo |
+1 for custom modifiers, there are plenty of use-cases that could be accomplished with this feature In our application there are few different inputs to format currencies, we always store cents amount in model, but display nicely formatted dollars amounts in inputs (so 123456 in model shown as $1,234.56) Other use-case is sanitizing and unescaping html fields in order to prevent XSS atacks (Model stores " |
+1 |
1 similar comment
+1 |
+1 for custom modifiers. |
+1 |
2 similar comments
+1 |
+1 |
+1 |
should we open an issue here? https://github.com/vuejs/rfcs |
i have already +1 this, but want to throw a note for people who need something “now” while modifier is way more effecient, i have been able to achieve the same effect using a transparent input/component with a computed getter/setter field. i can share an example if someone needs it |
We did not implement this because there are many things to be considered in this seemingly easy feature:
All of these questions are unanswered and makes the request more complex than it seems. This is why I agree it would be a good candidate for a proper RFC that covers all these if anyone really want this feature. Until then, more +1s doesn't move it forward in any way. |
@rkingon There is already an example in #3666 (comment), but if yours is different/better, just post it. It can be useful for novices. |
@Gotterbild i did miss that sample, but it is widely over complicated in later versions of Vue (that could have been pre transparent component support) here is a very simple one I have that just converts a percent to decimal (ie: 4.5 -> .045) & vice versa ("view" value and "model" value) <template>
<input type="number" v-on="listeners" v-model="innerValue">
</template>
<script>
export default {
props: ['value'],
computed: {
listeners () {
const { input, ...listeners } = this.$listeners
return listeners
},
innerValue: {
get () {
return (this.value) ? `${(this.value * 100)}` : ''
},
set (_val) {
const val = (_val) ? (_val / 100) : ''
this.$emit('input', val)
}
}
}
}
</script> this is more simple than the above since you do not have to respecify all of the focus/blur/etc |
@yyx990803 thanks for giving more background information on this topic. for my use-cases, i do not "need" custom modifieres. it's just that from a consumers-perspective, it would make sense that vue has a way to build your own. it basically has that for everything, except modifieres 😄 while i could find ways to create something that properly encapsulates my transformation logic and that i could reuse, i think that having a proper API for such use-cases would open up a much broader way of sharing common code through a collection of "best-practice modifiers". |
I came here because I was looking to make modifier to convert string to uppercase I ended up using custom directive
Custom directives should be enough for an alternative. |
@Christhofernatalius considering |
@rkingon Is it updating twice? EDIT: Isn't using computed with setter and getter also updating twice? |
i haven't tried it, merely speculation -- i suppose the idea of two directives to update one value just feels a bit funny to me, but if you've checked it out, i don't see anything wrong with it. my component workaround has limitations too, which is why i'm still in favor of a modifier -- ie: it requires an element, more rendering time, & only works as a component in how that component is defined (ie: an input field) vs being able to even simply use it on some arbitrary component/element which is the power of a directive. two ways to skin the cat, nice to have options :) |
Is there an RFC for this? |
i don't think so |
I could create it but does it just mean adding an issue in that RFC repo or something else? |
https://github.com/vuejs/rfcs#what-the-process-is does this answer your question? |
Can this be reopened please? A custom directive may work in some cases but behavior can be funny, example:
If you add this directive (along with a v-model) and type letters really fast every other keystrokes the |
@jeankvd I know it feels like overkill, but a wrapper component is going to be the most reliable (see example above). a wrapper component will allow you to "do more" as well. In your "number" example, ideally, the v-model actually becomes a What if you want to customize the empty value? Empty String? Null? Undefined? -- you can pass in a prop for While once an advocate for this, I realized shortly after modifiers have way too many limitations and just having a component is far superior (at least imo). |
We have
.lazy
,.number
,.trim
and.undef
is on the way.Apart from
.lazy
they all work like two-way filters.Since 2.0 does not support 2 way filters, may be there should be a new api to add custom v-model modifiers to fulfill the same need.
The text was updated successfully, but these errors were encountered: