Skip to content

Autocomplete

WARNING

Experimental

Introduction

The Autocomplete component is a flexible and customizable dropdown select component built using Vue 3 and the Headless UI library. It supports both single and multiple selections, provides slot customization, and offers various props for enhanced functionality.

Storybook

Props

PropTypeDefaultDescription
modelValueT | T[] | undefined-The value bound to the component.
itemsT[]-An array of items to be displayed in the dropdown.
multiplebooleanfalseAllows multiple selections if set to true.
itemTextstring'text'The key for the text to be displayed from the item.
itemValuestring'value'The key for the value of the item.
placeholderstring'Search...'Placeholder text for the input.
labelstring-Label for the autocomplete.
transitionstring'dropdown'Transition animation name.
errorbooleanfalseIf set to true, indicates there's an error.
disabledbooleanfalseIf set to true, the component will be disabled.
clearablebooleanfalseIf set to true, adds the option to clear the selected value.
emptyTextstring'No results.'Text displayed when there are no results to show.
searchBystring-Key by which the list will be filtered. If not provided, will fall back to itemText or itemValue.
displayValue(item: any) => string-A function to custom format the displayed value.
selectionItemPropsInstanceType<typeof VBadge>['$props']-Props for the selected item badge.
readonlybooleanfalseIf set to true, the component will be in readonly mode.
shadowbooleanfalseIf set to true, a shadow will be applied to the component.
hintstring-A hint message to be displayed below the component.
errorMessagestring-Error message to be displayed when error is true.
hideErrorbooleanfalseIf set to true, error message will be hidden even if error is true.

Slots

Slot NameProps ProvidedDescription
default{}Default slot for additional content.
hint{}Slot for custom hint message rendering.
selection{ selected, multiple, itemValue, itemText, selectionItemProps }Slot for custom selected items rendering.
selection-item{ item, idx, itemValue, itemText, remove }Slot for custom rendering of each selected item.
item{ item, active, selected, itemValue, itemText }Slot for custom rendering of each dropdown item.

Events

Event NamePayloadDescription
update:modelValueModelValueEmits the current selected value(s).
clear-Emits when the selected value(s) is cleared.

Usage Example

vue
<script setup lang="ts">
import {ref} from 'vue';
import {Autocomplete} from '@morpheme/autocomplete';

const selectedValue = ref();
const items = [
  {text: 'Option 1', value: 1},
  {text: 'Option 2', value: 2},
];
</script>

<template>
  <Autocomplete
    :items="items"
    v-model="selectedValue"
    label="Choose an option"
  />
</template>
<script setup lang="ts">
import {ref} from 'vue';
import {Autocomplete} from '@morpheme/autocomplete';

const selectedValue = ref();
const items = [
  {text: 'Option 1', value: 1},
  {text: 'Option 2', value: 2},
];
</script>

<template>
  <Autocomplete
    :items="items"
    v-model="selectedValue"
    label="Choose an option"
  />
</template>