Usage
Requires@rails/request.js ^0.0.9
<%= form_with model: @address do |f| %>
<%= ui.dynamic_select url: states_path, param: :country_code do %>
<%= f.select :country, countries, {}, { data: { action: "change->dynamic-select#change" } } %>
<%= f.select :state, [], {}, { data: { dynamic_select_target: "select" } } %>
<% end %>
<% end %>
<%# With f.choices, the dependent select also takes action: "dynamic-select:loaded->choices#reload" %>
<%# app/views/states/index.turbo_stream.erb, for GET /states?country_code=US&target=address_state %>
<%= turbo_stream.update params[:target] do %>
<option value="">Select a state…</option>
<%= options_for_select(states_for(params[:country_code])) %>
<% end %>Block contentThe field that drives the reload and the targets it updates. The wrapper is display: contents, so the fields stay in the form's own layout.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
url | String | nil | Endpoint that answers with a Turbo Stream. It gets a GET with the chosen value as `param`, the id of the `select` target as `target` and the id of the `container` target as `container`. |
param | String | Symbol | nil | Name of the query parameter that carries the chosen value. |
data-action="change->dynamic-select#change" | Action | — | On the field that drives the reload: a select, a radio button or an element around a group of them, a button with a `value`, or a Choices select. |
data-dynamic-select-target="select" | Target | — | The select the server refills. It gets an id if it has none. A Choices select also needs `dynamic-select:loaded->choices#reload` in its actions to pick up the new options. |
data-dynamic-select-target="container" | Target | — | Any element the server updates, addressed by its id. It gets an id if it has none. |
data-action="change->dynamic-select#fillTitle" | Action | — | Copies the chosen option's label into the `title` target (`data-dynamic-select-target="title"`), unless something is typed there already. Needs no `url`. |
dynamic-select:loaded | Event | — | Dispatched on the `select` target, or on the wrapper without one, once the Turbo Stream is rendered. It bubbles. |
Also accepts any HTML attributes via **options (e.g., id:, data:, aria:). class: is also supported for custom styling.
Examples
Native Selects
<%= form_with url: root_path do |f| %>
<%= ui.dynamic_select url: states_path, param: :country_code do %>
<%= f.select :office_country, [["United States", "US"], ["Canada", "CA"], ["Germany", "DE"], ["Australia", "AU"]],
{ include_blank: "Select a country…" }, { label: "Country", data: { action: "change->dynamic-select#change" } } %>
<%= f.select :office_state, [], { include_blank: "Select a country first" }, { label: "State", data: { dynamic_select_target: "select" } } %>
<% end %>
<% end %>Radio Buttons
<%= form_with url: root_path do |f| %>
<%= ui.dynamic_select url: states_path, param: :country_code do %>
<div data-action="change->dynamic-select#change">
<%= f.radio_buttons_collection :shipping_country, [["US", "United States"], ["CA", "Canada"], ["MX", "Mexico"]], :first, :last %>
</div>
<%= f.select :shipping_state, [], { include_blank: "Select a country first" }, { data: { dynamic_select_target: "select" } } %>
<% end %>
<% end %>Buttons
<%= form_with url: root_path do |f| %>
<%= ui.dynamic_select url: states_path, param: :country_code do %>
<div class="flex flex-wrap gap-2">
<% { "US" => "United States", "CA" => "Canada", "AU" => "Australia" }.each do |code, name| %>
<%= ui.btn name, type: :button, value: code, variant: :secondary, data: { action: "dynamic-select#change" } %>
<% end %>
</div>
<%= f.select :region, [], { include_blank: "Pick a country above" }, { data: { dynamic_select_target: "select" } } %>
<% end %>
<% end %>Updating a Container
<%= form_with url: root_path do |f| %>
<%= ui.dynamic_select url: ui_dynamic_select_country_path, param: :country_code do %>
<%= f.select :destination, [["Japan", "JP"], ["Brazil", "BR"], ["Norway", "NO"], ["Kenya", "KE"]],
{ include_blank: "Select a country…" }, { data: { action: "change->dynamic-select#change" } } %>
<div class="rounded-card border border-border p-4" data-dynamic-select-target="container">
<p class="text-sm text-muted-foreground">Pick a country to see its details.</p>
</div>
<% end %>
<% end %>Filling a Title
<%= form_with url: root_path do |f| %>
<%= ui.dynamic_select do %>
<%= f.select :page_id, [["Welcome to Lisbon", 1], ["Packing list", 2], ["Emergency contacts", 3]],
{ include_blank: "Select a page…" }, { label: "Page", data: { action: "change->dynamic-select#fillTitle" } } %>
<%= f.text_field :card_title, hint: "Filled from the page unless you type your own", data: { dynamic_select_target: "title" } %>
<% end %>
<% end %>On the Form Itself
<%= form_with url: root_path, data: { controller: "dynamic-select", dynamic_select_url_value: states_path, dynamic_select_param_value: "country_code" } do |f| %>
<%= f.select :home_country, [["United States", "US"], ["Canada", "CA"], ["Germany", "DE"]],
{ include_blank: "Select a country…" }, { data: { action: "change->dynamic-select#change" } } %>
<%= f.select :home_state, [], { include_blank: "Select a country first" }, { data: { dynamic_select_target: "select" } } %>
<% end %>