solop
Hi,
i would like to share the following component that you can use in your phoenix forms to provide a input field with a customizable source of suggestions. Could be used as a tagging component etc.
defmodule MyProjectWeb.Components.SelectComponent do
use MyProjectWeb, :live_component
def render(assigns) do
~H"""
<div class="flex flex-row items-baseline gap-4 break-inside-avoid" phx-target={@myself}>
<.label><%= @label %></.label>
<div class="w-full">
<div class="flex flex-wrap items-center gap-1">
<.inputs_for :let={f} field={@form[@field_name]}>
<.tag_badge color={@color_function.(f[:type].value)}>
<%= f[:name].value %>
<:actions>
<button
type="button"
phx-click="autocomplete_remove"
phx-target={@myself}
phx-value-index={f.index}
class="hover:text-red-500 pl-1"
>
×
</button>
</:actions>
</.tag_badge>
<.input field={f[:id]} type="hidden" />
<.input field={f[:name]} type="hidden" />
<input type="hidden" name={"case[#{@field_name}_sort][]"} value={f.index} />
</.inputs_for>
</div>
<.input
field={@form[:"new_#{@field_name}"]}
type="text"
placeholder={@placeholder}
phx-keydown="autocomplete_key"
onkeydown="return (event.keyCode!=13);"
phx-target={@myself}
/>
<input type="hidden" name={"case[#{@field_name}_drop][]"} />
<div>
<%= for {suggestion, index} <- Enum.with_index(@suggestions) do %>
<div
class={"suggestion #{if index == @selected_index, do: "bg-indigo-200"} rounded-sm p-1"}
phx-click="autocomplete_select"
phx-target={@myself}
phx-value-index={index}
>
<%= suggestion.name %>
</div>
<% end %>
</div>
</div>
</div>
"""
end
def mount(socket) do
{:ok, assign(socket, suggestions: [], selected_index: 0)}
end
def update(assigns, socket) do
{:ok, assign(socket, assigns)}
end
def handle_event("autocomplete_key", %{"key" => key, "value" => value}, socket) do
case key do
"ArrowDown" ->
{:noreply, update_selected_index(socket, 1)}
"ArrowUp" ->
{:noreply, update_selected_index(socket, -1)}
"Enter" ->
selected = get_from_suggestions(socket, socket.assigns.selected_index) || %{name: value}
{:noreply, socket.assigns.on_select.(socket, selected)}
_ ->
suggestions = socket.assigns.search_function.(value)
{:noreply, assign(socket, suggestions: suggestions, selected_index: 0)}
end
end
## In real life there seems to be always a value, but to make tests easier
## this def allows events without value
def handle_event("autocomplete_key", %{"key" => key}, socket) do
handle_event("autocomplete_key", %{"key" => key, "value" => ""}, socket)
end
def handle_event("autocomplete_remove", %{"index" => index}, socket) do
{:noreply, socket.assigns.on_remove.(socket, index)}
end
def handle_event("autocomplete_select", %{"index" => index}, socket) do
{int, _} = Integer.parse(index)
selected = get_from_suggestions(socket, int)
{:noreply, socket.assigns.on_select.(socket, selected)}
end
defp get_from_suggestions(socket, index) do
Enum.at(socket.assigns.suggestions, index)
end
defp update_selected_index(socket, direction) do
update(
socket,
:selected_index,
&rem(
&1 + direction + length(socket.assigns.suggestions),
length(socket.assigns.suggestions)
)
)
end
end
use it like this:
<.live_component
module={SelectComponent}
id={@id}
form={@form}
field_name="tags"
label="Tags"
placeholder="Enter a tag"
search_function={&search_suggestions/1}
color_function={&get_color_for_actor/1}
on_select={&handle_select/2}
on_remove={&handle_remove/2}
/>
You need to define these callback functions for it to work..
Trending in Discussions
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
This is where the projects in the Hologram Showcase get posted: apps built with Hologram and libraries made for it. New ones go up usuall...
New
Hi everyone!
This is our fourth edition. Last year, 1,000+ developers took part. The more of you who answer, the more useful the results...
New
I’ve been fixing race condition / crash bugs in an open source org, for a few months now and almost all of them had the same problem. The...
New
Over the years I’ve tried dozens of time tracking and invoicing apps, and Harvest was the only one I liked. Primarily due to its relative...
New
Hi everyone,
I asked Claude (Anthropic’s AI) a question: if AI writes most of the code, what should a language on the BEAM look like? It...
New
I have a project that I’d like to build in Elixir, but there’s a statistical decision engine component that will likely use hierarchical ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex









