diff --git a/pkg/app/node.go b/pkg/app/node.go index 4dfeb749..dcd182d0 100644 --- a/pkg/app/node.go +++ b/pkg/app/node.go @@ -348,7 +348,21 @@ func (m *nodeManager) Dismount(v UI) { } func (m *nodeManager) dismountHTML(v HTML) { - panic("not implemented") + for _, child := range v.body() { + m.Dismount(child) + } + + for _, handler := range v.events() { + m.dismountHTMLEventHandler(handler) + } + + v.setJSElement(nil) +} + +func (m *nodeManager) dismountHTMLEventHandler(handler eventHandler) { + if handler.close != nil { + handler.close() + } } func (m *nodeManager) dismountComponent(v Composer) error { diff --git a/pkg/app/node_test.go b/pkg/app/node_test.go index d298249d..93239525 100644 --- a/pkg/app/node_test.go +++ b/pkg/app/node_test.go @@ -346,6 +346,42 @@ func BenchmarkNodeManagerMount(b *testing.B) { } } +func TestNodeManagerDismount(t *testing.T) { + t.Run("html element is dismounted", func(t *testing.T) { + var m nodeManager + + div, err := m.Mount(1, Div()) + require.NoError(t, err) + + m.Dismount(div) + require.False(t, div.Mounted()) + require.Nil(t, div.JSValue()) + }) + + t.Run("html element child is dismounted", func(t *testing.T) { + var m nodeManager + + div, err := m.Mount(1, Div().Body( + Span(), + )) + require.NoError(t, err) + span := div.(HTML).body()[0] + + m.Dismount(div) + require.False(t, span.Mounted()) + }) + + t.Run("html element event handler is dismounted", func(t *testing.T) { + var m nodeManager + + div, err := m.Mount(1, Div(). + On("", func(ctx Context, e Event) {})) + require.NoError(t, err) + + m.Dismount(div) + }) +} + func TestNodeManagerCanUpdate(t *testing.T) { t.Run("elements with same type can be updated", func(t *testing.T) { var m nodeManager