Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fragment element #145

Merged
merged 1 commit into from
Aug 15, 2024
Merged

Add fragment element #145

merged 1 commit into from
Aug 15, 2024

Conversation

goring
Copy link
Contributor

@goring goring commented Aug 10, 2024

Added a new fragment element that is purely for ergonomics, allowing the ability to group elements without adding an extra wrapper element to the DOM.

I found this to be useful when I have functions that return nodes that need to be merged into the same parent without any element wrapping it.

Example Use Case

func TestFragment(t *testing.T) {
	expected := `<div><p>0</p><p>1</p><p>2</p><p>3</p><p>4</p></div>`
	nodes1 := []Node{
		P(nil,
			Text("1"),
		),
		P(nil,
			Text("2"),
		),
	}
	nodes2 := []Node{
		P(nil,
			Text("3"),
		),
		P(nil,
			Text("4"),
		),
	}
	el := Div(nil,
		P(nil,
			Text("0"),
		),
		Fragment(nodes1...),
		Fragment(nodes2...),
	)
	actual := el.Render()
	assert.Equal(t, expected, actual)
}

Copy link
Owner

@chasefleming chasefleming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! Thanks for adding this.

@chasefleming chasefleming merged commit bdff672 into chasefleming:main Aug 15, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants