input field value keeps getting reset @input?

Viewed 111

I have created an custom reusable autocomplete component.The issue i am facing is whenever i start to type anything into the fields(@input) the value in the input field gets reset. I feel it has something to do within the code written in the debounce function but i am not sure.Plz help?

main.js

Vue.component('AutoComplete', {
  props: ['list','value','title'],
  data() {
    return {
      input: '',
      
    }
  },
  template: `<template>
  <div class="autocomplete">
    <input style="font-size: 12pt; height: 36px; width:1800px; " type="text"  v-model="input"  @input="handleInput"/>
     <ul v-if="input" >
      <li v-for="(item, i) in list" :key="i" @click="setInput(item)" >
         
          <template v-if="title!='manager'">
          <div class="container">
            <p>
              <b>ID:</b>
              {{item.id}}
            </p>
            <p>
              <b>Description:</b>
              {{item.description}}
            </p>
          </div>
        </template>
         <template v-else>
        <div class="container">
            <p>
              <b>ID:</b>
              {{item.id}}
            </p>
            <p>
              <b>First Name:</b>
              {{item.firstName}}
            </p>
            <p>
              <b>Last Name:</b>
              {{item.lastName}}
            </p>
          </div>
        </template>
           </li>
    </ul>
  </div>
</template>`,
  methods: {
    handleInput(e) {
      console.log('inside handleInput')
      this.$emit('input', e.target.value)
    },

    setInput(value) {
      console.log('inside setInput')
      this.input = value
      this.$emit('click', value)
    }
  },
  watch: {
    $props: {
      immediate: true,
      deep: true,
      handler(newValue, oldValue) {
        console.log('new value is'+newValue)
        console.log('old value is'+oldValue)
        console.log('value inside handler'+this.value)
        console.log('list inside handler'+this.list)
        console.log('title inside handler'+this.title)
       this.input=this.value

      }
    }
    
}
})

Currently i have called this component from JobDetail.vue page like this-

JobDetail.vue

<template>
 <b-field label="Custom Action">
      <AutoComplete v-on:input="getAsyncDataAction" v-on:click="(option) => {updateRowValue('records', props.index, 'action', option.id); props.row.action = option.id}" :value="props.row.action" :list="dataAction" title='action' >
      </AutoComplete>
</b-field> 
</template>

<script>
import { viewMixin } from "../viewMixin.js";
import debounce from "lodash/debounce";
import api from "../store/api";
const ViewName = "JobDetail";
export default {
  name: "JobDetail",
  mixins: [viewMixin(ViewName)],  
  data() {
    return {    
      dataAction: [],
      isFetching: false      
    };
  },

  methods: {

 getAsyncDataAction: debounce(function(name) {
      if (!name.length) {
        this.dataAction = [];
        return;
      }
      this.isFetching = true;
      api
        .getSearchData(`/action/?query=${name}`)
        .then(response => {
          this.dataAction = [];
          response.forEach(item => {
            this.dataAction.push(item);
          });
        })
        .catch(error => {
          this.dataAction = [];
          throw error;
        })
        .finally(() => {
          this.isFetching = false;
        });
    }, 500)
}
};
</script>

viewmixin.js


computed: {
            viewData() {
                return this.$store.getters.getViewData(viewName)
            },
            objectData() {
                return this.$store.getters.getApiData(this.viewData.api_id).data
            },
            sessionData() {
                return this.$store.getters.getSessionData()
            },
            isLoading() {
                return this.$store.getters.getApiData(this.viewData.api_id).isLoading
            },
            newRecord() {
                return this.$route.params.id === null;
            }            
        },

I don't understand why the input fields value keeps resetting @input. Please help and also let me know if this is the correct approach?

0 Answers
Related