Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue: need to disable all inputs on page

I'm developing an app that has different license types, and according to the license we need to disable/enable inputs.

One way is to put a conditional :disabled for each input but that's a lot of work and error prone, since we might forget to put it on some inputs.

I thought of using a directive like v-disable-all that searches for all inputs under the container and adds disabled to them.

I was wandering if there is a better solution or if there is already a solution like this?

like image 326
Tomer Avatar asked Apr 29 '19 14:04

Tomer


People also ask

How do you turn off input on Vue?

We can disable inputs conditionally with Vue 3 by setting the disabled prop to the condition when we want to disable the input. We have the input with the disabled prop set to the disabled reactive property. Below that, we have the @click directive to toggle the disabled reactive property when we click the button.

How do I turn off Vue components?

Vue Button component can be enabled/disabled by giving disabled property. To disable Vue Button component, the disabled property can be set as true .

How can you prevent layout jumps in Vue JS?

Use single file components if you can. If you come from React you can also find jsx for Vue useful, or even write render function by hand (though not recommended). If you want to totally nullify the time of the first rendering though, the only way to go is to do server-side rendering.

What does Vue extend do?

Since Vue itself is a constructor, Vue. extend() is a class inheritance method. Its task is to create a sub-class of Vue and return the constructor.


1 Answers

I ended up creating this directive:

import Vue from "vue";

Vue.directive("disable-all", {
  // When all the children of the parent component have been updated
  componentUpdated: function(el, binding) {
    if (!binding.value) return;
    const tags = ["input", "button", "textarea", "select"];
    tags.forEach(tagName => {
      const nodes = el.getElementsByTagName(tagName);
      for (let i = 0; i < nodes.length; i++) {
        nodes[i].disabled = true;
        nodes[i].tabIndex = -1;
      }
    });
  }
});
like image 60
Tomer Avatar answered Sep 22 '22 20:09

Tomer