1.1.1 • Published 5 years ago

vue-multiclick v1.1.1

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

Vue-Multiclick

Actions Status

A renderless Vue component for managing list item selection state.

Install

$ npm install vue-multiclick --save

or include the UMD build, hosted by unpkg in a <script> tag.

<script src="//unpkg.com/vue-multiclick" />

Usage

Import and register the component.

import VueMulticlick from 'vue-multiclick'

export default {
  ...
  components: {
    VueMulticlick
  }
}

In your template, wrap a single element in the VueMulticlick component. You must pass an array of objects to the items prop, as well as uid prop, which is the key in your item objects that makes them unique.

<div>
  <VueMulticlick :items="items" uid="id">
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}</li>
    </ul>
  </VueMulticlick>
</div>

Setting selection states

The easiest way to set item selection states is through the itemClicked method. This method will automatically pick up on any necessary event modifiers (such as the meta/ctrl or shift key being pressed), and sets the selection items appropriately.

<div>
  <VueMulticlick :items="items" uid="id" v-slot="{ itemClicked }">
    <ul>
      <li v-for="item in items" :key="item.id" @click="itemClicked">{{ item.name }}</li>
    </ul>
  </VueMulticlick>
</div>

Retrieving selection states

Of course just setting selection states is often not enough, and you'll want to visually change elements that are currently selected. You can use the itemIsSelected function to check if a given item is currently selected.

<div>
  <VueMulticlick :items="items" uid="id" v-slot="{ itemClicked, itemIsSelected }">
    <ul>
      <li v-for="item in items" :key="item.id" :class="{ 'is-selected': itemIsSelected(item) }" @click="itemClicked">
        {{ item.name }}
      </li>
    </ul>
  </VueMulticlick>
</div>
<style>
  .is-selected {
    background-color: blue;
    color: #fff;
  }
</style>

Available properties

NameDescriptionReturn Type
selectedItemsReturns all currently selected items.Array
selectedIndexesReturns the indexes of all selected itemsArray
lastSelectedItemReturns the last selected item.Object
lastSelectedIndexReturns the index of the last selected item.Number

Available methods

NameDescriptionParamsReturn Type
itemClickedSets the selection state based on the event modifiers if they exist.item: Objectevent: Objectnull
setSelectedItemSets the current selection to a single item.item: Objectnull
setSelectedItemsSets the current selection to the items passed in.items: Arraynull
appendToSelectionPushes an item to the selection listitem: Objectnull
removeFromSelectionRemoves an item from the selection list.item: Objectnull
getItemIndexReturns the index of a given itemitem: ObjectNumber
itemIsSelectedReturns whether the given item is currently selected or not.item: ObjectBoolean
selectAllPushes all items to the selection list.N/Anull
selectNoneRemoves all items from the selection list.N/Anull
getItemsFromRangeRetrieves all items between a given range.start: Numberend: NumberArray
setSelectedItemsFromLastSelectedSets the selected items to a range based off the last selected itemitem: Objectnull

Development

# To run the example
$ npm run example

# To run the tests
$ npm test

# To publish the dist file
$ npm run build

License

MIT © Collin Henderson

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago