Form fields and ToggleGroup items keyed by name and value; shared stableKey

This commit is contained in:
ZacharyZhang-NY committed 2026-10-05 14:44:58 -04:00
1 parent 37f432caf8
commit b57d8e7da7
5 files changed
+105 -20

No files matched your search

+11 -6
View File
@@ -51,12 +51,7 @@ func RadioGroup[T comparable](c *ui.Context, value *T, items []RadioItem[T], opt
wrap.Children(func() {
r.Element = ui.RadioGroup(c, func() {
for _, it := range items {
key := slices.Index(*known, it.Value)
if key < 0 {
key = len(*known)
*known = append(*known, it.Value)
}
if radioGroupItem(c, k, d, value, it, key) {
if radioGroupItem(c, k, d, value, it, stableKey(known, it.Value)) {
r.changed = true
slog.Debug("mujica: radio_group choose", "label", it.Label)
}
@@ -103,3 +98,13 @@ func radioGroupItem[T comparable](c *ui.Context, k theme.Tokens, d theme.Density
})
return row.Changed()
}
// stableKey keys v by its first appearance in *known, so equal-looking values stay apart.
func stableKey[T comparable](known *[]T, v T) int {
i := slices.Index(*known, v)
if i < 0 {
i = len(*known)
*known = append(*known, v)
}
return i
}