Search code examples
javascriptvuejs2componentsinput-field

vue2 disable input with multiple components


Vue 2 - disable input - multiple components

Hi all,

I struggle to solve a problem, where I want to disable other input fields, once the first char has been entered in another input field.

I've been trying to solve this with $emit, @focus, and other solutions and I'm still stuck. I was also not able to utilize the answers to be found here.

Snippet:

const Autocomplete = {
  name: "autocomplete",
  props: {
    items: {
      type: Array,
      required: false,
      default: () => ['test']
    },
    isAsync: {
      type: Boolean,
      required: false,
      default: false
    },
    formLock: {
      type: Boolean,
    },
    formId: {
      type: String,
    }
  },

  data() {
    return {
      isOpen: false,
      results: [],
      search: "",
      isLoading: false,
      arrowCounter: 0,
    };
  },

  methods: {
    onChange() {
      // Let's warn the parent that a change was made
      this.$emit("input", this.search);

      // Is the data given by an outside ajax request?
      if (this.isAsync) {
        this.isLoading = true;
      } else {
        // Let's search our flat array
        this.filterResults();
        this.isOpen = true;
      }

      if (this.search.length === 0) {
        this.isOpen = false;
      }

      console.log(this.search.length);

    },

    disableOther() {
      var searchForms = document.getElementsByClassName('searchForm');
      for (i = 0; i < searchForms.length; i++) {

      }

      console.log(searchForms.length);

    },

    filterResults() {
      // first uncapitalize all the things
      this.results = this.items.filter(item => {
        return item.toLowerCase().indexOf(this.search.toLowerCase()) > -1;
      });
    },
    setResult(result) {
      this.search = result;
      this.isOpen = false;
    },
    onArrowDown(evt) {
      if (this.arrowCounter < this.results.length) {
        this.arrowCounter = this.arrowCounter + 1;
      }
    },
    onArrowUp() {
      if (this.arrowCounter > 0) {
        this.arrowCounter = this.arrowCounter - 1;
      }
    },
    onEnter() {
      this.search = this.results[this.arrowCounter];
      this.isOpen = false;
      this.arrowCounter = -1;
    },
    handleClickOutside(evt) {
      if (!this.$el.contains(evt.target)) {
        this.isOpen = false;
        this.arrowCounter = -1;
      }
    }
  },

  mounted() {
    document.addEventListener("click", this.handleClickOutside);

  },
  destroyed() {
    document.removeEventListener("click", this.handleClickOutside);
  },

  template: `

    <div>
        <input type="text" @input="onChange" class="searchForm" v-model="search" @keyup.down="onArrowDown" @keyup.up="onArrowUp" @keyup.enter="onEnter" v-bind:disabled="formLock" @focus="disableOther" />
        <ul id="autocomplete-results" v-show="isOpen" class="autocomplete-results">
          <li class="loading" v-if="isLoading">
            Loading results...
          </li>
          <li v-else v-for="(result, i) in results" :key="i" @click="setResult(result)" class="autocomplete-result" :class="{ 'is-active': i === arrowCounter }">
            {{ result }}
          </li>
        </ul>
    </div>
    
        `,
};

new Vue({
  el: "#productSearchApp",
  name: "productSearchApp",

  data() {
    return {
      productName: [],
      productCatalog: [],
      lock: false,
      searchName: "searchForm",
      searchCatalog: "searchCatalog"
    }
  },

  mounted() {
    fetch("http://cormay.314-work.pl/wp-json/wp/v2/product")
      .then(response => response.json())
      .then((data) => {
        for (i = 0; i < data.length; i++) {
          this.productName.push(data[i].title.rendered);
        };
        for (i = 0; i < data.length; i++) {
          this.productCatalog.push(data[i].product_catalog);
        };
      })
  },

  components: {
    autocomplete: Autocomplete,
  },

  methods: {
    updateLock(updateLock) {
      this.lock = updateLock;
    }
  }
});
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<div id="productSearchApp">
  <autocomplete :items="productName" :form-id="searchName"></autocomplete>
  <autocomplete :items="productCatalog" :form-id="searchCatalog"></autocomplete>
</div>

Thanks!


Solution

  • You could try something like this.

    You'll notice that I'm passing the name of model back and forth a bit, which may seem like a nuisance to manage, but if you were to configure it as part of a v-for loop, it would make it easy to manage.

    Vue.component('custom-input', {
      props: ['value', 'disabled'],
      template: `
        <input
          :disabled="disabled"
          v-bind:value="value"
          v-on:input="$emit('input', $event.target.value)"
        >
      `
    })
    new Vue({
        el:'#app',
        data:{
          first: null,
          second: null,
          active: null
        },
        methods: {
          onChange(e, model){
            this.active = null
            if (e.length > 0) {
              this.active = model
            }
          }
        },
        
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.3/vue.js"></script>
    <div id="app">
      <custom-input v-model="first" :disabled="active !== null && active !== 'first'" @input="onChange($event, 'first')">Foo</custom-input>
      <custom-input v-model="second" :disabled="active !== null && active !== 'second'" @input="onChange($event, 'second')">Bar</custom-input> 
    </div>