Skip to content

useInputFetch ​

Handles paginated remote data fetching for select-like inputs (autocomplete, combobox, select-scroll). Builds a URL from endpoint + page + search params, appends pages on scroll, and re-fetches on search change.

File: vue/src/js/hooks/useInputFetch.js
Props factory: makeInputFetchProps


Usage ​

js
import { useInputFetch, makeInputFetchProps } from '@/hooks'

const props = defineProps({ ...makeInputFetchProps() })
const {
  elements,
  itemsLoading,
  activePage,
  activeLastPage,
  nextPage,
  getItemsFromApi,
  searchOnInputFetch
} = useInputFetch(props, context)
html
<v-select
  :items="elements"
  :loading="itemsLoading"
  @update:search="searchOnInputFetch"
/>

Props (via makeInputFetchProps) ​

Merges makeSelectProps and makePaginationProps:

PropTypeDescription
endpointStringBase URL to fetch items from
itemValueStringKey used as the option value
itemTitleStringKey used as the option label
multipleBooleanWhether multiple values can be selected
pageNumberStarting page (default 1)
perPageNumberItems per page
searchParamStringQuery param name for search (default 'search')

Returns ​

NameTypeDescription
elementsRef<Array>Accumulated items fetched so far
itemsLoadingRef<Boolean>True while a request is in flight
activePageRef<Number>Current page number
activeLastPageRef<Number>Last page from the API response (-1 means not yet loaded)
nextPageRef<Number>Next page to request
getItemsFromApi() => PromiseFetch the next page and append to elements
searchOnInputFetch(searchVal) => voidReset pagination and re-fetch with a new search term

Pagination behaviour ​

  • Pages are fetched sequentially. getItemsFromApi is a no-op once nextPage > activeLastPage.
  • On the first call, activeLastPage is -1 (sentinel for "not loaded yet"), which forces an initial fetch.
  • When search is non-empty, elements is replaced (not appended) so results always reflect the query.
  • After fetching, if the current modelValue is not yet in elements, the hook calls itself recursively to fetch more pages until the selected value is found.

See Also ​