0.0.2 • Published 3 years ago

vue-next-typeahead v0.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

A simple component to display a typeahead format with Vue3.

Status

Version js-standard-style Downloads

Demo

Install

npm install vue-next-typeahead

Quick start

Globally

You can import in your main.js file

import App from './App.vue'
import { createApp } from 'vue'
import VueNextTypeahead from 'vue-next-typeahead'

const app = createApp(App)
app.component(VueNextTypeahead)

Locally in any component

import VueNextTypeahead from 'vue-next-typeahead'

export default {
  components: {
    VueNextTypeahead
  }
};

Basic usage

<template>
    <VueNextTypeahead
      v-model:input="inputSearch"
      :api-url="`http://openlibrary.org/search.json${inputSearch ? `?title=${inputSearch}` : ''}`"
      :title="'Encuentra libros que te gusten'"
      :placeholder="'Busca un libro...'"
      :items="results"
      :key-item="'title'"
      :loading-parent-component="loading"
      @response="getResponse"
      @reset="resetComponent()"
      @hit="selectItem" />
</template>

<script>
  export default {
    data () {
        return {
            results: [],
            loading: false,
            inputSearch: '',
            itemSelected: {}
        }
    },
    methods: {
        async getResponse (responseJson) {
            this.loading = true
            const response = await responseJson.json()
            this.results = response.docs
            this.loading = false
        },

        selectItem (item) {
            this.itemSelected = item
        },

        resetComponent () {
            this.results = []
        }
    }
  };
</script>

Props

Property nameTypeDefaultDescription
apiUrlStringURL to invoke the GET with fetch
inputString''Default value of the input, used to make the data-binding.
titleStringnullTitle that appears above the search input.
classNameString''Class to modify component styles.
itemsArray[]Items to be displayed in the results dropdown.
keyItemStringnullKey to display the desired value of the array 'items'.
showTitleBooleantrueDisplays or not the title of the input.
placeholderStringQué necesitas...Placeholder for search input.
noResultsTextStringNo existen resultados para esa búsqueda...Text to be displayed when there are no results in the search.
minCharsNumber2Minimum of values until you start searching, at least it should be 1.
delayNumber500Time in milliseconds for the timeout used in the typing of the input.
loadingParentComponentBooleantrueUsed in case it is necessary to make synchronous operations in the main service response in the component.

Emits

Emit nameDescription
update:inputIn the parent component, you can use v-model:input="var" so that 'var' is automatically updated when you write in the input.
responseFirst response returned by fetch GET.
hitEvent launched when you click on an option of the items that appears in the dropdown.
resetWhen you reset all data within the component.
errorError returned by the GET service.

Development

contributions welcome

Note: Contributions are very welcomed, however is very important to open a new issue using the issue template before you start working on anything, so we can discuss it before hand

Fork the project and enter this commands in your terminal

git clone https://github.com/cristianpoleyJS/vue-next-typeahead.git
cd vue-next-typeahead
npm install
npm run serve

Commitlint

This project follows the commitlint guidelines, with minor changes.

TODO

  • Unit testing with Jest.
  • Web Component: Currently Vue3 does not support the generation of a Web Component, that's why I haven't generated it yet.

License

MIT © cristianpoleyJS