InputSearch.vue component is a flexible and dynamic search input field that supports real-time searching with debounced API calls. It provides customization options for inline or labeled search layouts, and it supports displaying search results with scoped slots for templating.
Features
Event Emission
Emits a
search event with the query when the user types, enabling integration with external search logic.Debounced Search
Minimizes API calls by debouncing the search function.
Customizable Results Display
Supports rendering custom result items using a scoped
result slot.Flexible Layouts
Can be displayed inline or as a standalone search box with a label.
Usage
Basic Example
Props
Two-way binding for the search query input.
Label text displayed above the search input.
Placeholder text inside the search input.
Minimum number of characters required to trigger the
search event.Whether the search box is displayed inline with other elements.
Array of search result objects to display in the result list.
Events
Triggered when the user types in the search box after the required character length is met.
Triggered when the user blurs the search input but only when the search box is not open.
Triggered when the user closes the search input box.
Slots
Slot for placing a custom icon on the left side of the search input.
<template #iconLeft></template>Scoped slot for rendering custom search result items. Provides
result as context. <template #result="{ result, state }"></template>Scoped slot for rendering custom no results message. Provides
result as context. <template #result-none="{ state }"></template>Scoped slot for rendering a custom results list. Provides
result as context. <template #results="{ state, results }"></template>Scoped slot for rendering before the result list. Provides
result as context. <template #top="{ state }"></template>Scoped slot for rendering after the result list. Provides
result as context. <template #bottom="{ state }"></template>Example Use Cases
Inline Search with Custom Results
Close Search After Click
You can close the search box settingstate.open to false. The state is a result slot template parameter.
About the Debounce System
The debounce system ensures that the search event is not triggered for every keystroke, but rather after a specified delay once the user stops typing. This minimizes performance overhead and reduces the number of unnecessary API calls or search function executions. The debounce function wraps the searchBox function, delaying its execution by500 milliseconds after the last keypress.
Purpose:
- Prevents triggering the search logic too frequently while the user is typing.
- Ensures smooth and efficient operation, especially for remote or API-based search systems.
Notes
- Debounced Searching: The component includes a debounced search function, reducing unnecessary API calls or computations.
- Custom Result Rendering: Use the
resultslot to fully control how search results are displayed. - Flexible Layouts: Adjust the layout using the
inlineprop to fit various design requirements.