Accessible Select Dropdown Component For Vue.js

Description:

A custom, accessible, searchable select dropdown component that supports multiple selection.

How to use it:

1. Import the component into your Vue project.

import { 
  // single selection
  SelectSingle, 
  // multiple selection
  SelectMulti, 
  // core
  SelectOption 
} from '@politico/vue-accessible-selects'

2. Add the single & multiple select components to the template.

<SelectSingle
  v-model="selectSingleValue"
  :options="myOptions"
  label="Single Select"
  :labelIsVisible="true"
  @select="handleSelectEvent"
/>
<SelectMulti
  v-model="selectMultiValues"
  :options="myOptions"
  label="Multiple Select"
  :labelIsVisible="true"
  placeholder="Placeholder"
  @select="handleSelectEvent"
  @remove="handleRemoveEvent"
  @searchChange="handleSearchChangeEvent"
/>

3. Register the component and define the data to be presented in the select.

const myOptions: SelectOption[] = [{
  label: 'Option 1',
  value: 'option1'
}, {
  label: 'Option 2',
  value: 'option2'
}, {
  label: 'Option 3',
  value: 'option3'
}]
export default Vue.extend({
  components: { SelectSingle, SelectMulti },
  data() {
    return {
      myOptions,
      selectSingleValue: {} as SelectOption,
      selectMultiValues: [] as SelectOption[]
    }
  },
  methods: {
    handleSelectEvent(selectedValue: SelectOption) {
      // do something
    },
    handleRemoveEvent(selectedValue: SelectOption) {
      // do something
    },
    handleSearchChangeEvent(inputValue: string) {
      // do something
    }
  }
})

4. All default component props.

disabled: {
  type: Boolean,
  default: false
},
label: {
  type: String,
  required: true
},
loading: {
  type: Boolean,
  default: false
},
options: {
  type: Array as PropType<SelectOption[]>,
  required: true
},
labelIsVisible: {
  type: Boolean,
  required: false,
  default: true
},
placeholder: {
  type: String,
  required: false,
  default: () => null
},
/**
 * When using a slot to display each option in the select,
 * you'll want to pass in a way for the select to *search* for those options as a user types,
 * in order to accurately filter the available options
 * @example
 * ```
 * :optionLabelForSearching="a => a.label + '-' + a.value"
 * ```
 */
optionLabelForSearching: {
  type: Function as PropType<(option: SelectOption) => string>,
  required: false,
  default: null
},
displayPillsBelowInput: {
  type: Boolean,
  default: false
},
/**
 * By default, the list will be empty when either no options are passed in,
 * or a user has typed a string that doesn't match any of the options. 
 * If you'd like to display a messsage instead when that occurs, pass it in here
 */
noResultsMessage: {
  type: String,
  default: ''
},
/** Generally, there's no need to set this via a prop - it will be set automatically when using v-model */
values: {
  type: Array as PropType<SelectOption[]>,
  required: false,
  default: () => []
}

Preview:

Accessible Select Dropdown Component For Vue.js

Changelog:

v0.8.2 (11/22/2020)

  • Fix changing selected option on click

v0.8.1 (11/19/2020)

  • SelectSingle – add option-selected class to selected option

v0.8.0 (11/11/2020)

  • Add a watcher on loading to invoke opening the filter options

v0.7.1 (11/07/2020)

  • Updated

v0.7.0 (10/29/2020)

  • Add loading prop. add icons slot

v0.6.0 (10/28/2020)

  • Add optional no results msg

Download Details:

Author: politico

Live Demo: https://vue-accessible-selects.netlify.app/

Download Link: https://github.com/politico/vue-accessible-selects/archive/main.zip

Official Website: https://github.com/politico/vue-accessible-selects

Install & Download:

# NPM
$ npm i @politico/vue-accessible-components

You Might Be Interested In:

Add Comment