-
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.
[feature] Add types for buttons and text fields. (#162)
- Loading branch information
Showing
6 changed files
with
144 additions
and
6 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
Sources/Slipstream/Documentation.docc/W3C/Forms/Forms-Button.md
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,7 @@ | ||
# ``Button`` | ||
|
||
## Topics | ||
|
||
### Supporting types | ||
|
||
- ``ButtonType`` |
7 changes: 7 additions & 0 deletions
7
Sources/Slipstream/Documentation.docc/W3C/Forms/Forms-TextField.md
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,7 @@ | ||
# ``TextField`` | ||
|
||
## Topics | ||
|
||
### Supporting types | ||
|
||
- ``TextFieldInputType`` |
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 |
---|---|---|
@@ -1,49 +1,66 @@ | ||
import SwiftSoup | ||
|
||
/// Constants that control the behavior of a ``Button`` when it is activated. | ||
/// | ||
/// - SeeAlso: W3C [button type](https://html.spec.whatwg.org/multipage/form-elements.html#attr-button-type) specification. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public enum ButtonType: String { | ||
case submit | ||
case reset | ||
} | ||
|
||
/// A control that initiates an action. | ||
/// | ||
/// - SeeAlso: W3C [button](https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element) specification. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public struct Button<Label>: View where Label: View { | ||
/// Creates a button that displays a custom label and executes a custom action | ||
/// when clicked. | ||
public init(action: String, @ViewBuilder label: @escaping () -> Label) { | ||
public init(action: String, type: ButtonType? = nil, @ViewBuilder label: @escaping () -> Label) { | ||
self.label = label | ||
self.action = action | ||
self.type = type | ||
} | ||
|
||
/// Creates a button that displays a custom label. | ||
public init(@ViewBuilder label: @escaping () -> Label) { | ||
public init(type: ButtonType? = nil, @ViewBuilder label: @escaping () -> Label) { | ||
self.label = label | ||
self.action = nil | ||
self.type = type | ||
} | ||
|
||
/// Creates a button that generates its label from a string. | ||
public init(_ text: String) where Label == DOMString { | ||
public init(_ text: String, type: ButtonType? = nil) where Label == DOMString { | ||
self.label = { | ||
DOMString(text) | ||
} | ||
self.action = nil | ||
self.type = type | ||
} | ||
|
||
/// Creates a button that generates its label from a string and executes a custom | ||
/// action when clicked. | ||
public init(_ text: String, action: String) where Label == DOMString { | ||
public init(_ text: String, action: String, type: ButtonType? = nil) where Label == DOMString { | ||
self.label = { | ||
DOMString(text) | ||
} | ||
self.action = action | ||
self.type = type | ||
} | ||
|
||
@ViewBuilder private let label: () -> Label | ||
private let action: String? | ||
private let type: ButtonType? | ||
|
||
@_documentation(visibility: private) | ||
public func render(_ container: Element, environment: EnvironmentValues) throws { | ||
let element = try container.appendElement("button") | ||
if let action { | ||
try element.attr("onclick", action) | ||
} | ||
if let type { | ||
try element.attr("type", type.rawValue) | ||
} | ||
try self.label().render(element, environment: environment) | ||
} | ||
} |
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