trisolaran

trisolaran

LiveSelect - Dynamic selection input component for LiveView

Hi! :waving_hand:

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.

demo

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 :wink:

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 :folded_hands:

Thanks,
Max

Hexdocs

194 10054 106

Most Liked

trisolaran

trisolaran

Version 1.0.0 is here :tada: :monkey:

Happy to announce this release. It’s been a lot of work, and I definitely fancy a little break from LV :laughing:, but I’m glad this is finally out!

Here’s the changes:

  1. Rendering using a (sexy, new) function component <.live_select /> instead of the (unsexy, old) function style <%= live_select ... %>
  2. Dropping the message-based update cycle (which used handle_info/2) in favour of an event-based update cycle (which uses handle_event/3). This makes it much easier and more intuitive to use LiveSelect from another LiveComponent.
  3. Ability to customize the default rendering of dropdown entries and tags using the :option and :tag slots

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 />&nbsp;<%= option.label %>
          </div>
        </:option>
        <:tag :let={option}>
            <.check />&nbsp;<%= option.label %>
        </:tag>
</.live_select>

which would result in this:

Thanks to anyone who provided feedback so far!

Cheers,
Max

trisolaran

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 :tada:
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 :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 :slight_smile:

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

trisolaran

Version 1.4.0 released with support for associations/embeds :handshake:

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 :chart_with_upwards_trend:

Thanks again to everyone who used this component! :pray: :tada:

trisolaran

trisolaran

Finally: version 0.3.0 with multiselect support is out

LiveSelect now accepts a new :mode option, which takes either :single or :tags as value. :single mode is the old single-selection mode you already know. :tags mode turns the component into a multiselect input with tags:

DEMO

I didn’t simply call it :multiple because I envision new multiselect modes in the future with different flavors, for example using checkboxes in the dropdown.

As usual, all new elements (these includes the single tags as well as the box containing them) can be styled by passing the appropriate options.

And as usual, you can experiment with all the options by running the showcase app locally.

I hope folks who were waiting on the multiselect feature will be satisfied! :rocket:

Looking forward to your feedback. Cheers! :beers:

trisolaran

trisolaran

Version 1.5.0 is out

Thanks to @ringvold’s great work, this version introduces a new mode called quick_tags. In this mode, the dropdown stays open after selection, allowing the user to select additional elements in quick succession. Also, elements can be deselected via the dropdown, too. Here it goes:

DEMO

As an extra bonus, @ringvold also added a toggle to the showcase app that styles the options in the dropdown as checkboxes using slots. Nice.

Thanks again @ringvold! :metal: :rocket:

Where Next?

Popular in Announcing Top

BartOtten
This powerful library works together with Phoenix Router to provide the ultimate routing solution. It simplifies route manipulation, givi...
New
mikehostetler
I’m falling in love with the Req Plugin pattern. It has limits, but when it works - it’s good. I ported over my Tesla based Fly Machines...
New
frerich
An application wishing to store larger amounts of data typically has two options for doing so: A new column on some table can be introd...
New
rahultumpala
Hello Elixir community, I work primarily on Java and other object oriented programming languages and often found it difficult to read an...
New
zteln
Hello Elixir community, I’ve written a small embedded key-value database in Elixir called Goblin. It’s based on the Log-Structured Merge...
New
jsm
Once is an Ecto type for locally unique 64-bits IDs generated by multiple Elixir nodes. Locally unique IDs make it easier to keep things ...
New
martosaur
Hello, I’m excited to introduce InstructorLite – a fork of the Instructor package. Instructor brought the very idea of structured LLM p...
New
tmbb
Playfair (named after: William Playfair) is a data visualization/plotting library with the goal of being able to produce publication-qual...
New
belaustegui
Announcing ErrorTracker, an Elixir-based built-in error tracking solution. Features Basic, free, built-in error tracking solution. Trac...
New
rodloboz
Sifter is a a query filtering library for Ecto. It lets frontend apps send human-readable query strings like: "elixir phoenix status:pu...
New

Other popular topics Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement