Skip to content

Commit 11d246d

Browse files
committedJul 5, 2023
Add radio_fieldset function component that autogenerates radio buttons
1 parent 736b4e3 commit 11d246d

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed
 

‎lib/live_cal/scheduling/calendar.ex

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule LiveCal.Scheduling.Calendar do
55
schema "calendars" do
66
field :description, :string
77
field :name, :string
8+
field :visibility, Ecto.Enum, values: [:public, :private], default: :private
89

910
has_many :events, LiveCal.Scheduling.Event, on_replace: :delete
1011

@@ -14,12 +15,12 @@ defmodule LiveCal.Scheduling.Calendar do
1415
@doc false
1516
def changeset(calendar, attrs) do
1617
calendar
17-
|> cast(attrs, [:name, :description])
18+
|> cast(attrs, [:name, :description, :visibility])
1819
|> cast_assoc(:events,
1920
with: &LiveCal.Scheduling.Event.changeset/2,
2021
sort_param: :events_sort,
2122
drop_param: :events_drop
2223
)
23-
|> validate_required([:name, :description])
24+
|> validate_required([:name, :description, :visibility])
2425
end
2526
end

‎lib/live_cal_web/components/core_components.ex

+25
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,31 @@ defmodule LiveCalWeb.CoreComponents do
387387
"""
388388
end
389389

390+
@doc """
391+
Renders a fieldset with multiple radio buttons representing `Ecto.Enum` fields.
392+
note: string interpolation of `@field.value` is because it can switch from an atom to string
393+
depending on whether it came from the database or form
394+
"""
395+
attr :field, Phoenix.HTML.FormField,
396+
doc: "a form field struct retrieved from the form, for example: @form[:email]"
397+
attr :options, :list, doc: "the options for the radio buttons in the fieldset"
398+
attr :checked_value, :string, doc: "the currently checked value"
399+
def radio_fieldset(%{field: %Phoenix.HTML.FormField{}} = assigns) do
400+
~H"""
Has a conversation. Original line has a conversation.
401+
<div phx-feedback-for={@field.name}>
402+
<.input :for={option <- @options}
403+
field={@field}
404+
id={"#{@field.id}_#{option}"}
405+
type="radio"
406+
label={String.capitalize(option)}
407+
value={option}
408+
checked={(@checked_value == option) || (@field.value == String.to_atom(option))}
409+
/>
410+
</div>
411+
"""
412+
end
413+
414+
390415
@doc """
391416
Renders a label.
392417
"""

‎lib/live_cal_web/live/calendar_live/form_component.ex

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ defmodule LiveCalWeb.CalendarLive.FormComponent do
2222
<.input field={@form[:name]} type="text" label="Name" />
2323
<.input field={@form[:description]} type="text" label="Description" />
2424
25+
<.radio_fieldset field={@form[:visibility]}
26+
options={Ecto.Enum.dump_values(Scheduling.Calendar, :visibility)}
27+
checked_value={@form.params["visibility"]}
28+
/>
29+
2530
<h2>Manage Events</h2>
2631
<.inputs_for :let={ef} field={@form[:events]}>
2732
<input type="hidden" name="calendar[events_sort][]" value={ef.index} />

‎priv/repo/migrations/20230614220250_create_calendars.exs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule LiveCal.Repo.Migrations.CreateCalendars do
55
create table(:calendars) do
66
add :name, :string
77
add :description, :string
8+
add :visibility, :string
89

910
timestamps()
1011
end

0 commit comments

Comments
 (0)
Please sign in to comment.