Skip to content

Commit

Permalink
Make it possible to add attributes to wrapper components (#63)
Browse files Browse the repository at this point in the history
When an attribute is added to a component that acts as a wrapper for an element-based
component, that attribute is now added directly to the wrapped, underlying element. That
makes it possible to append things like class names to existing, custom components.
  • Loading branch information
JohnSundell authored May 12, 2021
1 parent 0ca2d6b commit f4fd706
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Sources/Plot/API/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ internal extension Node where Context == Any {
static func modifiedComponent(_ component: ModifiedComponent) -> Node {
Node { renderer in
renderer.renderComponent(component.base,
deferredAttributes: component.deferredAttributes,
deferredAttributes: component.deferredAttributes + renderer.deferredAttributes,
environmentOverrides: component.environmentOverrides
)
}
Expand Down
19 changes: 18 additions & 1 deletion Tests/PlotTests/HTMLComponentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ final class HTMLComponentTests: XCTestCase {
XCTAssertEqual(html, #"<p class="one two three">Hello</p>"#)
}

func testNotAppendingEmptyClassNames() {
func testNotAppendingEmptyClasses() {
let html = Paragraph("Hello")
.class("")
.class("one")
Expand All @@ -99,6 +99,23 @@ final class HTMLComponentTests: XCTestCase {
XCTAssertEqual(html, #"<p class="one two">Hello</p>"#)
}

func testAppendingClassesToWrappingComponents() {
struct InnerWrapper: Component {
var body: Component {
Paragraph("Hello").class("one")
}
}

struct OuterWrapper: Component {
var body: Component {
InnerWrapper().class("two")
}
}

let html = OuterWrapper().class("three").render()
XCTAssertEqual(html, #"<p class="one two three">Hello</p>"#)
}

func testReplacingClass() {
let html = Paragraph("Hello")
.class("one")
Expand Down

0 comments on commit f4fd706

Please sign in to comment.