From c76e9e8b8aa6f292ee5ec13645aaf9527bca05b4 Mon Sep 17 00:00:00 2001 From: Jonathan Gao Date: Wed, 27 Apr 2022 20:51:38 +0800 Subject: [PATCH] fix: Fix select. --- lib/phoenix_webcomponent/form_helper.ex | 88 ++++++------------------- package.json | 2 +- priv/src/phoenix_webcomponent.js | 2 +- 3 files changed, 23 insertions(+), 69 deletions(-) diff --git a/lib/phoenix_webcomponent/form_helper.ex b/lib/phoenix_webcomponent/form_helper.ex index fdba8643..39f9329f 100644 --- a/lib/phoenix_webcomponent/form_helper.ex +++ b/lib/phoenix_webcomponent/form_helper.ex @@ -735,38 +735,17 @@ defmodule Phoenix.WebComponent.FormHelper do All other options are forwarded to the underlying HTML tag. """ def wc_select(form, field, options, opts \\ []) when is_atom(field) or is_binary(field) do - {selected, opts} = selected(form, field, opts) - options_html = options_for_select(options, selected) - - {options_html, opts} = - case Keyword.pop(opts, :prompt) do - {nil, opts} -> {options_html, opts} - {prompt, opts} -> {[prompt_option(prompt) | options_html], opts} - end + options_html = options_for_select(options) opts = opts + |> Keyword.put_new(:"label-text", humanize(field)) |> Keyword.put_new(:id, input_id(form, field)) |> Keyword.put_new(:name, input_name(form, field)) + |> Keyword.put_new(:value, input_value(form, field)) + |> Keyword.update!(:value, &maybe_html_escape/1) - content_tag(:"mwc-select", options_html, opts) - end - - defp prompt_option(prompt) when is_list(prompt) do - {prompt_key, prompt_opts} = Keyword.pop(prompt, :key) - - prompt_key || - raise ArgumentError, - "expected :key key when building a prompt select option with a keyword list: " <> - inspect(prompt) - - prompt_option(prompt_key, prompt_opts) - end - - defp prompt_option(key) when is_binary(key), do: prompt_option(key, []) - - defp prompt_option(key, opts) when is_list(opts) do - content_tag(:option, key, Keyword.put_new(opts, :value, "")) + content_tag(:"bx-select", options_html, opts) end @doc """ @@ -791,18 +770,14 @@ defmodule Phoenix.WebComponent.FormHelper do #=> """ - def options_for_select(options, selected_values) do - {:safe, - escaped_options_for_select( - options, - selected_values |> List.wrap() |> Enum.map(&html_escape/1) - )} + def options_for_select(options) do + {:safe, escaped_options_for_select(options)} end - defp escaped_options_for_select(options, selected_values) do + defp escaped_options_for_select(options) do Enum.reduce(options, [], fn {option_key, option_value}, acc -> - [acc | option(option_key, option_value, [], selected_values)] + [acc | option(option_key, option_value, [])] options, acc when is_list(options) -> {option_key, options} = Keyword.pop(options, :key) @@ -817,44 +792,25 @@ defmodule Phoenix.WebComponent.FormHelper do raise ArgumentError, "expected :value key when building