Attaching a menu of suggested actions to a search input
In some cases, you may want to attach a DropdownMenu to a TextField or Search input - instead of using the Combobox component. A Combobox is meant for displaying a list of values to select from when populating a field - for example, a list of customer attributes or events. In this use case, however, we want to display a list of actions that run when selected, which is why you might choose a DropdownMenu.
The example below shows a search input, where the dropdown menu contains a dynamic list of options, depending on the field's value. When an option is selected, the menu item's action will modify the search term accordingly. Here, the dropdown actions adjust the search term to perform a wildcard search.
There are a few things to note in the example.
Notes on DropdownMenu props:
listNavigationOptions to customize the behavior of Floating UI's list navigation plugin:
virtual is set to true, which makes the focus "virtual". Since we want the focus to remain in the search field, we can't actually move focus to dropdown itemsloop is set to true, which makes the virtual focus loop back to the first or last item when navigating with keyboard arrows past the last/first itemsactiveIndex) so that we know which item's callback to run when the search is triggered (e.g. when pressing Enter)middlewareOptions to { offset: { mainAxis: 0 } } to make sure the dropdown menu renders without a gap after the search fieldwithTypeahead) - it's not meant to be used with typeable elements like inputkeyboardHandlers option - otherwise we won't be able to type correctly (it would prevent us from typing spaces)initialFocus to -1 and shouldReturnFocus to false to keep the dropdown menu from taking focus away from the search fieldshouldTriggerOnFocus to true so that the menu opens when the search field is focusedNotes on Search props:
setReference to the wrapperRef prop, so that it's used as the reference for positioning the menusetReference and getReferenceProps as usual, so that the input element is the trigger for the menuwrapperModifier, which calls setPositionReference. We need the modifier because setPositionReference is a plain function and not a modifier itselfsetReference on the element as usual, which sets the input as the trigger