Skip to content

Duplicate message forwarding in Group.UpdateΒ #710

@franco-stripe

Description

@franco-stripe

Description

In

huh/group.go

Lines 256 to 274 in a01a1e3

g.selector.Range(func(i int, field Field) bool {
switch msg := msg.(type) {
case tea.KeyMsg:
break
default:
m, cmd := field.Update(msg)
g.selector.Set(i, m.(Field))
cmds = append(cmds, cmd)
}
if g.selector.Index() == i {
m, cmd := field.Update(msg)
g.selector.Set(i, m.(Field))
cmds = append(cmds, cmd)
}
m, cmd := field.Update(updateFieldMsg{})
g.selector.Set(i, m.(Field))
cmds = append(cmds, cmd)
return true
})
if msg is not a tea.KeyMsg and if g.selector.Index() == i is true, then field.Update(msg) will be called twice for the same field and msg.

At the very least, this requires custom Field implementations to look out for duplicate custom message types being received.

For what it's worth, I was able to get the behavior I expected by changing the linked block to

g.selector.Range(func(i int, field Field) bool {
	var forwardMsg bool = true
	// Don't forward key messages to non-selected fields.
	if _, ok := msg.(tea.KeyMsg); ok && g.selector.Index() != i {
		forwardMsg = false
	}

	if forwardMsg {
		m, cmd := field.Update(msg)
		g.selector.Set(i, m.(Field))
		cmds = append(cmds, cmd)
	}

	m, cmd := field.Update(updateFieldMsg{})
	g.selector.Set(i, m.(Field))
	cmds = append(cmds, cmd)
	return true
})

Version

a01a1e3

Environment

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions