Skip to content

Commit

Permalink
Add radio group
Browse files Browse the repository at this point in the history
  • Loading branch information
bluzky committed Sep 28, 2024
1 parent ec99828 commit 518c19d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/salad_ui/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ defmodule SaladUI.Helpers do

def normalize_integer(_), do: nil

def normalize_boolean(value) do
case value do
"true" -> true
"false" -> false
true -> true
false -> false
_ -> false
end
end

@doc """
Variant helper for generating classes based on side and align
"""
Expand Down
54 changes: 54 additions & 0 deletions lib/salad_ui/radio_group.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
defmodule SaladUI.RadioGroup do
@moduledoc false
use SaladUI, :component

@doc """
Radio input group component
## Examples:
"""
attr :name, :string, default: nil
attr :value, :any, default: nil
attr :class, :string, default: nil
slot :inner_block, required: true

def radio_group(assigns) do
assigns = assign(assigns, :builder, %{name: assigns.name, value: assigns.value})

~H"""
<div
role="radiogroup"
aria-required="false"
dir="ltr"
class={classes(["grid gap-2", @class])}
tabindex="0"
style="outline: none;"
>
<%= render_slot(@inner_block, @builder) %>
</div>
"""
end

attr :builder, :map, required: true
attr :class, :string, default: nil
attr :checked, :any, default: false
attr :value, :string, default: nil
attr :rest, :global

def radio_group_item(assigns) do
~H"""
<input
type="radio"
class={
classes([
"aspect-square h-4 w-4 rounded-full border border-primary text-primary ring-offset-background disabled:cursor-not-allowed disabled:opacity-50",
@class
])
}
name={@builder.name}
checked={normalize_boolean(@checked) || @builder.value == @value}
{@rest}
/>
"""
end
end

0 comments on commit 518c19d

Please sign in to comment.