0.7.0 • Published 4 years ago

vue-quick-search-all-vals v0.7.0

Weekly downloads
15
License
MIT
Repository
github
Last release
4 years ago

Vue Quick Search

A Vue component for adding a search overlay with instant search results to your site.

Vue Quick Search Example

Install

npm i vue-quick-search --save

Usage

import vueQuickSearch from 'vue-quick-search'

export default {
  name: 'wrapper',
  components: {
    'vue-quick-search': vueQuickSearch
  },
  data () {
    return {
      isActive: false
    }
  },
  methods: {
    getResults (url, callback) {
      callback(200, {
        results: [Object, Object],
        total: 2
      })
    }
  }
}
</script>

<template>
  <div>
    <header class="header">
      <button @click.prevent.stop="(isActive = !isActive)" class="btn">
        <span v-if="!isActive">Search</span>
        <span v-else>Close</span>
      </button>
    </header>
    <vue-quick-search
      :is-active="isActive"
      :get-results="getResults"
      :result-limit="0"
      @hide="(isActive = false)">
      <template slot="no-results" slot-scope="props">
        <div>No results available for {{props.searchTerm}}</div>
      </template>
    </vue-quick-search>
  </div>
</template>

Props

keydescriptiondefaultoptions
isActiveIs the search overlay active? Use this to toggle active state.falseBoolean
urlThe URL to call to fetch instant results. This URL is also sent a POST request on form submission unless the hijackSubmit method is used. The \|val\| merge tag is replaced with the search term./search/term/\|val\|String
formActionThe action="" attribute value of the search form./searchString
formInputNameThe name="" attribute value of the search input.qString
formInputTypeThe type="" attribute value of the search input.TextString
formInputLabelThe <label> text for the search input.Search by keyword, style, etc...String
formSubmitTitleThe title="" and aria-label="" attribute values for the form submit button.Fetch search resultsString
filterResultsA method to filter results before rendering them.(result) => resultFunction
filterAJAXUrlA method to mutate the AJAX url before a request is started.(endpoint) => endpointFunction
getResultsA method to get requests from a data source of choice. This method is called whenever the search term changes. If not supplied, a AJAX call will be requested to url.(url, callback) => nanoajax.ajax({url}, callback)Function
resultLimitThe amount of results to show.5Number
exitDelayA time delay before the user navigates away from the page via form submission or a click on a result listing. Helpful if you need to fire tracking events.300Number
hijackSubmitA method called when the form gets submitted. Helpful if you need to send the user to a different location.form => form.submit()Function
decodeResultsA method called when the AJAX request has completed. Use this to decode the AJAX response, returning a {total: Number, results: Array} object. See below Function
The default decodeResults method:
function decodeResults (response) {
  if (typeof response === 'string') {
    response = JSON.parse(response)
  }
  return {
    results: response.results || [],
      total: response['total'] || 0
  }
}

Events

eventdescriptionoutput
exitThe user is about to leave the page (called immediately, irrespective of exitDelay).The current search term
focusA focus event was fired on the form input.Event
blurA blur event was fired on the form input.Event
hideThe overlay should be hidden by changing the isActive prop on the parent component.Event

Slots

<vue-quick-search>
  
  <!-- Props: None -->
  <template slot="submit-button" slot-scope="props">
    <i class="foo">Submit Icon</i>
  </template>
  
  <!-- Props: {item} -->
  <template slot="result" slot-scope="props">
    <img :src="props.item.image" :alt="props.item.title" />
    <p class="result-title">{{props.item.title}}</p>
  </template>
  
  <!-- Props: {searchTerm} -->
  <template slot="no-results" slot-scope="props">
    <div>No results available for {{props.searchTerm}}</div>
  </template>
  
  <!-- Props: None -->
  <template slot="loading" slot-scope="props">
    <p>Loading Results</p>
  </template>
  
  <!-- Props: None -->
  <template slot="form-inputs" slot-scope="props">
    <input type="hidden" name="type" value="foo" />
    <input type="hidden" name="filter" value="bar" />
  </template>
  
  <!-- Props: {activeSearchUrl, resultsCount} -->
  <template slot="results-footer" slot-scope="props">
    <a :href="props.activeSearchUrl">{{props.resultsCount}}</a>
    <span> results returned</span>
  </template>

</vue-quick-search>

CSS

Add the following to your stylesheet for basic styles

Styles are minimal so that they don't interfere with your project-specific system

@import "vue-quick-search/dist/vueQuickSearch.css";
0.7.0

4 years ago

0.6.0

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.1.0

4 years ago

0.3.0

4 years ago

0.0.9

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago