salla-searchable-dropdown

A combobox-style dropdown with an optional search box.

A combobox-style dropdown with an optional search box. Salla components such as salla-delivery-promise, salla-bullet-delivery and salla-gifting use it for city and country pickers. It is controlled: you pass items and selected-item, and update them yourself when it emits itemSelected or searchInput. Use it in a theme when you need a single-choice list that is too long for a native select.

Item names are shown in English (name_en) when the store language is not Arabic and name_en is set, otherwise name is shown.

Accessibility: the trigger has role="combobox" with aria-expanded, the list has role="listbox" and items have role="option" with aria-selected. The trigger opens with Enter, Space or ArrowDown, ArrowUp and ArrowDown move through the items, Enter selects the highlighted item and Escape closes the list.

Use it

In HTML or JSX, use the tag <salla-searchable-dropdown> with the Attribute names below. In React, the wrapper takes the Property names (camelCase).

import { SallaSearchableDropdown } from '@salla.sa/twilight-components-react/searchable-dropdown';

Example

import { useEffect } from 'react';
import { SallaSearchableDropdown } from '@salla.sa/twilight-components-react/searchable-dropdown';

export function Example() {
  useEffect(() => {
    const dropdown = document.querySelector('#city');
    dropdown.items = [{ id: 1, name: 'الرياض', name_en: 'Riyadh' }, { id: 2, name: 'جدة', name_en: 'Jeddah' }];
    dropdown.clientSearch = true;
    dropdown.addEventListener('itemSelected', e => { dropdown.selectedItem = e.detail; });
  }, []);

  return (
    <SallaSearchableDropdown id="city" label="City" placeholder="Select a city" inputId="city-input" required />
  );
}

Properties

PropertyAttributeDescriptionTypeDefault
clientSearchclient-searchWhen true, the component filters items itself as the user types (case-insensitive match on name and name_en, with Arabic alef variants treated as equal). When false, it only emits searchInput and you must update items.booleanfalse
disableddisabledPrevents opening the dropdown and removes the trigger from the tab order.booleanfalse
dropUpdrop-upOpens the list above the trigger instead of below it.booleanfalse
inputIdinput-idUsed as the id of the search input (the label points to it) and as the prefix of the listbox id (<input-id>-listbox). Set a unique value per dropdown on the page.string''
itemsitemsOptions to list. Each item is { id: number | string, name: string, name_en?: string }. Set as a JS property, not an HTML attribute.DropdownItem[][]
keepParentScrollkeep-parent-scrollWhen true, the dropdown will NOT override the nearest scrollable ancestor's overflow to visible while open. Use this when the host (e.g. a modal body) needs to keep its own scrolling intact while the dropdown is open.booleanfalse
labellabelLabel text shown above the trigger. Nothing is rendered when empty.string
loadingloadingDisables the whole dropdown (it cannot be opened) and shows a spinner in place of the list.booleanfalse
noResultsTextno-results-textText shown when the list is empty. Falls back to the translated "no options" message, then to "No results found".string''
placeholderplaceholderText shown in the trigger when nothing is selected, and as the placeholder of the search input.string''
requiredrequiredAdds a red asterisk after the label. It is visual only and does not validate anything.booleanfalse
searchQuerysearch-queryValue shown in the search input when client-search is off. Use it to keep the typed text in sync with a server-side search.string''
searchablesearchableShows the search input inside the open list. Set to false for a plain select-like list.booleantrue
searchingsearchingShows a spinner in place of the list while the dropdown is open. Use it while you fetch results for a searchInput event.booleanfalse
selectedItemselected-itemThe currently selected item, shown in the trigger and marked in the list. The component does not set it itself, so update it from the itemSelected event.DropdownItemnull

Events

Listen with addEventListener, or the on prop of the React wrapper (for example onModalBack).

EventDescriptionDetail
dropdownClosedFires when the list closes, by selection, outside click or Escape. event.detail is empty.void
dropdownOpenedFires when the list opens. event.detail is empty.void
itemSelectedFires when the user picks an item. event.detail is the selected item object. The list closes afterwards.DropdownItem
searchInputFires on every keystroke in the search input. event.detail is the current text.string

Used inside: <salla-bullet-delivery>, <salla-delivery-promise>, <salla-gifting>.

On this page