I'm creating a form using vue.js and I need to create inputs in vue that is always capitalized. I know I could use the css property
text-transform: uppercase;
and then transform the data before sending using
data.someData.toUpperCase()
But I wonder if there is a more intelligent way of doing that in vue.js. In react we can create controlled inputs and easily do it. Is there anything like that in Vue.js?
I managed to do it using computed fields, however, I would have to create computed getter and setter for each input in the form. Is there a better way of doing it?
From what I have seen and used, vuejs doesn't mind your component files', component names' case. Note that Vue does not enforce the W3C rules for custom tag names (all-lowercase, must contain a hyphen) though following this convention is considered good practice.
The const keyword is used to create a constant in Vue. js.
Vuex is a state management pattern for vue. js. $t is the injected method from vue. js or Vue. i18n.
React/React-Native Developers are used to capitalizing components because of the Class based component architecture. So they tend to carry that over to Vue. The Vue engine itself doesn't care, it interprets both just fine.
It is built on top of HTML, CSS, and Javascript which makes it easier for developers to integrate Vue.js in any application at any stage. Form Input Value Binding is handled by the v-model directive that helps to bind the static strings, to simplify the input value binding.
Note that Vue does not enforce the W3C rules for custom tag names (all-lowercase, must contain a hyphen) though following this convention is considered good practice. So you can use an imported component in kebab-case or camelCase vuejs internally checks for it and would render the component the same way.
Sometimes doing a simple task (like changing a text to uppercase or lowercase) can turn tedious and take away a large amount of time. Thanks to the watch property of Vue.js, making changes to a state has become so easy.
You could create a custom directive.
Vue.directive( 'touppercase', {
update (el) {
el.value = el.value.toUpperCase()
},
})
And then use it where you need. For example:
<input type="text" v-model="modelfield" v-touppercase>
This directive works fine with v-model
(last character is in upper case too):
Vue.directive('uppercase', {
update(el) {
const sourceValue = el.value;
const newValue = sourceValue.toUpperCase();
if (sourceValue !== newValue) {
el.value = newValue;
el.dispatchEvent(new Event('input', { bubbles: true }));
}
},
});
Usage:
<input type="text" v-model="myField" v-uppercase />
Since you don't have a lot of code to run, you should manually bind events to your textfield and then handle the uppercasing there.
Handling events from a text field can be done by adding an input
event handler on them, and then updating the initial state again.
<input :value="text" @input="updateText($event.target.value)"/>
export default {
data() {
return {
text: '',
}
},
methods: {
updateText(newValue) {
this.value = newValue.toUpperCase();
},
}
}
You can also do it inline in a template, but this might make it harder to read depending on your code style preferences
<input :value="text" @input="text = $event.target.value.toUpperCase()"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With