TerryZ / v-suggest

A Vue2 plugin for input content suggestions, support using keyboard to navigate and quick pick, it make use experience like search engine input element
https://terryz.github.io/vue/#/suggest
MIT License
75 stars 16 forks source link

New data doesn't rerender when already focused. Bug with async data. #14

Open JoshMoreno opened 4 years ago

JoshMoreno commented 4 years ago

I'm populating the passing a list of suggestions to the data prop which get set in an async function.

Basically, I have a component that watches search, hits the api, and sets the suggestions which get passed to your component. But your component doesn't update list in your component until you re-trigger a focus event.

Component that uses v-suggest

<template>
    <v-suggest
            v-model="search"
            :data="this.suggestions"
    ></v-suggest>
</template>

<script>
    import { Suggest } from 'v-suggest'
    import axios from 'axios'

    export default {
        components: {
            'v-suggest': CustomVSuggest
        },
        data() {
            return {
                search: '',
                suggestions: [],
            }
        },
        methods: {
            getSuggestions: function() {
                return axios.get('/api/search/' + this.search)
                        .then(response => {
                            this.suggestions = response.data
                        })
            },
        },
        watch: {
            search: function(newValue, oldValue) {
                this.getSuggestions()
            }
        }
    }
</script>

The workaround I'm using right now is creating a component that extends yours and just calls this.focus() whenever data changes. And then of course using this in place of the v-suggest component.

Workaround component

<script>
    import { Suggest } from 'v-suggest'

    export default {
        extends: Suggest,
        watch: {
            data(newValue, oldValue) {
                this.focus()
            }
        }
    }
</script>

I think you can probably just add this to your methods.js

watch: {
    data(newValue, oldValue) {
        this.focus()
    }
}