maxmarcon / live_select

Dynamic (multi)selection field for LiveView
https://hex.pm/packages/live_select
Apache License 2.0
173 stars 32 forks source link

Displayed selected label does not change after first selection #77

Closed camfeghali closed 1 month ago

camfeghali commented 1 month ago

I have the following setup (In a live component).

<.simple_form
  for={@form}
   id="draft_order-form"
   phx-target={@myself}
   phx-change="validate"
   phx-submit="save"
   >
     <.live_select placeholder="Search by Email" field={@form[:customer_id]} label="Customer" phx-target={@myself} />
</.simple_form>

...

  @impl true
  def handle_event("validate", %{"draft_order" => draft_order_params} = rest, socket) do
    changeset =
      socket.assigns.draft_order
      |> DraftOrders.change_draft_order(draft_order_params)
      |> Map.put(:action, :validate)

    {:noreply, assign_form(socket, changeset)}
  end

  ...

    @impl true
  def handle_event("live_select_change", %{"text" => text, "id" => live_select_id, "field" => live_select_field}, socket) do
    options =
      case live_select_field do
        "draft_order_customer_id" -> MyApp.Customers.search_by_email(text) |> Enum.map(& {&1.email, &1.id})
        "draft_order_tags" -> [{"Refund", :refund}, {"Lost Item", :lost_item}]
      end

    send_update(LiveSelect.Component, id: live_select_id, options: options)

    {:noreply, socket}
  end

The displayed selection works fine when i search and select the first customer, but stays the same on subsequent searches and selection

Any help would be much appreciated

pau-riosa commented 1 month ago

On validate handle_event, you still need to send_update just like what you did on live_select_change.

maxmarcon commented 1 month ago

On validate handle_event, you still need to send_update just like what you did on live_select_change.

Mmmh not really. Hard to say what's going on.

What live view version are you on? What live select version?

Sharing a minimal repo that reproduces the issue would help a lot.

camfeghali commented 1 month ago

Hey thanks for getting back to me, i'm working on producing a minimal repo, until then, I'm using

{:phoenix_live_view, "~> 0.20.17"},
{:live_select, "~> 1.0"}
camfeghali commented 1 month ago

I actually updated to live_select 1.4 and it works!

maxmarcon commented 1 month ago

This doesn't tell me which versions are installed :) You should run: mix deps

maxmarcon commented 1 month ago

I actually updated to live_select 1.4 and it works!

Yep, always try the latest version before submitting a bug report :) it saves time! :D