-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #25.
- Loading branch information
Showing
6 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Foundation | ||
|
||
import SwiftSoup | ||
|
||
/// An option in a ``Picker``. | ||
/// | ||
/// - SeeAlso: W3C [option](https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element) specification. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public struct Option: View { | ||
/// Creates an option with an optional value. | ||
public init(_ label: String, value: String? = nil) { | ||
self.label = label | ||
self.value = value | ||
} | ||
|
||
@_documentation(visibility: private) | ||
public func render(_ container: Element, environment: EnvironmentValues) throws { | ||
let element = try container.appendElement("option") | ||
if let value { | ||
try element.attr("value", value) | ||
} | ||
try element.text(label) | ||
} | ||
|
||
private let label: String | ||
private let value: String? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Foundation | ||
|
||
import SwiftSoup | ||
|
||
/// A picker represents a control for selecting amongst a set of options. | ||
/// | ||
/// ```swift | ||
/// struct MySiteContent: View { | ||
/// var body: some View { | ||
/// Body { | ||
/// Picker { | ||
/// Option("option1", value: "Option 1") | ||
/// Option("option2", value: "Option 2") | ||
/// } | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// - SeeAlso: W3C [select](https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element) specification. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public struct Picker<Content>: W3CElement where Content: View { | ||
@_documentation(visibility: private) | ||
public let tagName: String = "select" | ||
|
||
@_documentation(visibility: private) | ||
@ViewBuilder public let content: () -> Content | ||
|
||
/// Creates a picker view. | ||
public init(@ViewBuilder content: @escaping () -> Content) { | ||
self.content = content | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
import Testing | ||
|
||
import Slipstream | ||
|
||
struct OptionTests { | ||
@Test func noValue() throws { | ||
try #expect(renderHTML(Option("Label")) == #"<option>Label</option>"#) | ||
} | ||
|
||
@Test func withValue() throws { | ||
try #expect(renderHTML(Option("Label", value: "value")) == #"<option value="value">Label</option>"#) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Foundation | ||
import Testing | ||
|
||
import Slipstream | ||
|
||
struct PickerTests { | ||
@Test func emptyBlock() throws { | ||
try #expect(renderHTML(Picker {}) == #"<select></select>"#) | ||
} | ||
} |