0.1.21 • Published 5 years ago

vueto-complete v0.1.21

Weekly downloads
10
License
MIT
Repository
github
Last release
5 years ago

vueto-complete

vue autoComplete component

Index

Features

  • Supports full control over functionality and display.
  • Supports regular lists as well as AJAX fetching.
  • Supports grouping of results.
  • Supports slots for custom rendering and formatters for custom formatting.
  • Supports full control over styling.
  • Supports full control over filtering and sorting results.
  • Supports multiple keys to search against.
  • Supports keyboard accessibility.
  • Fires events for all its lifesycle hooks.

Installation

npm install --save vueto-complete

or

yarn add vueto-complete

Examples

<template>
  <div>
    <VuetoComplete
        v-model="query"
        :auto-complete-list="list"
        :styles="styles"
    >
    </VuetoComplete>
  </div>
</template>

<script>
  import VuetoComplete from 'vueto-complete';
  
  const styles = {
    container: {
      maxWidth: '500px',
    },
    input: {
      border: '1px solid gray',
    },
    focusedInput: {
      background: '#e0e0e0',
    },
  };
  
  export default {
    name: 'app',
    data: () => ({
      query: '',
      styles,
      list: [
        'Cairo, Egypt',
        'Cairo, Thomasville, United States',
        'Cairo, Southwest Illinois, United States',
        'Giza, Cairo, Egypt',
        'Cairo, Central West Virginia, United States',
        'Cairo Montenotte, Savona - Riviera Delle Palme, Italy',
      ],
    }),
    components: {
      VuetoComplete,
    }
  };
</script>
<template>
  <div>
    <VuetoComplete
        :fetch-handler="getTodos"
        display-key="title"
        :filter-handler="getCompleted"
        @select="onSelect"
    >
    </VuetoComplete>
  </div>
</template>

<script>
  import VuetoComplete from 'vueto-complete';
  
  export default {
    name: 'app',
    methods: {
      /**
       * get todos
       *
       * @param {String} query
       * @returns {Promise}
       */
      getTodos(query) {
        return fetch('https://jsonplaceholder.typicode.com/todos')
          .then(response => response.json());
      },
      
      /**
       * filter todos by completed
       * @param {Array<object>} todos
       * @return {Array<object>} todos
       */
      getCompleted(todos) {
        return todos.filter(({ completed }) => completed);
      },
  
      /**
       * handle select
       * @param {Object} todo
       */
      onSelect(todo) {
        console.log(todo);
      }
    },
    components: {
      VuetoComplete,
    },
  };
</script>
<template>
  <div>
    <VuetoComplete
        :fetch-handler="getPosts"
        display-key="title"
        :search-keys="['title', 'body']"
        :debounce-time="200"
        :min-chars-to-auto-complete="2"
        :max-results-to-display="5"
        @select="onSelect"
    >
      <template slot="item-icon" slot-scope="{ item }">
        <i :class="`post-icon-${item.id}`"></i>
      </template>
    </VuetoComplete>
  </div>
</template>

<script>
  import VuetoComplete from 'vueto-complete';
  
  export default {
    name: 'app',
    methods: {
      /**
       * get posts
       *
       * @param {String} query
       * @returns {Promise}
       */
      getPosts(query) {
        return fetch('https://jsonplaceholder.typicode.com/posts')
          .then(response => response.json());
      },
      
      /**
       * handle select
       * @param {Object} post
       */
      onSelect(post) {
        console.log(post);
      }
    },
    components: {
      VuetoComplete,
    },
  };
</script>

Props

PropTypeDefaultRequiredDescription
autoCompleteListArrayempty array only when no fetchHandler providedautoCompleteList is required only if no fetchHandler provided, it can be array of strings or objects
fetchHandlerFunctionno defaultonly when no autoCompleteList providedfetchHandler for fetching AJAX results, it's a function returns a promise resolves to a list, it's required only if no autoCompleteList provided.
displayKeyStringtitleonly when the results are a list of objectsdisplayKey is the key that will be displayed in the list, it's required only if the list is array of objects and you can override how you display the results with resultsDisplayFormatHandler prop
searchKeysArraydisplayKeyNoyou can provide multiple keys from the result object to search against while autoCompleting
sortHandlerFunctionno defaultNoa function to sort the autoCompleted results
filterHandlerFunctionno defaultNoa function to filter the autoCompleted results
resultsDisplayFormatHandlerFunctionno defaultNoa function to format how the results get displayed
selectedItemFormatHandlerFunctionno defaultNoa function to format how the selected result get displayed on the autoComplete input
minCharsToAutoCompleteNumber1Nothe minimum characters to start autoComplete or fetch
maxResultsToDisplayNumberInfinityNothe maximum results to display
debounceTimeNumber300 msNodebounce time for the input event while fetching results
highlightMatchedBooleantrueNohighlight matched words as the query
showLoadingIconBooleantrueNoshow loading icon while fetching results
fetchOnFocusBooleanfalseNofetch results with the current query on input focus
placeholderStringempty stringNoautoComplete input placeholder
noResultsMessageBooleanNo results foundNothe message to display when no results matches the current query
showNoResultsMessageStringtrueNoshow a message when no results matches the current query
testIdStringempty stringNoid to define a data attribute data-test-id for each element in the component for automation test
emptyResultsOnEmptyQueryBooleantrueNoempty the results array when the input query get empty
highlightResultsBooleantrueNohighlight results with hovering over it or using keyboard
useCategoriesBooleanfalseNocategorise results
categoryKeyStringno defaultonly when useCategories is truethe key to categorise with
categoryLabelStringno defaultNothe label to show for the category, if it's not provided the categoryKey prop will be used as a label
initialValueStringempty string ' 'Noinitial value for the input query
stylesObjectempty object { }Nostyles object to style each element in the component in all its states

Slots

NameScopeDescription
input-iconno scopeit renders icon for the input
input-labelno scopeit renders label for the input
itemcurrent item objectit renders the result item
item-iconcurrent item objectit renders the result item icon
item-captioncurrent item objectit renders the result item caption
loading-iconno scopeit renders the input loading icon
no-resultsno scopeit renders the no results message

Events

NamePayloadDescription
inputno payloadfired on input change, you can use v-model on the component
startedFetchingno payloadfired before fetch start
fetchSuccessresults listfired on fetch success
fetchErrorerrorfired on fetch error
finishedFetchingno payloadfired after fetch resolve/reject
selectselected item objectfired on selecting autoComplete item
inputFocusno payloadfired on focusing the input
inputBlurno payloadfired on bluring the input
highlightItemitem indexfired on highlighting new item with hover/keyboard
noResultsFoundno payloadfired on when no results found

Styling

Keydescription
containerto style the component container
inputto style the input
focusedInputto style the input in the focus state
inputWrapperto style the input wrapper
focusedInputWrapperto style the input wrapper in the focus state
inputLabelto style the input label
focusedInputLabelto style the input label in the focus state
resultsListto style the results list
resultItemto style the result item
highlightedItemto style the result item in the highlight state
matchedWordsto style the matched words
highlightedMatchedWordsto style the matched words in the highlight state
categoryLabelto style the category label

LICENSE

MIT

0.1.21

5 years ago

0.1.20

5 years ago

0.1.19

5 years ago

0.1.18

5 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.4

5 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago