Skip to content

Commit 1a2fa4c

Browse files
committed
Fix issues identified by the reviewer
1 parent e600a4c commit 1a2fa4c

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import Config
22

33
config :phoenix, :json_library, Jason
44

5-
File.exists?("config/#{config_env()}.exs") && import_config "#{config_env()}.exs"
5+
if File.exists?("config/#{config_env()}.exs"), do: import_config "#{config_env()}.exs"

lib/petal_components/behaviors/field_extension.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule PetalComponents.Field.Extension do
1515
'''
1616
end
1717
"""
18-
@callback render(assigns :: map) :: map
18+
@callback render(assigns :: map) :: Phoenix.LiveView.Rendered.t()
1919

2020
@doc """
2121
This callback function is used to get classes for the custom field.
@@ -25,7 +25,7 @@ defmodule PetalComponents.Field.Extension do
2525
2626
def get_class_for_type("pretty_field"), do: "pc-text-input"
2727
"""
28-
@callback get_class_for_type(String.t) :: String.t
28+
@callback get_class_for_type(String.t()) :: String.t()
2929

3030
@optional_callbacks get_class_for_type: 1
3131
end

lib/petal_components/field.ex

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ defmodule PetalComponents.Field do
1717
1818
It's possible to support custom field types by using the `type={%{type="SomeCustomType", other_assigns...}}.`
1919
In order to define a custom field, a module that implements `PetalComponents.Field.Extension` behavior
20-
needs to be implemented. Its `render/1` function needs to accept an assigns that will
21-
be used for passing a map obtained by merging the assigns passed to this field with the
20+
needs to be implemented. Its `render/1` function needs to accept an assigns argument
21+
that will be used for passing a map obtained by merging the assigns given to this field with the
2222
assigns in the `other_assigns...` passed in the `type` field.
2323
See the example below:
2424
@@ -32,7 +32,7 @@ defmodule PetalComponents.Field do
3232
@impl true
3333
def render(assigns) do
3434
~H'''
35-
<input id={@id} field={@name} value={@value} class={@class} datalist={@datalist} {@rest}/>
35+
<input id={@id} name={@name} value={@value} class={@class} datalist={@datalist} {@rest}/>
3636
<datalist id={@datalist}>
3737
<option :for={{value, option} <- @options} value={value}><%= option %></option>
3838
</datalist>
@@ -643,7 +643,7 @@ defmodule PetalComponents.Field do
643643

644644
module = field_type_extension(type)
645645
assigns = assign(assigns, :class, [assigns.class, get_class_for_type(module, type)])
646-
{assigns, body} = render_body(module, assigns)
646+
body = render_body(module, assigns)
647647
assigns = assign(assigns, :body, body)
648648
~H"""
649649
<.field_wrapper id={"#{@id}-wr"} errors={@errors} name={@name} class={@wrapper_class} no_margin={@no_margin}>
@@ -824,32 +824,23 @@ defmodule PetalComponents.Field do
824824
end
825825
end
826826

827-
@spec render_body(nil|atom(), map()) :: {map(), Phoenix.LiveView.Rendered.t()}
827+
@spec render_body(nil|atom(), map()) :: Phoenix.LiveView.Rendered.t()
828828

829829
defp render_body(nil, assigns) do
830-
{
831-
assigns,
832-
~H"""
833-
<input
834-
type={@type}
835-
name={@name}
836-
id={@id}
837-
value={Phoenix.HTML.Form.normalize_value(@type, @value)}
838-
class={@class}
839-
required={@required}
840-
{@rest}
841-
/>
842-
"""
843-
}
830+
~H"""
831+
<input
832+
type={@type}
833+
name={@name}
834+
id={@id}
835+
value={Phoenix.HTML.Form.normalize_value(@type, @value)}
836+
class={@class}
837+
required={@required}
838+
{@rest}
839+
/>
840+
"""
844841
end
845842

846-
defp render_body(mod, assigns) do
847-
rest = assigns[:rest] || %{}
848-
{extensions, rest} = Map.pop(rest, :extension, %{})
849-
new_assigns = assign(assigns, :rest, rest)
850-
assigns = Map.merge(new_assigns, extensions)
851-
{new_assigns, mod.render(assigns)}
852-
end
843+
defp render_body(mod, assigns), do: mod.render(assigns)
853844

854845
# Note: we use persistent term to avoid the overhead of calling Application.get_env/2.
855846
# Here is the corresponding performance benchmark:

0 commit comments

Comments
 (0)