Skip to content

Commit

Permalink
Fix mixing string and integer for integer type
Browse files Browse the repository at this point in the history
  • Loading branch information
bluzky committed Jun 8, 2024
1 parent 4530e21 commit b82bc23
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
12 changes: 12 additions & 0 deletions lib/salad_ui/progress.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ defmodule SaladUI.Progress do
attr(:rest, :global)

def progress(assigns) do
assigns = assign(assigns, :value, normalize_integer(assigns[:value]))

~H"""
<div
class={classes(["relative h-4 w-full overflow-hidden rounded-full bg-secondary", @class])}
Expand All @@ -29,4 +31,14 @@ defmodule SaladUI.Progress do
</div>
"""
end

defp normalize_integer(value) do
case Integer.parse(value) do
{integer, _} ->
integer

_ ->
nil
end
end
end
14 changes: 14 additions & 0 deletions lib/salad_ui/slider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ defmodule SaladUI.Slider do
attr(:rest, :global)

def slider(assigns) do
assigns =
assigns
|> Map.put(:value, normalize_integer(assigns[:value]))
|> Map.put(:min, normalize_integer(assigns[:min]))
|> Map.put(:max, normalize_integer(assigns[:max]))
|> Map.put(:step, normalize_integer(assigns[:step]))

~H"""
<div
class={classes(["relative w-full", @class])}
Expand Down Expand Up @@ -66,4 +73,11 @@ defmodule SaladUI.Slider do
</div>
"""
end

defp normalize_integer(value) do
case Integer.parse(value) do
{:ok, integer} -> integer
_ -> nil
end
end
end
10 changes: 2 additions & 8 deletions lib/salad_ui/textarea.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,12 @@ defmodule SaladUI.Textarea do
"""
attr(:id, :any, default: nil)
attr(:name, :string)
attr(:name, :string, default: nil)
attr(:value, :string)

attr(:field, Phoenix.HTML.FormField, doc: "a form field struct retrieved from the form, for example: @form[:email]")

attr(:class, :string, default: nil)
attr(:rest, :global)

def textarea(assigns) do
assigns = prepare_assign(assigns)
rest = Map.merge(assigns.rest, Map.take(assigns, [:id, :name]))
assigns = assign(assigns, :rest, rest)

~H"""
<textarea
class={
Expand All @@ -35,6 +28,7 @@ defmodule SaladUI.Textarea do
@class
])
}
{%{id: @id, name: @name}}
{@rest}
><%= Phoenix.HTML.Form.normalize_value("textarea", assigns[:value]) %></textarea>
"""
Expand Down
11 changes: 11 additions & 0 deletions lib/salad_ui/tooltip.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ defmodule SaladUI.Tooltip do
"""
end

@doc """
Render only for compatible with shad ui
"""
slot(:inner_block, required: true)

def tooltip_trigger(assigns) do
~H"""
<%= render_slot(@inner_block) %>
"""
end

@doc """
Render
"""
Expand Down

0 comments on commit b82bc23

Please sign in to comment.