Abdulkader Safi

LaralCN-UI combobox: a searchable select that still submits with the form

The combobox is the first of the seven components I designed in Figma and then built in PR #7. It is a searchable select that submits through a hidden input, works with wire:model and x-model, and filters without caring about case, accents or Arabic harakat. Building it also turned up a bug where the search box's own typing leaked out to the listeners wrapped around it.

5 min read

Share
The LaralCN-UI combobox open on the docs page, with its search box filtering a list of frameworks.

The combobox is the first of the seven components I designed in Figma and then built in PR #7. It is a searchable select that submits through a hidden input, works with wire:model and x-model, and filters without caring about case, accents or Arabic harakat. Building it also turned up a bug where the search box's own typing leaked out to the listeners wrapped around it.

Options in, one value out

The API is a name, an array of options and an optional starting value:

<x-ui.label for="framework">Framework</x-ui.label>
<x-ui.combobox id="framework" name="framework" :options="$frameworks"
    placeholder="Select framework..." search-placeholder="Search framework..."
    empty="No framework found." />

$frameworks is a plain ['value' => 'Label'] array, the shape you get from Model::pluck('name', 'id'). placeholder, search-placeholder and empty set the copy, so the Arabic demo is the same markup with Arabic strings.

The rendered markup is a wrapper with three parts: a hidden input that holds the value, a button with role="combobox" that shows the selected label, and a popover with a search box and a role="listbox" list. The selected label is rendered on the server, so the button reads "Laravel" before any JavaScript runs.

Attributes go where they are useful

A Blade component gets one bag of attributes, and the combobox splits it three ways:

<div class="{{ $classes }}" data-ui-combobox data-state="closed"
    @if ($attributes->has('style')) style="{{ $attributes->get('style') }}" @endif>
    <input type="hidden" data-ui-combobox-input
        @if ($name) name="{{ $name }}" @endif value="{{ $selected }}"
        @disabled($disabled)
        {{ $attributes->except(['class', 'style', 'id']) }} />

    <button type="button" data-ui-combobox-trigger role="combobox"
        @if ($attributes->has('id')) id="{{ $attributes->get('id') }}" @endif

class and style go on the wrapper, because that is what you want to size. id goes on the trigger button, so <label for="framework"> focuses something focusable. Everything else, including wire:model, x-model, form and data-*, lands on the hidden input, because that is where the value is.

A hidden input never fires events on its own

That last point needed more than an attribute split. Alpine's x-model and Livewire's wire:model both listen for input on the element they are bound to. A hidden input never fires input or change by itself, even when a script sets its value. So binding to it would do nothing.

When an option is picked, the script fires both events itself, and only when the value actually changed:

// input for x-model and wire:model, change for everything else.
if (changed) {
    input.dispatchEvent(new Event('input', { bubbles: true }));
    input.dispatchEvent(new Event('change', { bubbles: true }));
}

Both bubble, so these all work with no extra glue:

<x-ui.combobox wire:model.live="framework" :options="$frameworks" />

<x-ui.combobox name="framework" :options="$frameworks"
    onchange="console.log('Picked', event.target.value)" />

Bug: the search box was talking to wire:model

Here is the part that went wrong. The search box is a real <input type="text"> inside the component. Every keystroke in it fires input, and those events bubble. If you wrapped the combobox in an element listening for input, or in a form with a wire:model on it, every letter typed into the search box looked like a value change. A Livewire listener would see "l", then "la", then "lar", none of which are option values.

The fix is to catch the search box's events at the document in the capture phase, run the filter, and stop them there:

// Capture phase, then stop: the search box's own input and change
// events must not reach a wrapper listening for the value.
['input', 'change'].forEach(function(type) {
    document.addEventListener(type, function(event) {
        if (!event.target.matches('[data-ui-combobox-search]')) return;
        event.stopPropagation();
        if (type === 'input') filter(event.target.closest('[data-ui-combobox]'));
    }, true);
});

Capture matters. A listener in the bubble phase on document would run after every wrapper had already seen the event. In the capture phase, document is the first stop, so the event never reaches the wrappers at all. The command palette in part 14 uses the same trick for its own search box.

Filtering that ignores accents and harakat

Filtering folds both the query and each label before comparing them:

function fold(text) {
    return text.normalize('NFD').replace(/[\u0300-\u036f\u064b-\u0652]/g, '').toLowerCase();
}

The source writes those ranges as literal combining characters, which are invisible on a page, so they are shown escaped here. normalize('NFD') splits "é" into "e" plus a combining accent, and the regex drops the combining marks in the Latin range and the Arabic harakat range. So "jose" finds "José", and an Arabic search without vowel marks finds a label written with them.

The keyboard follows the listbox pattern. On the closed trigger, the arrow keys open the list, and typing a character opens it with that character already in the search box. Inside, Up and Down move the active option (wrapping at the ends), Home and End jump, Enter selects, Escape closes and returns focus to the trigger, and Tab closes. Enter in the search box calls preventDefault(), so it never submits the surrounding form by accident.

Known limits

Two things the combobox does not do, both written in the PR:

  • required does nothing. It lands on the hidden input, and browsers never validate hidden inputs. Validate on the server, which you should be doing anyway.
  • The popover is positioned absolutely inside the wrapper, so a parent with overflow: hidden can clip it.

Where it is

composer require --dev safi/laralcn-ui
php artisan ui:init
php artisan ui:add combobox

Docs: https://laralcn-ui.abdulkadersafi.com/components/combobox, Arabic at https://laralcn-ui.abdulkadersafi.com/ar/components/combobox. GitHub: https://github.com/Abdulkader-Safi/LaralCN-UI. MIT licensed.

Next: the command palette, a native <dialog> that opens with Cmd/Ctrl+K.

Last updated 1 Dec 2026 · filed under Blade, javascript, shadcn, ui, laralcn-ui

Every entry on this project

26 build notes, in order

Including the ones where nothing worked. You are on part 13.

Read the series →

FAQ

Frequently asked questions

How do I use the LaralCN-UI combobox with Livewire?

Put wire:model (or wire:model.live) on the component. It lands on the hidden input, and the combobox fires input and change on that input whenever the value changes.

Can I pass Eloquent results as options?

Yes. Options are a ['value' => 'Label'] array, so Model::pluck('name', 'id')->all() works directly.

Why doesn't required work on the combobox?

The value lives in a hidden input, and browsers do not validate hidden inputs. Validate the field on the server instead.

Does the search work for Arabic text?

Yes. Both the query and the labels are normalized and stripped of Arabic harakat and Latin accents before comparing, so a search without vowel marks matches labels that have them.

Written by

Abdulkader Safi

Software Engineer

Lead engineer at DSRPT, from Lebanon and based in Kuwait. I write about the tools and bugs from real client work, with the numbers I measured.

About me → GitHub LinkedIn

Need this kind of work done on your project?

Start a project →

Keep reading

More from LaralCN-UI: shadcn-style Blade components and blocks you own

All 26 entries →