Skip to content

Build a region picker

createRegionList turns the engine’s region data into picker rows. createRegionPicker holds the open state, the keyboard cursor, and the selection. attachRegionPicker wires a button, a search box, and a list to it, while your render functions draw the rows and the button.

Open the picker. All 245 regions are in the list, with US, CA, and GB pinned on top. Search united and the list narrows to three rows. The match runs over names, region codes, and calling codes; +44 finds the United Kingdom too. Pick a row and the button shows its flag and name, with the status line reporting the selection. The arrow keys walk the rows from the search box, Enter picks the active row, and Escape closes the list.

The demo and the source are the same three files. The markup is a button with a flag sprite cell and a name, a popup with the search box and an empty list, and a status line.

The script creates two widgets. createRegionList builds the rows, with prioritize pinning US, CA, and GB. createRegionPicker takes the list and, without a phone input, keeps the selection itself. select seeds it with the United States.

attachRegionPicker wires the button, the search box, and the list to the picker and owns the clicks, the keys, the outside press, and the ARIA attributes. The demo hands it three functions. renderOption builds a row from a flag cell, the name, and the calling code, with flagTransform selecting the cell. renderEmpty builds the row shown when the search matches nothing. renderTrigger puts the selected region’s flag and name on the button and writes the status line.

<div id="region-picker">
<label for="region-picker-trigger">Region</label>
<button id="region-picker-trigger" class="region-picker-trigger">
<span class="tlx-flag" aria-hidden="true"><span class="tlx-flag__image"></span></span>
<span class="region-picker-name"></span>
<svg class="region-picker-chevron" viewBox="0 0 10 6" aria-hidden="true">
<path d="M1 1l4 4 4-4" fill="none" stroke="currentColor" stroke-width="1.5" />
</svg>
</button>
<div class="region-picker-menu" hidden>
<input class="region-picker-search" type="text" aria-label="Search regions" placeholder="Search regions" />
<ul aria-label="Regions"></ul>
</div>
<p class="region-picker-status"></p>
</div>

The picker so far only reports the selection. Build the complete field hands it a phone input, which then owns the region.