Sebb

Sebb

I’m facing a problem like this one here: Multi-select with Alpine.js - Help - Livewire Forum - send the currently selected options from a custom multiselect (implemented in alpine, I’m using this one here: Multi select alpine js | Alpinejs, Buttons, Forms, Selects).

There is already a thread about this (I can’t find right now) and I think the easiest way to do it is with a hook:

Hooks.RelayHook = {
  mounted() {
    relay = this
    document.addEventListener('relay-event', (e) =>
      relay.pushEvent(e.detail.event, e.detail.message),
    )
  },
}

...

window.dispatchToLV = function (message) {
  let relay_event = new CustomEvent('relay-event', {
    detail: message,
  })
  document.dispatchEvent(relay_event)
}
<div id="relay-hook-el" phx-hook="RelayHook" hidden></div>

But now seeing, that livewire can do it out of the box, I’d like to ask if there is a better way and if not, I think there should be a better way.

Showing Posts 1 to 4

soup

soup

Not sure I quite understand the question.

If you are ok to alter the shown component, you can add phx-click={JS.push("selected_option", value: %{id: id})} on the “drop down option div”, which would ping an event out for each separate option cilck. You would have to track the remove event separately and do all the list maintenance yourself. You could wrap this in an functional component.

You could also adjust the given component to dispatch a custom event when a selection change occurs, and that event could contain all the options selected. Have a hook that catches that and pushes all the options in one go. There was a talk at this years ElixirConf about using custom html components which would fit this very well if you wanted to drop alpine. I assume this is what you’ve basically already done.

Or you could generate a small form inside the dropdown with alpines x-for, with hidden inputs and generate a submit event on it when selection changes and use the normal phx-submit/change stuff. This will have issues embedding in other forms but you could just use the form it’s embedded in in that case.

The hooks probably the easiest to maintain in the end I think. One event with the whole set of selected options. Phoenix LiveViews provided events aren’t designed to be “input specific” (i.e. you cant phx-change on an <input>), so it’s not really designed to work like LiveWire’s model binding, I would think.

Sebb

Sebb OP

Sorry, the question was a little sloppy.

I just stumbled upon that livewire post and thought, that they have a simple way to just send arbitrary events to the server - I’d like to have that for LV. With LV the only way to do this is with a hook (right?). The hook is OK, but the element I need in the HTML is a little annoying.

But I think I did not understand the livewire solution right, they just simulate an input event. I can do the same with LV:

  • the alpine component uses a hidden select, use a Phoenix.HTML.multiple_select for this
  • reflect all the selected options in the hidden <select> by toggling <option selected ...>
  • after each change trigger a phx-change with
this.select_el.dispatchEvent(new Event("input", {bubbles: true})

see: JavaScript interoperability — Phoenix LiveView v1.2.5

Now the fancy-alpine-multiselect behaves just like a multiple_select. Nice.

So I do not need to send a custom even here, but still it would be nice if it was possible.

derek-zhou

derek-zhou

FOr non-UI related general event, I use a hook on the base container of my liveview.

Sebb

Sebb OP

like this?

<!-- live.hmtl.heex -->
<main class="container" phx-hook="RelayHook">
  ...
  <%= @inner_content %>
  ...
</main>

why did I never think of that? :+1:

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 92995 915
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
GES233
I’m posting this in response to Jose’s recent tweet (Cr. link) : People are sleeping on Elixir for a coding harness: Hot-code swappi...
New
_mfierro
Hello, I wrote Stop My Hand, a Scattergories-like web application using Phoenix/LiveView as my learning project for Elixir (after readin...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews