Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I go about creating an always capitalized input in vue.js?

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?

like image 304
Ian Pierre Gomes Avatar asked Feb 07 '19 16:02

Ian Pierre Gomes


People also ask

Should Vue components be capitalized?

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.

Which keyword is used to create a constant in Vue JS?

The const keyword is used to create a constant in Vue. js.

What does $t mean in Vue?

Vuex is a state management pattern for vue. js. $t is the injected method from vue. js or Vue. i18n.

Why are components capitalized in vuejs?

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.

What is V-model in Vue?

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.

Can I use kebab case in vuejs?

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.

What is the watch property in Vue JS?

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.


3 Answers

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>
like image 193
Fab Avatar answered Oct 19 '22 19:10

Fab


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 />
like image 44
asologor Avatar answered Oct 19 '22 19:10

asologor


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()"/>
like image 45
Ferrybig Avatar answered Oct 19 '22 21:10

Ferrybig