Packages
corex
0.1.0-alpha.30
0.1.2
0.1.1
0.1.0
0.1.0-rc.1
0.1.0-rc.0
0.1.0-beta.5
0.1.0-beta.4
0.1.0-beta.3
0.1.0-beta.2
0.1.0-beta.1
0.1.0-alpha.33
0.1.0-alpha.32
0.1.0-alpha.31
0.1.0-alpha.30
0.1.0-alpha.29
0.1.0-alpha.28
0.1.0-alpha.27
0.1.0-alpha.26
0.1.0-alpha.25
0.1.0-alpha.24
0.1.0-alpha.23
0.1.0-alpha.22
0.1.0-alpha.21
0.1.0-alpha.20
0.1.0-alpha.19
0.1.0-alpha.18
0.1.0-alpha.17
0.1.0-alpha.16
0.1.0-alpha.15
0.1.0-alpha.14
0.1.0-alpha.13
0.1.0-alpha.12
0.1.0-alpha.11
0.1.0-alpha.10
0.1.0-alpha.9
0.1.0-alpha.8
0.1.0-alpha.7
0.1.0-alpha.6
0.1.0-alpha.5
0.1.0-alpha.4
0.1.0-alpha.3
0.1.0-alpha.2
0.1.0-alpha.1
Accessible and unstyled UI components library written in Elixir and TypeScript that integrates Zag.js state machines into the Phoenix Framework.
Current section
Files
Jump to
Current section
Files
lib/components/combobox.ex
defmodule Corex.Combobox do
@moduledoc ~S'''
Phoenix implementation of [Zag.js Combobox](https://zagjs.com/components/react/combobox).
<!-- tabs-open -->
### Minimal
```heex
<.combobox
class="combobox"
translation={%Corex.Combobox.Translation{placeholder: "Select a country", empty: "No results"}}
collection={[
%{label: "France", id: "fra", disabled: true},
%{label: "Belgium", id: "bel"},
%{label: "Germany", id: "deu"},
%{label: "Netherlands", id: "nld"},
%{label: "Switzerland", id: "che"},
%{label: "Austria", id: "aut"}
]}
>
<:trigger>
<.heroicon name="hero-chevron-down" />
</:trigger>
</.combobox>
```
### Grouped
```heex
<.combobox
class="combobox"
translation={%Corex.Combobox.Translation{placeholder: "Select a country", empty: "No results"}}
collection={[
%{label: "France", id: "fra", group: "Europe"},
%{label: "Belgium", id: "bel", group: "Europe"},
%{label: "Germany", id: "deu", group: "Europe"},
%{label: "Netherlands", id: "nld", group: "Europe"},
%{label: "Switzerland", id: "che", group: "Europe"},
%{label: "Austria", id: "aut", group: "Europe"},
%{label: "Japan", id: "jpn", group: "Asia"},
%{label: "China", id: "chn", group: "Asia"},
%{label: "South Korea", id: "kor", group: "Asia"},
%{label: "Thailand", id: "tha", group: "Asia"},
%{label: "USA", id: "usa", group: "North America"},
%{label: "Canada", id: "can", group: "North America"},
%{label: "Mexico", id: "mex", group: "North America"}
]}
>
<:trigger>
<.heroicon name="hero-chevron-down" />
</:trigger>
</.combobox>
```
### Extended
This example requires the installation of [Flagpack](https://hex.pm/packages/flagpack) to display the use of custom item rendering.
```heex
<.combobox
class="combobox"
translation={%Corex.Combobox.Translation{placeholder: "Select a country", empty: "No results"}}
collection={[
%{label: "France", id: "fra"},
%{label: "Belgium", id: "bel"},
%{label: "Germany", id: "deu"},
%{label: "Netherlands", id: "nld"},
%{label: "Switzerland", id: "che"},
%{label: "Austria", id: "aut"}
]}
>
<:item :let={item}>
<Flagpack.flag name={String.to_atom(item.id)} />
{item.label}
</:item>
<:trigger>
<.heroicon name="hero-chevron-down" />
</:trigger>
<:clear_trigger>
<.heroicon name="hero-backspace" />
</:clear_trigger>
<:item_indicator>
<.heroicon name="hero-check" />
</:item_indicator>
</.combobox>
```
### Extended Grouped
This example requires the installation of [Flagpack](https://hex.pm/packages/flagpack) to display the use of custom item rendering.
```heex
<.combobox
class="combobox"
translation={%Corex.Combobox.Translation{placeholder: "Select a country", empty: "No results"}}
collection={[
%{label: "France", id: "fra", group: "Europe"},
%{label: "Belgium", id: "bel", group: "Europe"},
%{label: "Germany", id: "deu", group: "Europe"},
%{label: "Japan", id: "jpn", group: "Asia"},
%{label: "China", id: "chn", group: "Asia"},
%{label: "South Korea", id: "kor", group: "Asia"}
]}
>
<:item :let={item}>
<Flagpack.flag name={String.to_atom(item.id)} />
{item.label}
</:item>
<:trigger>
<.heroicon name="hero-chevron-down" />
</:trigger>
<:clear_trigger>
<.heroicon name="hero-backspace" />
</:clear_trigger>
<:item_indicator>
<.heroicon name="hero-check" />
</:item_indicator>
</.combobox>
```
## Phoenix Form Integration
Use `field={f[:key]}` or `field={@form[:key]}` with a form built from an Ecto changeset. Set the form id with `Corex.Form.get_form_id/1`. Build the form in the controller with `Schema.changeset(%Schema{}, %{}) |> Phoenix.Component.to_form(as: :form_name, id: "form-id")`. In Live view add controlled mode and use the same changeset pattern. See the Select or NumberInput component docs for the full Controller and Live View examples.
### Server-side Filtering
Disable client filtering with `disabled={false}` and use `on_input_value_change` to filter on the server. This example uses a local list; replace with a database query for real apps.
```heex
defmodule MyAppWeb.CountryCombobox do
use MyAppWeb, :live_view
@items [
%{id: "fra", label: "France"},
%{id: "bel", label: "Belgium"},
%{id: "deu", label: "Germany"},
%{id: "usa", label: "USA"},
%{id: "jpn", label: "Japan"}
]
def mount(_params, _session, socket) do
{:ok, assign(socket, items: [])}
end
def handle_event("search", %{"value" => value, "reason" => "input-change"}, socket) do
filtered =
if byte_size(value) < 1 do
[]
else
term = String.downcase(value)
Enum.filter(@items, fn item ->
String.contains?(String.downcase(item.label), term)
end)
end
{:noreply, assign(socket, items: filtered)}
end
def render(assigns) do
~H"""
<.combobox
id="country-combobox"
collection={@items}
filter={false}
on_input_value_change="search"
>
<:trigger><.heroicon name="hero-chevron-down" /></:trigger>
</.combobox>
"""
end
end
```
<!-- tabs-close -->
## Styling
Use data attributes to target elements:
```css
[data-scope="combobox"][data-part="root"] {}
[data-scope="combobox"][data-part="control"] {}
[data-scope="combobox"][data-part="input"] {}
[data-scope="combobox"][data-part="trigger"] {}
[data-scope="combobox"][data-part="clear-trigger"] {}
[data-scope="combobox"][data-part="content"] {}
[data-scope="combobox"][data-part="empty"] {}
[data-scope="combobox"][data-part="item-group"] {}
[data-scope="combobox"][data-part="item-group-label"] {}
[data-scope="combobox"][data-part="item"] {}
[data-scope="combobox"][data-part="item-text"] {}
[data-scope="combobox"][data-part="item-indicator"] {}
```
If you wish to use the default Corex styling, you can use the class `combobox` on the component.
This requires to install `Mix.Tasks.Corex.Design` first and import the component css file.
```css
@import "../corex/main.css";
@import "../corex/tokens/themes/neo/light.css";
@import "../corex/components/combobox.css";
```
You can then use modifiers
```heex
<.combobox class="combobox combobox--accent combobox--lg" collection={[]}>
<:empty>No results</:empty>
<:trigger>
<.heroicon name="hero-chevron-down" />
</:trigger>
</.combobox>
```
Learn more about modifiers and [Corex Design](https://corex-ui.com/components/combobox#modifiers)
'''
defmodule Translation do
@moduledoc """
Translation struct for Combobox component strings.
Without gettext: `translation={%Combobox.Translation{ placeholder: "Select...", empty: "No results" }}`
With gettext: `translation={%Combobox.Translation{ placeholder: gettext("Select..."), empty: gettext("No results") }}`
"""
defstruct [:placeholder, :empty]
end
@doc type: :component
use Phoenix.Component
import Corex.Gettext, only: [gettext: 1]
alias Corex.Combobox.Anatomy.{Content, Control, Input, Label, Positioner, Props, Root}
alias Corex.Combobox.Connect
@doc """
Renders a combobox component.
"""
attr(:id, :string,
required: false,
doc: "The id of the combobox, useful for API to identify the combobox"
)
attr(:collection, :list,
default: [],
doc: "The collection of items to display in the combobox"
)
attr(:controlled, :boolean, default: false, doc: "Whether the combobox is controlled")
attr(:on_open_change, :string,
default: nil,
doc: "The server event name to trigger on open change"
)
attr(:on_open_change_client, :string,
default: nil,
doc: "The client event name to trigger on open change"
)
attr(:bubble, :boolean, default: false, doc: "Whether the client events are bubbled")
attr(:disabled, :boolean, default: false, doc: "Whether the combobox is disabled")
attr(:open, :boolean, default: false, doc: "Whether the combobox is open")
attr(:value, :list, default: [], doc: "The value of the combobox")
attr(:translation, Corex.Combobox.Translation,
default: nil,
doc: "Override translatable strings"
)
attr(:always_submit_on_enter, :boolean,
default: false,
doc: "Whether to always submit on enter"
)
attr(:auto_focus, :boolean, default: false, doc: "Whether to auto focus the combobox")
attr(:close_on_select, :boolean, default: true, doc: "Whether to close the combobox on select")
attr(:dir, :string,
default: nil,
doc:
"The direction of the combobox. When nil, derived from document (html lang + config :rtl_locales)"
)
attr(:input_behavior, :string,
default: "autohighlight",
doc: "The input behavior of the combobox"
)
attr(:loop_focus, :boolean, default: false, doc: "Whether to loop focus the combobox")
attr(:multiple, :boolean, default: false, doc: "Whether to allow multiple selection")
attr(:invalid, :boolean, default: false, doc: "Whether the combobox is invalid")
attr(:name, :string, doc: "The name of the combobox")
attr(:form, :string, doc: "The id of the form of the combobox")
attr(:read_only, :boolean, default: false, doc: "Whether the combobox is read only")
attr(:required, :boolean, default: false, doc: "Whether the combobox is required")
attr(:filter, :boolean,
default: true,
doc:
"When true, filter options client-side by input value. Set to false when using on_input_value_change for server-side filtering"
)
attr(:on_input_value_change, :string,
default: nil,
doc: "The server event name to trigger on input value change"
)
attr(:on_value_change, :string,
default: nil,
doc: "The server event name to trigger on value change"
)
attr(:positioning, :map, default: %Corex.Positioning{}, doc: "The positioning of the combobox")
attr(:rest, :global)
slot :label, required: false, doc: "The label content" do
attr(:class, :string, required: false)
end
slot :empty,
required: false,
doc: "Content when there are no results. When omitted, translation.empty is used" do
attr(:class, :string, required: false)
end
slot :trigger, required: true, doc: "The trigger button content" do
attr(:class, :string, required: false)
end
slot :clear_trigger, required: false, doc: "The clear button content" do
attr(:class, :string, required: false)
end
slot :item_indicator, required: false, doc: "Optional indicator for selected items" do
attr(:class, :string, required: false)
end
slot :error, required: false do
attr(:class, :string, required: false)
end
slot :item,
required: false,
doc: "Custom content for each item. Receives the item as :let binding" do
attr(:class, :string, required: false)
end
attr(:field, Phoenix.HTML.FormField,
doc:
"A form field struct retrieved from the form, for example: @form[:country]. Automatically sets id, name, value, and errors from the form field"
)
attr(:errors, :list,
default: [],
doc: "List of error messages to display"
)
def combobox(%{field: %Phoenix.HTML.FormField{} = field} = assigns) do
errors = if Phoenix.Component.used_input?(field), do: field.errors, else: []
raw_value = get_value(field.value)
value = normalize_value_to_ids(assigns.collection, raw_value)
selected_label = get_selected_label(assigns.collection, value)
assigns
|> assign(field: nil)
|> assign(:errors, Enum.map(errors, &Corex.Gettext.translate_error(&1)))
|> assign_new(:id, fn -> field.id end)
|> assign_new(:form, fn -> field.form.id end)
|> assign_new(:name, fn -> field.name end)
|> assign(:value, value)
|> assign(:selected_label, selected_label)
|> combobox()
end
def combobox(assigns) do
default_translation = %Translation{
placeholder: gettext("Select an option"),
empty: gettext("No results")
}
translation = assigns[:translation] || default_translation
placeholder = translation.placeholder
empty_text = translation.empty
assigns =
assigns
|> assign_new(:id, fn -> "combobox-#{System.unique_integer([:positive])}" end)
|> assign_new(:name, fn -> "name-#{System.unique_integer([:positive])}" end)
|> assign_new(:form, fn -> nil end)
|> assign(:translation, translation)
|> assign(:placeholder, placeholder)
|> assign(:empty_text, empty_text)
value = Map.get(assigns, :value, [])
value_list = get_value(value)
value_list = normalize_value_to_ids(assigns.collection, value_list)
grouped_items = Enum.group_by(assigns.collection, &Map.get(&1, :group))
has_groups =
grouped_items
|> Map.keys()
|> Enum.any?(& &1)
selected_label = get_selected_label(assigns.collection, value_list)
assigns =
assigns
|> assign(:grouped_items, grouped_items)
|> assign(:has_groups, has_groups)
|> assign(:value, value_list)
|> assign(:selected_label, selected_label)
|> assign(:value_for_hidden_input, value_for_hidden_input(value_list, assigns.multiple))
~H"""
<div id={@id} phx-hook="Combobox" {@rest} {Connect.props(%Props{
id: @id, collection: @collection, controlled: @controlled, placeholder: @placeholder, value: @value, form: @form,
always_submit_on_enter: @always_submit_on_enter, auto_focus: @auto_focus, close_on_select: @close_on_select,
dir: @dir, input_behavior: @input_behavior, loop_focus: @loop_focus, multiple: @multiple, invalid: @invalid,
name: @name, read_only: @read_only, required: @required,
on_open_change: @on_open_change, on_open_change_client: @on_open_change_client, on_input_value_change: @on_input_value_change, on_value_change: @on_value_change,
open: @open, positioning: @positioning,
bubble: @bubble, disabled: @disabled, filter: @filter
})}>
<div phx-update="ignore" {Connect.root(%Root{id: @id, invalid: @invalid, read_only: @read_only})}>
<div phx-update="ignore" :if={!Enum.empty?(@label)} {Connect.label(%Label{id: @id, invalid: @invalid, read_only: @read_only, required: @required, disabled: @disabled, dir: @dir})}>
{render_slot(@label)}
</div>
<div phx-update="ignore" {Connect.control(%Control{id: @id, invalid: @invalid, open: @open, dir: @dir, disabled: @disabled})}>
<input {Connect.input(%Input{id: @id, value: @value, selected_label: @selected_label, form: nil, invalid: @invalid, open: @open, dir: @dir, disabled: @disabled, required: @required, placeholder: @placeholder, name: nil, auto_focus: @auto_focus})} />
<button :if={!Enum.empty?(@clear_trigger) and !Enum.empty?(@value)} data-scope="combobox" data-part="clear-trigger">
{render_slot(@clear_trigger)}
</button>
<button data-scope="combobox" data-part="trigger">
{render_slot(@trigger)}
</button>
</div>
<div phx-update="ignore" {Connect.positioner(%Positioner{id: @id, dir: @dir})}>
<ul phx-update="ignore" {Connect.content(%Content{id: @id, dir: @dir, open: @open})}>
</ul>
</div>
</div>
<div :if={!Enum.empty?(@errors)} :for={msg <- @errors} data-scope="combobox" data-part="error">
{render_slot(@error, msg)}
</div>
<div style="display: none;" data-templates="combobox">
<li data-scope="combobox" data-part="empty" data-template="true">
<%= if Enum.empty?(@empty) do %>
{@empty_text}
<% else %>
{render_slot(@empty)}
<% end %>
</li>
<div :if={@has_groups} :for={{group, items} <- @grouped_items} data-scope="combobox" data-part="item-group" data-id={group || "default"} data-template="true">
<div :if={group} data-scope="combobox" data-part="item-group-label" data-id={group}>
{group}
</div>
<div data-scope="combobox" data-part="item-group-content">
<li :for={item <- items} data-scope="combobox" data-part="item" data-value={item.id} data-template="true">
<span :if={!Enum.empty?(@item)} data-scope="combobox" data-part="item-text">
{render_slot(@item, item)}
</span>
<span :if={Enum.empty?(@item)} data-scope="combobox" data-part="item-text">
{item.label}
</span>
<span :if={!Enum.empty?(@item_indicator)} data-scope="combobox" data-part="item-indicator">
{render_slot(@item_indicator)}
</span>
</li>
</div>
</div>
<li :if={!@has_groups} :for={item <- @collection} data-scope="combobox" data-part="item" data-value={item.id} data-template="true">
<span :if={!Enum.empty?(@item)} data-scope="combobox" data-part="item-text">
{render_slot(@item, item)}
</span>
<span :if={Enum.empty?(@item)} data-scope="combobox" data-part="item-text">
{item.label}
</span>
<span :if={!Enum.empty?(@item_indicator)} data-scope="combobox" data-part="item-indicator">
{render_slot(@item_indicator)}
</span>
</li>
</div>
</div>
"""
end
defp get_value(field_value) do
case field_value do
nil -> []
[] -> []
value when is_list(value) -> Enum.map(value, &to_string/1)
value -> [to_string(value)]
end
end
defp normalize_value_to_ids(collection, value_list) do
Enum.map(value_list, &resolve_value_id(collection, &1))
end
defp resolve_value_id(collection, val) do
by_id = Enum.find(collection, &(&1.id == val))
by_label = Enum.find(collection, &(&1.label == val))
cond do
by_id != nil -> val
by_label != nil -> by_label.id
true -> val
end
end
defp get_selected_label(collection, value) do
case value do
[] ->
nil
_ ->
value
|> Enum.map(fn val ->
collection
|> Enum.find(&(&1.id == val))
end)
|> Enum.filter(& &1)
|> Enum.map(& &1.label)
|> case do
[] -> nil
labels -> Enum.join(labels, ", ")
end
end
end
defp value_for_hidden_input(value_list, _multiple) when value_list == [], do: ""
defp value_for_hidden_input(value_list, false), do: List.first(value_list)
defp value_for_hidden_input(value_list, true), do: Enum.join(value_list, ",")
end