advanced forms
Tailwind CSS Combo Box
The ComboBox JavaScript plugin improves user experience by dynamically suggesting options as users type into an input field.
Class Name | Type | Description |
---|---|---|
dropdown-item | Modifier | Styles applied to individual dropdown items. |
combo-box-active:{tw-utility-class} | Modifier | Modifies tailwind classes when the combo box is active. |
combo-box-selected:{tw-utility-class} | Modifier | Modifies tailwind classes when the combo box is selected. |
combo-box-tab-active:{tw-utility-class} | Modifier | Modifies tailwind classes when the combo box tab is active. |
combo-box-has-value::{tw-utility-class} | Modifier | A modifier that enables you to apply Tailwind classes when the SearchBox’s input field contains a value. |
The code snippet outlines a ComboBox with specific data
attributes to manage its behavior. The outer <div>
with data-combo-box
signifies that this section is the main ComboBox component. Within it, an <input>
field with data-combo-box-input
allows users to type text to get suggestions.
An accompanying icon
element with data-combo-box-toggle
acts as a button to open or close the dropdown menu, indicating it has a dropdown function. The dropdown menu itself is an <div>
with data-combo-box-output
, which contains several suggestion items. These individual suggestion items are <div>
elements with data-combo-box-output-item
. Each item has a data-combo-box-search-text
attribute that holds the text displayed for that option in the dropdown.
Below example shows basic use of combo-box.
<div class="relative max-w-sm" data-combo-box="">
<div class="relative">
<input class="input" type="text" value="India" role="combobox" aria-expanded="false" data-combo-box-input="" aria-label="Default combobox" />
<span class="icon-[tabler--caret-up-down] text-base-content/90 absolute end-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" data-combo-box-toggle="" ></span>
</div>
<div class="bg-base-100 vertical-scrollbar rounded-scrollbar rounded-box absolute z-50 max-h-44 w-full space-y-0.5 p-2 shadow-lg" style="display: none" data-combo-box-output="" >
<div class="dropdown-item combo-box-selected:active" tabindex="0" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="Venezuela" data-combo-box-value="">Venezuela</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
<div class="dropdown-item combo-box-selected:active" tabindex="1" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="Papua New Guinea" data-combo-box-value="">Papua New Guinea</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
<div class="dropdown-item combo-box-selected:active" tabindex="2" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="South Africa " data-combo-box-value="">South Africa</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
<div class="dropdown-item combo-box-selected:active" tabindex="3" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="Honduras" data-combo-box-value="">Honduras</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
<div class="dropdown-item combo-box-selected:active" tabindex="4" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="India" data-combo-box-value="">India</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
<div class="dropdown-item combo-box-selected:active" tabindex="5" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="El Salvador" data-combo-box-value="">El Salvador</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
</div>
</div>
A JSON-based ComboBox example.
A ComboBox, a dropdown component allowing users to type for suggestions, is being set up. The root div
includes an attribute apiUrl
, indicating the ComboBox where to retrieve data.
In this instance, it’s set to "https://freetestapi.com/api/v1/countries"
, used to obtain a list of countries.
Another attribute, outputItemTemplate
, defines the layout for each item within the dropdown menu. It comprises HTML structure incorporating data-combo-box-output-item
to represent individual dropdown items. Each item includes a section for the name (utilizing data-combo-box-output-item-field="name"
) and a span for a checkmark icon, visible upon item selection.
<div class="relative max-w-sm"
data-combo-box='{
"apiUrl": "https://freetestapi.com/api/v1/countries",
"outputItemTemplate": "<div class=\"dropdown-item combo-box-selected:active\" data-combo-box-output-item><div class=\"flex justify-between items-center w-full\"><div data-combo-box-output-item-field=\"name\" data-combo-box-search-text data-combo-box-value></div><span class=\"icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0\"></span></div></div>"
}' >
<div class="relative">
<input class="input" type="text" value="India" role="combobox" aria-expanded="false" data-combo-box-input="" aria-label="Json-based combobox" />
<span class="icon-[tabler--caret-up-down] text-base-content/90 absolute end-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" data-combo-box-toggle="" ></span>
</div>
<div class="bg-base-100 vertical-scrollbar rounded-scrollbar rounded-box absolute z-50 max-h-44 w-full space-y-0.5 p-2 shadow-lg" style="display: none" data-combo-box-output="" ></div>
</div>
:apiSearchQuery
serves as a property or parameter containing the key name utilized for searching within a dataset when making API calls. Usually, it’s combined with user-entered text to generate a query string, which is then appended to the base API endpoint (apiUrl
), resulting in the complete URL required to fetch the data.
:apiQuery
represents additional query parameters for the API request, such as limit
. :outputEmptyTemplate
enables you to specify the content to display when no matching options are available.
The following example demonstrates the usage of search and limit parameters in a combo box.
<div class="relative max-w-sm"
data-combo-box='{
"apiUrl": "https://freetestapi.com/api/v1/countries",
"apiQuery": "limit=7",
"apiSearchQuery": "search",
"outputEmptyTemplate": "<div class=\"dropdown-item\">No countries found...</div>",
"outputItemTemplate": "<div class=\"dropdown-item combo-box-selected:active\" data-combo-box-output-item><div class=\"flex justify-between items-center w-full\"><div data-combo-box-output-item-field=\"name\" data-combo-box-search-text data-combo-box-value></div><span class=\"icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0\"></span></div></div>"
}' >
<div class="relative">
<input class="input" type="text" value="India" role="combobox" aria-expanded="false" data-combo-box-input="" aria-label="Parameters in combobox" />
<span class="icon-[tabler--caret-up-down] text-base-content/90 absolute end-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" data-combo-box-toggle="" ></span>
</div>
<div class="bg-base-100 vertical-scrollbar rounded-scrollbar rounded-box absolute z-50 max-h-44 w-full space-y-0.5 p-2 shadow-lg" style="display: none;" data-combo-box-output="" ></div>
</div>
When isOpenOnFocus
is enabled, the dropdown menu will automatically appear upon focusing on the input field.
Alternatively, users have the option to utilize the data-combo-box-toggle
attribute, designating an element within the ComboBox responsible for toggling the visibility of the dropdown list.
<div class="relative max-w-sm"
data-combo-box='{
"apiUrl": "https://freetestapi.com/api/v1/countries",
"isOpenOnFocus": true,
"outputEmptyTemplate": "<div class=\"dropdown-item\">No countries found...</div>",
"outputItemTemplate": "<div class=\"dropdown-item combo-box-selected:active\" data-combo-box-output-item> <div class=\"flex justify-between items-center w-full\"> <div data-combo-box-output-item-field=\"name\" data-combo-box-search-text data-combo-box-value></div><span class=\"icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0\"></span></div></div>"
}' >
<div class="relative">
<input class="input" type="text" value="India" role="combobox" aria-expanded="false" data-combo-box-input="" aria-label="open on focus combobox" />
<span class="icon-[tabler--caret-up-down] text-base-content/90 absolute end-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" data-combo-box-toggle="" ></span>
</div>
<div class="bg-base-100 vertical-scrollbar rounded-scrollbar rounded-box absolute z-50 max-h-44 w-full space-y-0.5 p-2 shadow-lg" style="display: none" data-combo-box-output="" ></div>
</div>
A basic usage of ComboBox with close button.
<div class="relative max-w-sm" data-combo-box="">
<div class="relative">
<input class="input" type="text" role="combobox" aria-expanded="false" value="" data-combo-box-input="" aria-label="Combobox with close button" />
<div class="combo-box-active:flex absolute inset-y-0 end-8 hidden items-center">
<button type="button" class="btn btn-sm btn-circle btn-text btn-primary" aria-label="Close" data-combo-box-close="" >
<span class="sr-only">Close</span>
<span class="icon-[tabler--xbox-x] size-4 shrink-0"></span>
</button>
</div>
<span class="icon-[tabler--caret-up-down] text-base-content/90 absolute end-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" data-combo-box-toggle="" ></span>
</div>
<div class="bg-base-100 vertical-scrollbar rounded-scrollbar rounded-box absolute z-50 max-h-44 w-full space-y-0.5 p-2 shadow-lg" style="display: none" data-combo-box-output="" >
<div class="dropdown-item combo-box-selected:active" tabindex="0" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="Venezuela" data-combo-box-value="">Venezuela</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
<div class="dropdown-item combo-box-selected:active" tabindex="1" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="Papua New Guinea" data-combo-box-value="">Papua New Guinea</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
<div class="dropdown-item combo-box-selected:active" tabindex="2" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="South Africa " data-combo-box-value="">South Africa</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
<div class="dropdown-item combo-box-selected:active" tabindex="3" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="Honduras" data-combo-box-value="">Honduras</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
<div class="dropdown-item combo-box-selected:active" tabindex="4" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="India" data-combo-box-value="">India</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
<div class="dropdown-item combo-box-selected:active" tabindex="5" data-combo-box-output-item="">
<div class="flex items-center justify-between">
<span data-combo-box-search-text="El Salvador" data-combo-box-value="">El Salvador</span>
<span class="icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0"></span>
</div>
</div>
</div>
</div>
Combo box html example with dropdown and modal.
Below is an example showcasing the utilization of the SearchBox within a dropdown, employing data attributes in HTML. Furthermore, the example illustrates how to create groups.
<div class="max-w-sm">
<div class="relative"
data-combo-box='{
"groupingType": "default",
"preventSelection": true,
"isOpenOnFocus": true,
"groupingTitleTemplate": "<div class=\"block text-xs text-base-content/50 m-3 mb-1\"></div>"
}' >
<div class="relative">
<span class="icon-[tabler--search] text-base-content/90 absolute start-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" ></span>
<input class="input ps-8" type="text" placeholder="Search for an action" role="combobox" aria-expanded="false" value="" data-combo-box-input="" />
</div>
<div class="bg-base-100 rounded-box p-2 shadow-lg" style="display: none" data-combo-box-output="" >
<div data-combo-box-output-items-wrapper="" class="space-y-0.5">
<!-- Group: Recent Actions -->
<div data-combo-box-output-item='{"group": {"name": "recent", "title": "Recent Actions"}}' tabindex="0">
<a class="dropdown-item combo-box-selected:active" href="#">
<span class="icon-[tabler--writing] text-base-content/80 size-5 flex-shrink-0"></span>
<span class="text-base-content/90" data-combo-box-search-text="Write a document" data-combo-box-value="" >
Write a document
</span>
<span class="text-base-content/50 ms-auto hidden text-xs sm:inline" data-combo-box-search-text="Google Docs" data-combo-box-value="" >
Google Docs
</span>
</a>
</div>
<div data-combo-box-output-item='{"group": {"name": "recent", "title": "Recent Actions"}}' tabindex="1">
<a class="dropdown-item combo-box-selected:active" href="#">
<span class="icon-[tabler--calendar] text-base-content/80 size-5 flex-shrink-0"></span>
<span class="text-base-content/90" data-combo-box-search-text="Schedule a meeting" data-combo-box-value="" >
Schedule a meeting
</span>
<span class="text-base-content/50 ms-auto hidden text-xs sm:inline" data-combo-box-search-text="Google Calendar" data-combo-box-value="" >
Google Calendar
</span>
</a>
</div>
<div data-combo-box-output-item='{"group": {"name": "recent", "title": "Recent Actions"}}' tabindex="2">
<a class="dropdown-item combo-box-selected:active" href="#">
<span class="icon-[tabler--presentation] text-base-content/80 size-5 flex-shrink-0"></span>
<span class="text-base-content/90" data-combo-box-search-text="Create a presentation" data-combo-box-value="" >
Create a presentation
</span>
<span class="text-base-content/50 ms-auto hidden text-xs sm:inline" data-combo-box-search-text="Microsoft PowerPoint" data-combo-box-value="" >
PowerPoint
</span>
</a>
</div>
<!-- Group: People -->
<div data-combo-box-output-item='{"group": {"name": "people", "title": "People"}}' tabindex="4">
<a class="dropdown-item combo-box-selected:active" href="#">
<img class="size-6 flex-shrink-0 rounded-full" src="https://cdn.flyonui.com/fy-assets/avatar/avatar-2.png" alt="Image Description" />
<span class="text-base-content/90" data-combo-box-search-text="Alice Johnson" data-combo-box-value="" >
Alice Johnson
</span>
<span class="ms-auto text-xs text-teal-600" data-combo-box-search-text="Online" data-combo-box-value="">
Online
</span>
</a>
</div>
<div data-combo-box-output-item='{"group": {"name": "people", "title": "People"}}' tabindex="5">
<a class="dropdown-item combo-box-selected:active" href="#">
<img class="size-6 flex-shrink-0 rounded-full" src="https://cdn.flyonui.com/fy-assets/avatar/avatar-11.png" alt="Image Description" />
<span class="text-base-content/90" data-combo-box-search-text="David Kim" data-combo-box-value="">
David Kim
</span>
<span class="text-base-content/50 ms-auto text-xs" data-combo-box-search-text="Offline" data-combo-box-value="" >
Offline
</span>
</a>
</div>
<div data-combo-box-output-item='{"group": {"name": "people", "title": "People"}}' tabindex="6">
<a class="dropdown-item combo-box-selected:active" href="#">
<img class="size-6 flex-shrink-0 rounded-full" src="https://cdn.flyonui.com/fy-assets/avatar/avatar-12.png" alt="Image Description" />
<span class="text-base-content/90" data-combo-box-search-text="Rosa Martinez" data-combo-box-value="" >
Rosa Martinez
</span>
<span class="text-base-content/50 ms-auto text-xs" data-combo-box-search-text="Offline" data-combo-box-value="" >
Offline
</span>
</a>
</div>
</div>
</div>
</div>
</div>
A combo box displaying HTML content within a modal.
<h5 class="text-base-content/90 text-base"> Press <kbd class="kbd kbd-sm">Shift</kbd> + <kbd class="kbd kbd-sm">:</kbd> for SearchBox. </h5>
<!-- SearchBox Trigger -->
<button type="button" class="hidden" aria-haspopup="dialog" aria-expanded="false" aria-controls="html-modal-combo-box" data-overlay="#html-modal-combo-box" ></button>
<div id="html-modal-combo-box" class="overlay modal overlay-open:opacity-100 hidden" role="dialog" tabindex="-1">
<div class="modal-dialog overlay-open:opacity-100">
<div class="modal-content">
<div class="relative"
data-combo-box='{
"preventVisibility": true,
"groupingType": "default",
"preventSelection": true,
"isOpenOnFocus": true,
"groupingTitleTemplate": "<div class=\"block text-xs text-base-content/50 m-3 mb-1\"></div>"
}' >
<div class="modal-header block">
<div class="relative">
<span class="icon-[tabler--search] text-base-content/90 absolute start-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" data-combo-box-toggle="" ></span>
<input class="input ps-8" type="text" placeholder="Search or type a command" role="combobox" aria-expanded="false" value="" autofocus="" data-combo-box-input="" />
</div>
</div>
<!-- SearchBox Modal Body -->
<div class="modal-body vertical-scrollbar rounded-scrollbar mt-0 max-h-72 p-1.5" data-combo-box-output="">
<div class="space-y-0.5 p-0.5" data-combo-box-output-items-wrapper="">
<!-- Group: Recent Actions -->
<div data-combo-box-output-item='{"group": {"name": "recent", "title": "Recent Actions"}}' tabindex="0">
<a class="dropdown-item combo-box-selected:active" href="#">
<span class="icon-[tabler--writing] text-base-content/80 size-4 flex-shrink-0"></span>
<span class="text-base-content/90 text-sm" data-combo-box-search-text="Write a document" data-combo-box-value="" >
Write a document
</span>
<span class="text-base-content/50 ms-auto hidden text-xs sm:inline" data-combo-box-search-text="Google Docs" data-combo-box-value="" >
Google Docs
</span>
</a>
</div>
<div data-combo-box-output-item='{"group": {"name": "recent", "title": "Recent Actions"}}' tabindex="1">
<a class="dropdown-item combo-box-selected:active" href="#">
<span class="icon-[tabler--calendar] text-base-content/80 size-4 flex-shrink-0"></span>
<span class="text-base-content/90 text-sm" data-combo-box-search-text="Schedule a meeting" data-combo-box-value="" >
Schedule a meeting
</span>
<span class="text-base-content/50 ms-auto hidden text-xs sm:inline" data-combo-box-search-text="Google Calendar" data-combo-box-value="" >
Google Calendar
</span>
</a>
</div>
<div data-combo-box-output-item='{"group": {"name": "recent", "title": "Recent Actions"}}' tabindex="2">
<a class="dropdown-item combo-box-selected:active" href="#">
<span class="icon-[tabler--presentation] text-base-content/80 size-4 flex-shrink-0"></span>
<span class="text-base-content/90 text-sm" data-combo-box-search-text="Create a presentation" data-combo-box-value="" >
Create a presentation
</span>
<span class="text-base-content/50 ms-auto hidden text-xs sm:inline" data-combo-box-search-text="Microsoft PowerPoint" data-combo-box-value="" >
PowerPoint
</span>
</a>
</div>
<!-- Group: People -->
<div data-combo-box-output-item='{"group": {"name": "people", "title": "People"}}' tabindex="4">
<a class="dropdown-item combo-box-selected:active" href="#">
<img class="size-5 flex-shrink-0 rounded-full" src="https://cdn.flyonui.com/fy-assets/avatar/avatar-2.png" alt="Image Description" />
<span class="text-base-content/90 text-sm" data-combo-box-search-text="Alice Johnson" data-combo-box-value="" >
Alice Johnson
</span>
<span class="ms-auto text-xs text-teal-600" data-combo-box-search-text="Online" data-combo-box-value="">
Online
</span>
</a>
</div>
<div data-combo-box-output-item='{"group": {"name": "people", "title": "People"}}' tabindex="5">
<a class="dropdown-item combo-box-selected:active" href="#">
<img class="size-5 flex-shrink-0 rounded-full" src="https://cdn.flyonui.com/fy-assets/avatar/avatar-11.png" alt="Image Description" />
<span class="text-base-content/90 text-sm" data-combo-box-search-text="David Kim" data-combo-box-value="" >
David Kim
</span>
<span class="text-base-content/50 ms-auto text-xs" data-combo-box-search-text="Offline" data-combo-box-value="" >
Offline
</span>
</a>
</div>
<div data-combo-box-output-item='{"group": {"name": "people", "title": "People"}}' tabindex="6">
<a class="dropdown-item combo-box-selected:active" href="#">
<img class="size-5 flex-shrink-0 rounded-full" src="https://cdn.flyonui.com/fy-assets/avatar/avatar-12.png" alt="Image Description" />
<span class="text-base-content/90 text-sm" data-combo-box-search-text="Rosa Martinez" data-combo-box-value="" >
Rosa Martinez
</span>
<span class="text-base-content/50 ms-auto text-xs" data-combo-box-search-text="Offline" data-combo-box-value="" >
Offline
</span>
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Combo box example using JSON data displayed within a dropdown and a modal.
Utilizing a JSON example in a dropdown, use option :apiGroupField
to select a heading. Positions are categorized as headings in this scenario.
<div class="max-w-sm">
<div class="relative"
data-combo-box='{
"groupingType": "default",
"isOpenOnFocus": true,
"apiUrl": "/json/searchbox.json",
"apiGroupField": "position",
"outputItemTemplate": "<div class=\"dropdown-item combo-box-selected:active\" data-combo-box-output-item> <div class=\"flex items-center justify-between\"> <div class=\"flex items-center w-full\"> <div class=\"flex items-center justify-center rounded-full bg-base-200 size-6 overflow-hidden me-2.5\"> <img class=\"flex-shrink-0\" data-combo-box-output-item-attr='[{\"valueFrom\": \"image\", \"attr\": \"src\"}, {\"valueFrom\": \"name\", \"attr\": \"alt\"}]' /> </div> <div data-combo-box-output-item-field=\"name\" data-combo-box-search-text data-combo-box-value></div> </div> <span class=\"icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0\"> </span> </div> </div>","groupingTitleTemplate": "<div class=\"block text-xs text-base-content/50 m-3 mb-1\"></div>"
}' >
<!-- SearchBox -->
<div class="relative">
<span class="icon-[tabler--search] text-base-content/90 absolute start-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" ></span>
<input class="input ps-8" type="text" placeholder="Search or type a command" role="combobox" aria-expanded="false" value="" autofocus="" data-combo-box-input="" />
</div>
<!-- SearchBox Body -->
<div class="bg-base-100 vertical-scrollbar rounded-scrollbar rounded-box absolute z-50 max-h-56 w-full space-y-0.5 p-2 shadow-lg" style="display: none" data-combo-box-output="" ></div>
</div>
</div>
Combo box with json data show in modal.
Press Shift + t for SearchBox.
<h5 class="text-base-content/90 text-base"> Press <kbd class="kbd kbd-sm">Shift</kbd> + <kbd class="kbd kbd-sm">t</kbd> for SearchBox. </h5>
<!-- SearchBox Trigger -->
<button type="button" class="hidden" aria-haspopup="dialog" aria-expanded="false" aria-controls="json-modal-combo-box" data-overlay="#json-modal-combo-box" ></button>
<!-- SearchBox Modal -->
<div id="json-modal-combo-box" class="overlay modal overlay-open:opacity-100 hidden" role="dialog" tabindex="-1">
<div class="modal-dialog overlay-open:opacity-100">
<div class="modal-content">
<div class="relative"
data-combo-box='{
"preventVisibility": true,
"groupingType": "default",
"apiUrl": "/json/searchbox.json",
"apiGroupField": "position",
"outputItemTemplate": "<div class=\"dropdown-item combo-box-selected:active\" data-combo-box-output-item> <div class=\"flex items-center justify-between\"> <div class=\"flex items-center w-full\"> <div class=\"flex items-center justify-center rounded-full bg-base-200 size-6 overflow-hidden me-2.5\"> <img class=\"flex-shrink-0\" data-combo-box-output-item-attr='[{\"valueFrom\": \"image\", \"attr\": \"src\"}, {\"valueFrom\": \"name\", \"attr\": \"alt\"}]' /> </div> <div data-combo-box-output-item-field=\"name\" data-combo-box-search-text data-combo-box-value></div> </div> <span class=\"icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0\"> </span> </div> </div>",
"groupingTitleTemplate": "<div class=\"block text-xs text-base-content/50 m-3 mb-1\"></div>"
}' >
<!-- SearchBox -->
<div class="modal-header block">
<div class="relative">
<span class="icon-[tabler--search] text-base-content/90 absolute start-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" ></span>
<input class="input ps-8" type="text" placeholder="Search or type a command" role="combobox" aria-expanded="false" value="" autofocus="" data-combo-box-input="" />
</div>
</div>
<!-- SearchBox Modal Body -->
<div class="modal-body" data-combo-box-output="">
<div class="vertical-scrollbar rounded-scrollbar max-h-72 space-y-0.5" data-combo-box-output-items-wrapper="" ></div>
</div>
</div>
</div>
</div>
</div>
Combo box example demonstrating tab filtering functionality, incorporating both dropdown and modal components.
Experience tab filtering within a dropdown. Set :groupingType
to tabs
, utilize :groupingTitleTemplate
to style the filtering tabs, and :tabsWrapperTemplate
to style the tab wrapper.
<div class="max-w-sm">
<div
class="relative"
data-combo-box='{
"groupingType": "tabs",
"isOpenOnFocus": true,
"apiUrl": "https://fakestoreapi.com/products",
"apiGroupField": "category",
"outputItemTemplate": "<div class=\"dropdown-item combo-box-selected:active\" data-combo-box-output-item> <div class=\"flex justify-between items-center w-full\"> <div class=\"truncate\" data-combo-box-output-item-field=\"title\" data-combo-box-search-text data-combo-box-value></div> <span class=\"icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0\"> </span> </div> </div>",
"groupingTitleTemplate": "<button type=\"button\" class=\"btn btn-sm btn-text capitalize combo-box-tab-active:btn-active\"></button>",
"tabsWrapperTemplate": "<div class=\"horizontal-scrollbar rounded-scrollbar mb-1 pb-2\"></div>"
}' >
<!-- SearchBox -->
<div class="relative">
<span class="icon-[tabler--search] text-base-content/90 absolute start-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" ></span>
<input class="input ps-8" type="text" placeholder="Search or type a command" role="combobox" aria-expanded="false" value="" autofocus="" data-combo-box-input="" />
</div>
<!-- SearchBox body -->
<div class="bg-base-100 vertical-scrollbar rounded-scrollbar rounded-box absolute z-50 max-h-56 w-full p-2 shadow-lg" style="display: none" data-combo-box-output="" >
<div data-combo-box-output-items-wrapper="" class="space-y-0.5"></div>
</div>
</div>
</div>
In this example, a modal popup is employed to provide a user-friendly tab filtering experience.
Press \ for SearchBox.
<h5 class="text-base-content/90 text-base"> Press <kbd class="kbd kbd-sm">\</kbd> for SearchBox. </h5>
<!-- SearchBox Trigger -->
<button type="button" class="hidden" aria-haspopup="dialog" aria-expanded="false" aria-controls="tab-modal-combo-box" data-overlay="#tab-modal-combo-box" ></button>
<!-- SearchBox Modal -->
<div id="tab-modal-combo-box" class="overlay modal overlay-open:opacity-100 hidden" role="dialog" tabindex="-1">
<div class="modal-dialog overlay-open:opacity-100 overflow-x-hidden">
<div class="modal-content max-h-full">
<div class="relative"
data-combo-box='{
"preventVisibility": true,
"groupingType": "tabs",
"isOpenOnFocus": true,
"apiUrl": "https://fakestoreapi.com/products",
"apiGroupField": "category",
"outputItemTemplate": "<div class=\"dropdown-item combo-box-selected:active\" data-combo-box-output-item> <div class=\"flex justify-between items-center w-full\"> <div class=\"truncate\" data-combo-box-output-item-field=\"title\" data-combo-box-search-text data-combo-box-value></div> <span class=\"icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0\"> </span> </div> </div>",
"groupingTitleTemplate": "<button type=\"button\" class=\"btn btn-sm btn-text capitalize combo-box-tab-active:btn-active\"></button>",
"tabsWrapperTemplate": "<div class=\"horizontal-scrollbar rounded-scrollbar mb-1 pb-2\"></div>"
}' >
<div class="modal-header block">
<div class="relative">
<span class="icon-[tabler--search] text-base-content/90 absolute start-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" ></span>
<input class="input ps-8" type="text" placeholder="Search or type a command" role="combobox" aria-expanded="false" value="" autofocus="" data-combo-box-input="" />
</div>
</div>
<!-- SearchBox Modal Body -->
<div class="modal-body" data-combo-box-output="">
<div class="vertical-scrollbar rounded-scrollbar max-h-72 space-y-0.5" data-combo-box-output-items-wrapper="" ></div>
</div>
</div>
</div>
</div>
</div>
COMMAND | DESCRIPTION |
---|---|
Enter | Activates the selected tab. |
A-Z a-z | Focuses first item that matches keyboard input. |
Home End | Focuses first/last non-disabled item. |
Esc | Closes any open Menus. |
Focuses previous/next non-disabled item. |
PARAMETERS | DESCRIPTION | OPTIONS | DEFAULT VALUE |
---|---|---|---|
General Options | |||
data-combo-box | Activates a ComboBox on a specific element. | — | — |
:gap | Defines the gap between the input field and the dropdown list. | number | 6 |
:viewport | Specifies the parent element for calculating dropdown list position, by default relative to the document. | string, HTMLElement | — |
:preventVisibility | If true , prevents visibility changes in the ComboBox. | boolean | false |
:preventSelection | If true , prevents item selection in the dropdown list. | boolean | false |
:isOpenOnFocus | If true , the dropdown list opens when the field is focused. | boolean | false |
:groupingType | Type of grouping used in the dropdown. | default, tags | — |
:groupingTitleTemplate | Template for the title in grouped ComboBox. | One line HTML markup | — |
:preventAutoPosition | If true , it disables dropdown auto-positioning. | boolean | false |
:tabsWrapperTemplate | Template for the tabs wrapper in the ComboBox. | One line HTML markup | <div class="overflow-x-auto p-4"></div> |
API Options | |||
:apiUrl | The URL endpoint for fetching data via API. | string, null | — |
:apiDataPart | Specifies the part of the returned data to use as the primary data array. | string, null | — |
:apiQuery | Parameters for API requests, like limiting results. | string, null | — |
:apiSearchQuery | Parameter name used for search queries. | string, null | — |
:apiHeaders | Headers to include in API requests. | object | {} |
:apiGroupField | Field in the API response used for data grouping. | string, null | — |
Template Options | |||
:outputItemTemplate | Template for individual items in the dropdown list. | string, null | <div class="dropdown-item combo-box-selected:active" data-combo-box-output-item> ... </div> |
:outputEmptyTemplate | Template for when no data is found in the dropdown. | string, null | Nothing found... |
:outputLoaderTemplate | Template for the loader when fetching data. | string, null | <span class="loading loading-spinner text-primary"></span> |
:searchWrapperTemplate | Template for the search field wrapper. | string, null | <div></div> |
Input & Output Options | |||
data-combo-box-input | Identifies the element responsible for data entry in the ComboBox. | — | — |
data-combo-box-output | Identifies the element that outputs data from the ComboBox. | — | — |
data-combo-box-toggle | Element that toggles the dropdown list. | — | — |
data-combo-box-open | Element that open the dropdown list. | — | — |
data-combo-box-close | Element that close the dropdown list. | — | — |
data-combo-box-output-item | Defines an element within the initialized container that will handle the display of each individual data item. | — | — |
> data-combo-box-search-text | Defines the element within which text will be searched. | — | — |
> data-combo-box-value | Defines an element whose text will be set as a value when selected. | — | — |
> data-combo-box-output-item-field | Defines a data field that will be taken to fill the element with text. | — | — |
> data-combo-box-output-item-attr | Defines the attributes that will be filled with the corresponding data. For example, the src attribute of an image. | — | — |
:valueFrom | Field name from which data will be taken. | string | — |
:attr | Attribute name where data will be supplied. | string | — |
The HSCombobox
object is contained within the global window
object.
METHOD | DESCRIPTION | ||
---|---|---|---|
PUBLIC METHODS | |||
open() | Opens the autosuggestion list. | ||
close() | Closes the autosuggestion list. | ||
recalculateDirection() | Forces recalculation of the autosuggestion list’s positions. | ||
STATIC METHODS | |||
HSCombobox.getInstance(target, isInstance) | Returns the element associated with the target .
| ||
HSCombobox.autoInit() | Reinitializes all ComboBoxes on the page. | ||
HSCombobox.close(target) | Closes the target ComboBox.
| ||
HSCombobox.closeCurrentlyOpened() | Closes the currently opened ComboBox. |
Below, is the demonstration on how to use the methods. To test other methods, copy the following methods into your console and click the button.
<!-- Basic ComboBox structure with JavaScript event handling -->
<div class="relative max-w-sm" id="combo-box-method"
data-combo-box='{
"apiUrl": "https://freetestapi.com/api/v1/countries",
"outputItemTemplate": "<div class=\"dropdown-item combo-box-selected:active\" data-combo-box-output-item><div class=\"flex justify-between items-center w-full\"><div data-combo-box-output-item-field=\"name\" data-combo-box-search-text data-combo-box-value></div><span class=\"icon-[tabler--check] text-primary combo-box-selected:block hidden size-4 flex-shrink-0\"></span></div></div>",
"outputEmptyTemplate": "<div class=\"dropdown-item\">No countries found...</div>"
}' >
<div class="relative">
<input class="input" type="text" value="India" role="combobox" aria-expanded="false" data-combo-box-input="" aria-label="Combobox using methods" />
<span class="icon-[tabler--caret-up-down] text-base-content/90 absolute end-3 top-1/2 size-4 flex-shrink-0 -translate-y-1/2" data-combo-box-toggle="" ></span>
</div>
<div class="bg-base-100 vertical-scrollbar rounded-scrollbar rounded-box absolute z-50 max-h-44 w-full space-y-0.5 p-2 shadow-lg" style="display: none" data-combo-box-output="" ></div>
</div>
<button id="open-btn" class="btn btn-primary mt-4 absolute top-28 sm:top-2 sm:end-6">Open ComboBox</button>
<script>
window.addEventListener('load', function () {
const comboBox = HSComboBox.getInstance(document.querySelector('#combo-box-method'), true).element
const openBtn = document.querySelector('#open-btn')
openBtn.addEventListener('click', () => {
comboBox.open()
})
})
</script>
Open item (public method).
// This method is used in above example.
const comboBox = HSComboBox.getInstance(document.querySelector('#combo-box-method'), true).element
const openBtn = document.querySelector('#open-btn')
openBtn.addEventListener('click', () => {
comboBox.open()
})
Open item (mixed).
const { element } = HSComboBox.getInstance('#combo-box-method', true);
const openBtn = document.querySelector('#open-btn');
openBtn.addEventListener('click', () => {
element.open();
});