Skip to content

Extras

CountryPicker

Bit.BlazorUI.ExtrasCountrySelectorCountrySelect

BitCountryPicker picks a country out of a searchable list that shows the flag and the name of each one - for the billing country, the nationality or the country of residence a form asks for. Value is the selected BitCountry; the list can be replaced, trimmed and reordered, searched by name or ISO code, and the whole field is keyboard and screen-reader operable.

Events, forms & accessibility

The events the picker raises, opening it and picking a country from code, posting it with a plain HTML form, translating its texts, and keyboard and screen-reader support.

Events

OnChange reports the picked country, OnOpen / OnClose follow the list, OnSearch reports the search term, and OnFocus / OnBlur follow the field.

Selected country:
Search text:
Callout: closed
Field: blurred

Programmatic control

IsOpen is two-way bindable, and a reference offers OpenAsync, CloseAsync, SelectCountryAsync (which raises the same events picking it in the list does) and FocusAsync.



IsOpen: False

Form submission

For a plain HTML form, Name posts the ISO 3166-1 alpha-2 code of the selected country through a hidden field.

Posted as country:

Localization

Every text the picker shows or announces can be translated. Country names come from Countries; the search box and the empty result take SearchBoxPlaceholder, SearchBoxAriaLabel and NoResultsText; and SearchResultsAnnouncement phrases the result count a screen reader hears.

Accessibility

The field is a native button with the combobox role that pops up a listbox: it reports aria-expanded and points at the list through aria-controls while it is open. Each country is an option with aria-selected, kept out of the tab order. The country the keys are on is not focused but named through aria-activedescendant - by the search box, itself a combobox of the same list, or by the field when NoSearchBox is set - and outlined on screen.

Label (or LabelTemplate) names the field and the list through aria-labelledby. AriaLabel names both instead, for a field with no visible label; with neither, the field is read as "Country" and the list as "Countries". The search box is named by SearchBoxAriaLabel, else SearchBoxPlaceholder, else "Search countries". Flags and the caret are hidden from screen readers, so each country is read by its name alone.

On the closed field:
  • Enter, Space, ArrowDown and ArrowUp open the list on the selected country, or the first one; Home and End open it on the first and the last.
  • A letter or a digit opens it with that character as the start of the search, or, with NoSearchBox, on the country whose name it starts.
In the open list:
  • ArrowDown / ArrowUp move one country, wrapping around the ends unless NoWrapNavigation is set; PageDown / PageUp move ten and stop at the ends; Home / End jump to the first and the last country, except in the search box, where they move the caret.
  • Enter picks the active country. Space does too when there is no search box to type it into, unless it falls inside a name being typed ahead.
  • Without a search box, letters typed in quick succession move to the country whose name they start; after a pause of a second the next letter starts a new term.
  • Escape closes the list. Alt+ArrowUp closes it too, picking the active country when there is no search box.
  • Tab: the search box of the open list comes right after the field in the tab order. Tab out of the list moves on to what follows the field and closes it; Shift+Tab from the search box goes back to the field with the list still open, where the arrows and letters lead back into the search box. Without a search box, Tab closes the list and moves on.
Opening moves the focus into the search box; without one it stays on the field. Picking a country, Escape, Alt+ArrowUp, a click outside the list and the close button of the Responsive panel all return it to the field, and the list closes on its own once the focus has left both the field and the list.

A visually hidden polite live region says how many countries a search term leaves ("1 country found", "12 countries found"), or reads NoResultsText when none is left; SearchResultsAnnouncement rephrases the count. It also reads out an ErrorMessage when it appears, and the country picked from a list without a search box, where the focus never leaves the field.

Required sets aria-required, and its asterisk carries empty alternative text so it is not read out on top of that. Invalid, ErrorMessage and a failed EditForm validation set aria-invalid, and aria-describedby points at the error message and the Description. Disabled disables the native button, which takes it out of the tab order, and adds aria-disabled; ReadOnly sets aria-readonly and keeps the field focusable while refusing to open the list. In forced colors (Windows High Contrast) the selected, active and hovered rows and the disabled field keep showing in system colors.

Try both with the keyboard alone: the first searches its list, the second has no visible label and types ahead.

Arrow keys open the list, typing searches it, Enter picks.

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.