How do you assing options to the "select" input core component?

As an Elixir/Phoenix rookie the core components is somwhat overwhleming for me to understand so I’ve steered clear of creating my own components.

I would however like to know how to use the components that exist, and have been using them with no issues until I reached “select”

This bit seems straight forward

 <.input field={f[:example]} type="select"/>  

But I’m lost on what this is telling me to do

  attr :options, :list, doc: "the options to pass to Phoenix.HTML.Form.options_for_select/2"

If anyone has an example of this being used it would be greatly appreciated.

I think you mean…

<.input field={{f, :example}} type="select"/>  

(The value of field should be a tuple, no?)

Anyway. you need options to populate your select element…

def render(assigns) do
  my_options = [
    {"foo", 1},
    {"bar", 2}
  ]

  assigns = assign(assigns, my_options: my_options)

  ~H"""
    <.input field={{f, :example}} type="select" options={@my_options} />
  """
end

The options can be any kind of enumerable that yields two-tuples (I think).

Edit: This is pseudo code. I didn’t define a <.form> or anything… :grimacing:

1 Like

Awesome thanks

@options ["option1", "option2"]

     socket
     |> assign(assigns)
     |> assign(:changeset, changeset)
     |> assign(:options, @options)

<.input field={f[:example]} type="select" options={@options} />

This was enough to get it to work thanks

I was mainly confused because I believe you used to be able to do use an <%= options tag so the format changed from what I remember. Also wasn’t sure if I’m meant to do something like <:options> or <:list> or use the options_for_select/2

Fixed my issue so thanks