trisolaran
LiveSelect - Dynamic selection input component for LiveView
Hi! ![]()
I would like to present LiveSelect, a little library that I wrote to easily add a dynamic selection input to your LV forms.
The idea is that the user can type some text, and the component presents a dropdown with content that is filled dynamically by your LV as the user types (LiveSelect sends your LV a message, and your LV replies with the new list of options). The user can then select an option or continue the search.

Background
There are a few (mostly oldish) tutorials that explain how to build similar components, but to the best of my knowledge no component you can add to your LV as a library and just use.
While creating this component for one of my projects, I put considerable effort trying to get it right, especially handling all the tiny little details like navigation with the arrow keys, selection with mouse or enter key, resetting the selection and so on. Therefore, I decided to make it all available as an easy-to-use library so that hopefully the next poor developer won’t have to reeinvent this wheel ![]()
How to use
To use LiveSelect, you add it to your mix dependencies, import the JS hooks (1-2 lines) into your app.js, and add an extra line in your tailwind configurations. You’re now ready to add the LiveSelect input to your forms.
I would be very happy if at least some folks found this component useful, and I’ll be extremely grateful for any feedback ![]()
Thanks,
Max
Most Liked
trisolaran
Version 1.0.0 is here
![]()
Happy to announce this release. It’s been a lot of work, and I definitely fancy a little break from LV
, but I’m glad this is finally out!
Here’s the changes:
- Rendering using a (sexy, new) function component
<.live_select />instead of the (unsexy, old) function style<%= live_select ... %> - Dropping the message-based update cycle (which used
handle_info/2) in favour of an event-based update cycle (which useshandle_event/3). This makes it much easier and more intuitive to use LiveSelect from another LiveComponent. - Ability to customize the default rendering of dropdown entries and tags using the
:optionand:tagslots
Change (2) is the most important one: folks who were using LiveSelect from a LiveComponent were annoyed because LiveSelect was using messages to request an update, and messages can’t be received by LiveComponents. This was forcing users to place the update logic in the LiveView and not in the LiveComponent where it would naturally reside. But now you can just do:
<.live_select form={form} field={field} mode={:tags} phx-target={@myself} />
and
def handle_event("live_select_change", %{"text" => text, "id" => live_select_id}, socket) do
send_update(LiveSelect.Component, id: live_select_id, options: retrieve_options(text))
end
All in your LiveComponent (or LiveView), and you’re good.
I hope that folks who use LiveComponent-based forms will have a more pleasant experience now.
Change (3) is also kind of cool: you can now override rendering of tags and options using slots.
Say you want to add an icon to your tags or options. Now you can do:
<.live_select ... >
<:option :let={option}>
<div class="flex">
<.globe /> <%= option.label %>
</div>
</:option>
<:tag :let={option}>
<.check /> <%= option.label %>
</:tag>
</.live_select>
which would result in this:

Thanks to anyone who provided feedback so far!
Cheers,
Max
trisolaran
Hello! I released a new version of LiveSelect (0.2.0), which finally supports built-in default styles for tailwindcss, without the need to use daisyui ![]()
So now the default is to use basic tailwindcss, with daisyui as an option. Of course, all the styles are still completely customizable. Thanks to everyone who gave feedback and special thanks to @mindok for finding a bug ![]()
You can use the included showcase app to configure LiveSelect on the fly and play around with all the options and styles to see what they look like. You can even see LV events and messages in flight:

Check it out, I think it’s pretty cool ![]()
I hope that now more people will be interested in trying out this library. So: If you need a dynamic selection field for your LiveView, resist the temptation to RollYourOwn™ (trust me: it’s no fun) and take LiveSelect for a spin.
trisolaran
Version 1.4.0 released with support for associations/embeds 
This latest release includes support for displaying and selecting associations and embeds using LiveSelect.
This is something quite a few folks were trying to do, and were experiencing considerable frustration in the process. Not surprisingly! LiveSelect was designed to work well with simple values like strings and numbers, and although it was able to encode arbitrary values in its options, it wasn’t able to deal with complex values or changesets from a form.
Now this has changed, and users should be able to deal with associations/embeds with only minimal hassle. This section in the docs explains how to do it with the help of an example.
As usual, feedback is very welcome! Please do open an issue if anything is weird/doesn’t work/could be improved ![]()
Thanks again to everyone who used this component!
![]()
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








