0.0.8 • Published 6 years ago

vue-tag-autocomplete v0.0.8

Weekly downloads
9
License
MIT
Repository
github
Last release
6 years ago

Vue Tag Input

Travis npm Style Guide

Customizable Vue component for tag input, which support autocomplete.

Getting Started

Install

Recommand intall from CLI:

npm install --save vue-tag-autocomplete // OR
yarn add vue-tag-autocomplete

If you want to include script directly:

<!-- Load Script --> 
<script src="https://cdn.jsdelivr.net/npm/vue-tag-autocomplete/dist/umd/vue-tag-input.js"></script>

<!-- Load Style --> 
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/vue-tag-autocomplete/dist/umd/vue-tag-input.css">

Quick Example

<template>
  <VueTagInput v-model="tags" :quick-mode="true"></VueTagInput>
</template>

<script>
import VueTagInput from 'vue-tag-input'

export default {
  data() {
    return {
      tags: [
        'Africa',
        'Taiwan'
      ]
    }
  },
  components: {
    VueTagInput
  }
}
</script>

Usage

Props

tags - {Array<Object|String>} (required)

An array of tag objects. Each tag need an id and a text property which is used to display. Otherwise, you could just use array of strings directly. This component would generate id by index and string automatically.

// Recommended: Pass Object 
[
  {id: 1, text: 'Apple'},
  {id: 2, text: 'Banana'},
  ...
]
// Quick: Pass String 
[
  'Apple',
  'Banana',
  ...
]
//

suggestions - {Array<Object|String>}

Default: []
An array of suggestions that are used as basis for showing autocomplete. Each tag need an id and a text property which is used to display. Otherwise, you could just use array of strings directly. This component would generate id by index and string automatically.

// Recommended: Pass Object 
[
  {id: 1, text: 'Apple'},
  {id: 2, text: 'Banana'},
  ...
]
// Quick: Pass String 
[
  'Apple',
  'Banana',
  ...
]
//

quickMode - {Boolean}

Default: false
Allow developers to use v-model, rather than handling add and delete event by yourselves. In quick mode, new tag will append to array as string. If you want to control how new tag be added, you could listen change event. Otherwise, you should listen add and delete event instead.

<VueTagInput
  :quick-mode="true"
  v-model="tags"
/>
<!-- Which is equal to -->
<VueTagInput
  :quick-mode="true"
  :tags="tags"
  @change="val = { tags = val }"
/>

id - {String}

Defaults to ''
id attribute on <input/>. Recommanded to assign id for identity and associate with label.

<label for="email"></label>
<VueTagInput :id="email" />

placeholder - {String}

Defaults to 'Add new tags'
The placeholder shown for the input.

delimiters - {Array}

Default: 9, 13 (Tab and return keys)
Array of integers matching keyboard event keyCode values. When a corresponding key is pressed, the preceding string is finalised as tag.

delimiterChars - {Array}

Default: ','
Array of characters matching keyboard event key values. This is useful when needing to support a specific character irrespective of the keyboard layout. Note, that this list is separate from the one specified by the delimiters option, so you'll need to set the value there to [], if you wish to disable those keys.

onlyFromSuggestions - {Boolean}

Default: false
If set to true, it will be possible to add new items only from the autocomplete dropdown.

allowDuplicates - {Boolean}

Default: false
Allows users to add duplicated tags. If it's false, the duplicated tag would play animation with errorAninmatedClass to hint the user.

addOnBlur - {Boolean}

Default: false
Add tag automatically when input field blur.

maxInputLength - {Number}

Default: Infinity
Maximum input length.

maxSuggestionsLength - {Number}

Default: Infinity
Maximum number of suggestions to display.

errorAninmatedClass - {String}

Default: error The animation class would add on duplicated tag element when allowDuplicates is false. The default animation is shaking for 0.25s.

tagStyle - {Object|Function}

Default: {} The style object would be applied on the tag. Besides passing plain object, you can pass a function which return style object to assign conditional style. The function would recieve each element in tags as params. See the example below:

<!-- Conditional highlight: Add highlight property in each tag to determine the background color -->
<template>
  <VueTagInput
    :tags="tags"
    :tagStyle="tagComputedStyle"
  />
</template>

<script>
export default {
  data() {
    return {
      tags: [{
        id: 1,
        text: 'javascript',
        highlight: true
      }, {
        id: 2,
        text: 'css',
        highlight: false
      }, {
        id: 3,
        text: 'html',
        highlight: true
      }]
    }
  },
  methods: {
    tagComputedStyle(item) {
      // If item has highlight is true, the background would be aquamarine color. Otherwise it's grey.
      return item.highlight ? { backgroundColor: 'Aquamarine' } : { backgroundColor: 'grey' }
    }
  }
}
</script>

preventDefaultOnEnter - {Boolean}

Default: false
Prevent default when user tap ENTER to avoid the form submission.

Events

add

Params: tag would be added - {String}
Emitted when a tag had be added.

<template>
  <VueTagInput :tags="tags" @add="onAdd"></VueTagInput>
</template>

<script>
export default {
  data() {
    return { tags: [] }
  },
  methods: {
    onAddition(tag) {
      this.tags.push(tag)
    }
  }
}
</script>

delete

Params: index of tag would be removed - {Number}
Emitted when a tag had be added.

<template>
  <VueTagInput :tags="tags" @delete="onDelete"></VueTagInput>
</template>

<script>
export default {
  data() {
    return { tags: [] }
  },
  methods: {
    onDelete(index) {
      this.tags.splice(index, 1)
    }
  }
}
</script>  

change

Params: the array of updated tags - {Array}
Emitted when a tag had be added or deteled, which only be available in quickMode. The new tag will be appended as string into array. If you need to control new tag in the array, you could modify the last item of array and pass the whole array to data in the listener.

export default {
  data() {
    return { tags: [] }
  },
  methods: {
    onInput(newTags) {
      // newTags will contained the new array of updated tags. 
      const id = newTags.length - 1
      const text = newTags.pop()
      this.tags = [...newTags, {
        id,
        text
      }]
    }
  }
}

inputChange

Parmas: the current input data - {String}
Emitted when input field is changed. You could query autocomplete data here.

export default {
  data() {
    return { 
      tags: [],
      suggestions: []
    }
  },
  methods: {
    onInputChange(query) {
      fetch(`https://your.data.source?query=${query}`, (data) => {
        this.suggestions = data
      })
    }
  }
}

focus

Emitted when input is focused

blur

Emitted when input is Blur

Style

class names

Development

# Run development enviroment
npm start 

# Bundle test file and test it with Jest
npm run test

# Bundle test file and update test snapshot 
npm run test:update

# Examine code style with eslint
npm run lint

# Fix eslint automatically
npm run lint:fix 

# Bundle component module 
npm run build

Author

Lucien Lee

Contributor

Julien Croain

License

MIT

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago