Skip to content

Commit c9c68af

Browse files
committed
fix: redundant events for toggle button
1 parent 59af4c9 commit c9c68af

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Follows the same pattern as Phoenix core input components for consistency
1414
- Maintains 100% backward compatibility - existing usage without labels continues to work unchanged
1515

16+
17+
### Fixed
18+
- Toggle button no longer emits an event when clicking the already-selected option, preventing redundant LiveView events in both standalone and form-integrated usage.
19+
1620
## [2025-11-07]
1721

1822
### Added

lib/peek_app_sdk/ui/odyssey/toggle_button.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ defmodule PeekAppSDK.UI.Odyssey.ToggleButton do
9494
:for={{option, index} <- Enum.with_index(@options)}
9595
type="button"
9696
value={to_string(option_value(option))}
97-
phx-click={@on_change}
97+
phx-click={
98+
if to_string(@selected) == to_string(option_value(option)),
99+
do: nil,
100+
else: @on_change
101+
}
98102
phx-target={@phx_target}
99103
class={
100104
[

test/peek_app_sdk/ui/odyssey_test.exs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ defmodule PeekAppSDK.UI.OdysseyTest do
2828
assert html =~ "bg-gray-50 text-blue-600"
2929
end
3030

31+
test "does not attach phx-click for selected option" do
32+
html =
33+
render_component(&PeekAppSDK.UI.Odyssey.odyssey_toggle_button/1, %{
34+
options: ["Minutes", "Hours", "Days"],
35+
selected: "Hours",
36+
on_change: "change_time_unit"
37+
})
38+
39+
refute html =~ ~r/value="Hours"[^>]*phx-click=/
40+
assert html =~ ~r/value="Minutes"[^>]*phx-click="change_time_unit"/
41+
end
42+
3143
test "shows unselected options with gray styling" do
3244
html =
3345
render_component(&PeekAppSDK.UI.Odyssey.odyssey_toggle_button/1, %{
@@ -70,7 +82,9 @@ defmodule PeekAppSDK.UI.OdysseyTest do
7082
assert html =~ "Phone Call"
7183
assert html =~ "hero-phone"
7284
# Should have the selected button highlighted (email)
73-
assert html =~ ~r/value="email"[^>]*phx-click="change_channel"/
85+
# Selected option should not include a click handler; unselected should
86+
refute html =~ ~r/value="email"[^>]*phx-click=/
87+
assert html =~ ~r/value="sms"[^>]*phx-click="change_channel"/
7488
end
7589

7690
test "automatically integrates with form fields when field is provided" do

0 commit comments

Comments
 (0)