Skip to content

Commit 736b4e3

Browse files
committed
Add fieldset radio button example for Ecto.Enum field
1 parent 8fe5829 commit 736b4e3

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

lib/live_cal/scheduling/event.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule LiveCal.Scheduling.Event do
66
field :destination, :string
77
field :name, :string
88
field :type, :string
9+
field :show_as, Ecto.Enum, values: [:free, :busy, :tbd], default: :busy
910

1011
belongs_to :calendar, LiveCal.Scheduling.Calendar
1112

@@ -15,7 +16,7 @@ defmodule LiveCal.Scheduling.Event do
1516
@doc false
1617
def changeset(event, attrs) do
1718
event
18-
|> cast(attrs, [:name, :type, :destination])
19-
|> validate_required([:name, :type, :destination])
19+
|> cast(attrs, [:name, :type, :destination, :show_as])
20+
|> validate_required([:name, :type, :destination, :show_as])
2021
end
2122
end

lib/live_cal_web/components/core_components.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,24 @@ defmodule LiveCalWeb.CoreComponents do
346346
"""
347347
end
348348

349+
def input(%{type: "radio"} = assigns) do
350+
~H"""
351+
<div phx-feedback-for={@name}>
352+
<.label for={@id}><%= @label %></.label>
353+
<input
354+
type="radio"
355+
id={@id}
356+
name={@name}
357+
value={@value}
358+
checked={@checked}
359+
class="rounded border-zinc-300 text-zinc-900 focus:ring-0"
360+
{@rest}
361+
/>
362+
<.error :for={msg <- @errors}><%= msg %></.error>
363+
</div>
364+
"""
365+
end
366+
349367
# All other inputs text, datetime-local, url, password, etc. are handled here...
350368
def input(assigns) do
351369
~H"""

lib/live_cal_web/live/calendar_live/form_component.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@ defmodule LiveCalWeb.CalendarLive.FormComponent do
2828
<.input type="text" field={ef[:name]} placeholder="name" />
2929
<.input type="select" field={ef[:type]} options={["-- select type --", "field trip"]} placeholder="type" />
3030
<.input :if={ef.params["type"] in ["field trip"]} type="text" field={ef[:destination]} placeholder="destination" />
31+
32+
<fieldset>
33+
<.input field={ef[:show_as]}
34+
id="show_as_busy"
35+
type="radio"
36+
label="Busy"
37+
value="busy"
38+
checked={(ef[:show_as].value == :busy) || (ef.params["show_as"] == "busy")}
39+
/>
40+
<.input field={ef[:show_as]}
41+
id="show_as_free"
42+
type="radio"
43+
label="Free"
44+
value="free"
45+
checked={(ef[:show_as].value == :free) || (ef.params["show_as"] == "free")}
46+
/>
47+
<.input field={ef[:show_as]}
48+
id="show_as_tbd"
49+
type="radio"
50+
label="Tbd"
51+
value="tbd"
52+
checked={(ef[:show_as].value == :tbd) || (ef.params["show_as"] == "tbd")}
53+
/>
54+
</fieldset>
55+
3156
<label>
3257
<input type="checkbox" name="calendar[events_drop][]" value={ef.index} class="hidden" />
3358
delete event

priv/repo/migrations/20230614220318_create_events.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule LiveCal.Repo.Migrations.CreateEvents do
66
add :name, :string
77
add :type, :string
88
add :destination, :string
9+
add :show_as, :string
910
add :calendar_id, references(:calendars, on_delete: :nothing)
1011

1112
timestamps()

0 commit comments

Comments
 (0)