<%= if @item == [] do %>
{entry[:label]}
<% else %>
{render_slot(@item, %{item: entry, value: entry_value(entry), label: entry[:label]})}
<% end %>
{if @item_indicator != [], do: render_slot(@item_indicator), else: nil}
<%= if @item == [] do %>
{entry[:label]}
<% else %>
{render_slot(@item, %{item: entry, value: entry_value(entry), label: entry[:label]})}
<% end %>
{if @item_indicator != [], do: render_slot(@item_indicator), else: nil}
"""
end
defp content_attrs(id, dir, orientation, has_label) do
Connect.content(%Content{id: id, dir: dir, orientation: orientation})
|> Map.put("data-layout", "list")
|> then(fn attrs ->
if has_label, do: Map.put(attrs, "aria-labelledby", "listbox:#{id}:label"), else: attrs
end)
end
defp item_attrs(id, entry, dir, orientation) do
base =
Connect.item(%Item{
id: id,
item: entry,
value: entry_value(entry),
dir: dir,
orientation: orientation,
to: Map.get(entry, :to),
redirect: Map.get(entry, :redirect),
new_tab: Map.get(entry, :new_tab, false)
})
if Map.get(entry, :disabled) do
base
|> Map.put("data-disabled", "")
|> Map.put("aria-disabled", "true")
else
base
end
end
defp item_attrs_template(id, entry, dir, orientation) do
base =
Connect.item_template(%Item{
id: id,
item: entry,
value: entry_value(entry),
dir: dir,
orientation: orientation,
to: Map.get(entry, :to),
redirect: Map.get(entry, :redirect),
new_tab: Map.get(entry, :new_tab, false)
})
if Map.get(entry, :disabled) do
base
|> Map.put("data-disabled", "")
|> Map.put("aria-disabled", "true")
else
base
end
end
@doc type: :api
@doc """
Sets listbox selection from the client. Dispatches `corex:listbox:set-value` on the hook root.
"""
def set_value(listbox_id, value) when is_binary(listbox_id) do
JS.dispatch("corex:listbox:set-value",
to: "##{listbox_id}",
detail: %{value: validate_value!(List.wrap(value))},
bubbles: false
)
end
@doc type: :api
@doc """
Sets listbox selection from the server via `push_event` (`listbox_set_value`).
"""
def set_value(socket, listbox_id, value)
when is_struct(socket, Phoenix.LiveView.Socket) and is_binary(listbox_id) do
LiveView.push_event(socket, "listbox_set_value", %{
id: listbox_id,
value: validate_value!(List.wrap(value))
})
end
@doc type: :api
@doc """
Requests the listbox's current selected values from the client. See `value/2` (socket arity) for `:respond_to`.
"""
def value(listbox_id) when is_binary(listbox_id), do: value(listbox_id, [])
def value(listbox_id, opts) when is_binary(listbox_id) and is_list(opts) do
JS.dispatch("corex:listbox:value",
to: "##{listbox_id}",
detail: respond_to_fields(opts),
bubbles: false
)
end
@doc type: :api
@doc """
Requests the listbox's current selected values from the client via `push_event` (`listbox_value`).
The hook responds with `listbox_value_response` and/or dispatches `listbox-value` depending on `:respond_to`.
"""
def value(socket, listbox_id)
when is_struct(socket, Phoenix.LiveView.Socket) and is_binary(listbox_id) do
value(socket, listbox_id, [])
end
def value(socket, listbox_id, opts)
when is_struct(socket, Phoenix.LiveView.Socket) and is_binary(listbox_id) and is_list(opts) do
LiveView.push_event(
socket,
"listbox_value",
Map.merge(%{id: listbox_id}, respond_to_fields(opts))
)
end
end