@querry-kit/nuxt GitHub

Autocomplete

The autocomplete composable keeps selected resources loaded independently of the active search result.

UseAutocompleteOptions

interface UseAutocompleteOptions<TItem extends Record<string, unknown>> {
  api: AxiosInstance;
  endpoint: string;
  query?: MaybeRef<QueryParameters | undefined>;
  currentValue?: MaybeRef<string | number | Array<string | number> | null | undefined>;
  identityKey?: keyof TItem & string;
  itemDisabled?: (item: TItem) => boolean;
  immediate?: boolean;
}
PropertyRequiredPurpose
apiYesAxios client created by the application.
endpointYesResource endpoint relative to the configured API version.
queryNoReactive query for search results.
currentValueNoA selected identity or identities to load even when absent from the search query.
identityKeyNoProperty used for selection and deduplication; defaults to id.
itemDisabledNoAdds a derived disabled property to returned items.
immediateNoSet to false to call initialize() manually; the default is true.

useAutocomplete

function useAutocomplete<TItem extends Record<string, unknown>>(
  options: UseAutocompleteOptions<TItem>,
): AutocompleteState<TItem>;

Loads selected identities and search results independently, then returns selected items first and removes duplicate identities. Older responses never replace a newer result of the same request kind.

const users = useAutocomplete<User>({
  api,
  endpoint: 'users',
  query: computed(() => ({ where: { name: { contains: search.value } } })),
  currentValue: selectedUserId,
});
Returned valueMeaning
itemsComputed selected items followed by unique query items. itemDisabled is applied only here.
currentValueItems, queryItemsRaw cached results of the two requests.
loading, errorCombined pending state and the latest request error.
loadCurrentItems()Refreshes selected identities only.
loadItems()Refreshes search results only.
initialize() / refresh()Loads both groups concurrently.