Current section

48 Versions

Jump to

Compare versions

8 files changed
+50 additions
-24 deletions
  @@ -1,6 +1,14 @@
1 1 # Changelog
2 2
3 3
4 + # 0.2.6
5 +
6 + - Fixes a bug where `FormHelpers.field_state` did not handle forms without a changeset.
7 + - Updated components:
8 + - `text_input`: added attrs `name` and `value`
9 + - `checkbox`: add attrs `checked`, `checked_value`, `hidden_input`
10 + - Update `@primer/css` to `21.0.0`.
11 +
4 12 ## 0.2.5
5 13
6 14 Updated components:
  @@ -43,4 +43,4 @@
43 43 {<<"optional">>,false},
44 44 {<<"repository">>,<<"hexpm">>},
45 45 {<<"requirement">>,<<"~> 1.4">>}]]}.
46 - {<<"version">>,<<"0.2.5">>}.
46 + {<<"version">>,<<"0.2.6">>}.
unknownlib/component.ex
File is too large to be displayed (100 KB limit).
  @@ -501,21 +501,25 @@ defmodule PrimerLive.Helpers.AttributeHelpers do
501 501 """
502 502 def common_input_attrs(assigns, input_type) do
503 503 rest = assigns[:rest]
504 - name = rest[:name]
504 + name = assigns[:name] || rest[:name]
505 505 id = rest[:id]
506 506 form = assigns[:form]
507 507 field = assigns[:field]
508 508 field_or_name = field || name || ""
509 509 is_multiple = !!assigns[:is_multiple]
510 510
511 - input_name = input_name(form, field, name: name, is_multiple: is_multiple)
511 + input_name =
512 + if !is_nil(form) || !is_nil(field),
513 + do: input_name(form, field, name: name, is_multiple: is_multiple),
514 + else: name
512 515
513 516 phx_feedback_for_id = input_name
514 517
515 518 # Get value from checkbox or radio button
516 519
520 + ## Ignore the default value "true" when generating derived_label and input_id
521 + checked_value = if assigns[:checked_value] === "true", do: nil, else: assigns[:checked_value]
517 522 value = assigns[:value] || rest[:value]
518 - checked_value = rest[:checked_value]
519 523 value_for_derived_label = checked_value || value
520 524
521 525 derived_label =
  @@ -554,9 +558,9 @@ defmodule PrimerLive.Helpers.AttributeHelpers do
554 558 show_message? = !!message && !ignore_errors? && assigns[:type] !== "hidden"
555 559 validation_message_id = if !is_nil(field_state.message), do: "#{input_id}-validation"
556 560
557 - # Phoenix uses phx_feedback_for to hide form field errors that are untouched.
558 - # However, this attribute can't be set on the element itself (the JS DOM library stalls).
559 - # Element "validation_marker" is used as stopgap: a separate element placed just before the input element.
561 + ## Phoenix uses phx_feedback_for to hide form field errors that are untouched.
562 + ## However, this attribute can't be set on the element itself (the JS DOM library stalls).
563 + ## Element "validation_marker" is used as stopgap: a separate element placed just before the input element.
560 564 validation_marker_attrs =
561 565 case has_changeset do
562 566 true ->
  @@ -23,6 +23,8 @@ defmodule PrimerLive.Helpers.FormHelpers do
23 23 "url" => :url_input
24 24 }
25 25
26 + def text_input_types(), do: @text_input_types
27 +
26 28 @doc """
27 29 Returns a `PrimerLive.FieldState` struct to facilitate display logic in component rendering functions.
28 30
  @@ -32,7 +34,7 @@ defmodule PrimerLive.Helpers.FormHelpers do
32 34 # If validation_message_fn returns a string it will be added to FieldState, regardless of the changeset action value:
33 35 iex> PrimerLive.Helpers.FormHelpers.field_state(
34 36 ...> %Phoenix.HTML.Form{
35 - ...> source: %{
37 + ...> source: %Ecto.Changeset{
36 38 ...> action: :update,
37 39 ...> changes: %{first_name: "annette"},
38 40 ...> errors: [],
  @@ -41,12 +43,12 @@ defmodule PrimerLive.Helpers.FormHelpers do
41 43 ...> },
42 44 ...> },
43 45 ...> :first_name, fn _field_state -> "always" end)
44 - %PrimerLive.FieldState{valid?: true, changeset: %{action: :update, changes: %{first_name: "annette"}, data: nil, errors: [], valid?: true}, message: "always", field_errors: []}
46 + %PrimerLive.FieldState{valid?: true, changeset: %Ecto.Changeset{action: :update, changes: %{first_name: "annette"}, data: nil, errors: [], valid?: true}, message: "always", field_errors: []}
45 47
46 48 # If changeset action is :validate and no validation_message_fn is provided, the default field error is added to FieldState:
47 49 iex> PrimerLive.Helpers.FormHelpers.field_state(
48 50 ...> %Phoenix.HTML.Form{
49 - ...> source: %{
51 + ...> source: %Ecto.Changeset{
50 52 ...> action: :validate,
51 53 ...> changes: %{},
52 54 ...> errors: [first_name: {"can't be blank", [validation: :required]}],
  @@ -55,12 +57,12 @@ defmodule PrimerLive.Helpers.FormHelpers do
55 57 ...> },
56 58 ...> },
57 59 ...> :first_name, nil)
58 - %PrimerLive.FieldState{valid?: false, changeset: %{action: :validate, changes: %{}, data: nil, errors: [first_name: {"can't be blank", [validation: :required]}], valid?: true}, message: "can't be blank", field_errors: ["can't be blank"]}
60 + %PrimerLive.FieldState{valid?: false, changeset: %Ecto.Changeset{action: :validate, changes: %{}, data: nil, errors: [first_name: {"can't be blank", [validation: :required]}], valid?: true}, message: "can't be blank", field_errors: ["can't be blank"]}
59 61
60 62 # If changeset action is :update and no validation_message_fn is provided, no message is added to FieldState:
61 63 iex> PrimerLive.Helpers.FormHelpers.field_state(
62 64 ...> %Phoenix.HTML.Form{
63 - ...> source: %{
65 + ...> source: %Ecto.Changeset{
64 66 ...> action: :update,
65 67 ...> changes: %{},
66 68 ...> errors: [first_name: {"can't be blank", [validation: :required]}],
  @@ -69,7 +71,7 @@ defmodule PrimerLive.Helpers.FormHelpers do
69 71 ...> },
70 72 ...> },
71 73 ...> :first_name, nil)
72 - %PrimerLive.FieldState{valid?: false, changeset: %{action: :update, changes: %{}, data: nil, errors: [first_name: {"can't be blank", [validation: :required]}], valid?: true}, message: "can't be blank", field_errors: ["can't be blank"]}
74 + %PrimerLive.FieldState{valid?: false, changeset: %Ecto.Changeset{action: :update, changes: %{}, data: nil, errors: [first_name: {"can't be blank", [validation: :required]}], valid?: true}, message: "can't be blank", field_errors: ["can't be blank"]}
73 75 """
74 76 def field_state(form, field, validation_message_fn) do
75 77 changeset = form_changeset(form)
  @@ -121,17 +123,29 @@ defmodule PrimerLive.Helpers.FormHelpers do
121 123 end
122 124
123 125 @doc """
124 - Returns the form changeset (from form.source).
125 - Returns nil if no changeset is found,
126 + Returns the form changeset extracted from `form.source`.
127 + Returns nil if no changeset is found.
128 +
129 + iex> PrimerLive.Helpers.FormHelpers.form_changeset(nil)
130 + nil
131 +
132 + iex> PrimerLive.Helpers.FormHelpers.form_changeset(%{})
133 + nil
134 +
135 + iex> PrimerLive.Helpers.FormHelpers.form_changeset(%Phoenix.HTML.Form{})
136 + nil
137 +
138 + iex> PrimerLive.Helpers.FormHelpers.form_changeset(%Phoenix.HTML.Form{
139 + ...> source: %Ecto.Changeset{}
140 + ...> })
141 + %Ecto.Changeset{action: nil, changes: %{}, errors: [], data: nil, valid?: false}
126 142 """
127 - def form_changeset(form) do
128 - try do
129 - form.source
130 - rescue
131 - _ -> nil
132 - end
143 + def form_changeset(%Phoenix.HTML.Form{source: %Ecto.Changeset{}} = form) do
144 + form.source
133 145 end
134 146
147 + def form_changeset(_form), do: nil
148 +
135 149 @doc """
136 150 Returns all errors for a given field from a changeset.
137 151
  @@ -208,7 +222,7 @@ defmodule PrimerLive.Helpers.FormHelpers do
208 222
209 223 """
210 224 def text_input_type_as_atom(type_name) do
211 - input_type = Map.get(@text_input_types, type_name)
225 + input_type = Map.get(text_input_types(), type_name)
212 226
213 227 if is_nil(input_type), do: :text_input, else: input_type
214 228 end
Loading more files…