Skip to content

Make the selected attribute conditional #178

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions Sources/HTMLKit/Abstraction/Attributes/BasicAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3065,16 +3065,26 @@ extension PropertyAttribute where Self: EmptyNode {
}
}

/// The protocol provides the element with the selected handler.
/// A type that provides the `selected` modifier
@_documentation(visibility: internal)
public protocol SelectedAttribute: Attribute {

/// The function represents the html-attribute 'selected'.
/// Mark a select option as default.
///
/// ```html
/// <tag selected />
/// ```swift
/// Select {
/// Option()
/// .value("lorem")
/// .selected()
/// }
/// .name("lorem")
/// .id("lorem")
/// ```
func selected() -> Self
///
/// - Parameter condition: Whether to default the option.
///
/// - Returns: The element
func selected(_ condition: Bool) -> Self
}

extension SelectedAttribute where Self: ContentNode {
Expand Down
9 changes: 7 additions & 2 deletions Sources/HTMLKit/Abstraction/Elements/InputElements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,13 @@ extension Option: GlobalAttributes, GlobalEventAttributes, GlobalAriaAttributes,
return mutate(value: value)
}

public func selected() -> Option {
return mutate(selected: "selected")
public func selected(_ condition: Bool = true) -> Option {

if condition {
return mutate(selected: "selected")
}

return self
}

public func popover(_ value: Values.Popover.State) -> Option {
Expand Down
16 changes: 14 additions & 2 deletions Tests/HTMLKitTests/AttributesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,13 @@ final class AttributesTests: XCTestCase {
return self.mutate(property: value.rawValue)
}

func selected() -> Tag {
return self.mutate(selected: "selected")
func selected(_ condition: Bool = true) -> Tag {

if condition {
return self.mutate(selected: "selected")
}

return self
}

func draw(_ value: String) -> Tag {
Expand Down Expand Up @@ -2119,11 +2124,18 @@ final class AttributesTests: XCTestCase {
func testSelectedAttribute() throws {

let view = TestView {
// unconditionally
Tag {}.selected()
// with a false condition
Tag {}.selected(false)
// with a true condition
Tag {}.selected(true)
}

XCTAssertEqual(try renderer.render(view: view),
"""
<tag selected="selected"></tag>\
<tag></tag>\
<tag selected="selected"></tag>
"""
)
Expand Down