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

Added an attribute to represent the button types. #69

Merged
merged 3 commits into from
Jun 1, 2021
Merged
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
8 changes: 8 additions & 0 deletions Sources/Plot/API/HTMLAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,14 @@ public extension Attribute where Context == HTML.InputContext {
}
}

public extension Node where Context == HTML.ButtonContext {
/// Assign a button type to the element.
/// - parameter type: The button type to assign.
static func type(_ type: HTMLButtonType) -> Node {
.attribute(named: "type", value: type.rawValue)
}
}

public extension Node where Context == HTML.TextAreaContext {
/// Specify the number of columns that the text area should contain.
/// - parameter columns: The number of columns to specify.
Expand Down
15 changes: 15 additions & 0 deletions Sources/Plot/API/HTMLButtonType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Plot
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/

import Foundation

/// Enum that defines various button types that can be used with the
/// `<button/>` HTML element. For example `.button(.type(.submit))`.
public enum HTMLButtonType: String {
case button
case reset
case submit
}
8 changes: 6 additions & 2 deletions Tests/PlotTests/HTMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,15 @@ final class HTMLTests: XCTestCase {

func testButton() {
let html = HTML(.body(
.button(.name("Name"), .value("Value"), .text("Text"))
.button(.type(.button), .name("Name"), .value("Value"), .text("Text")),
.button(.type(.submit), .text("Submit"))
))

assertEqualHTMLContent(html, """
<body><button name="Name" value="Value">Text</button></body>
<body>\
<button type="button" name="Name" value="Value">Text</button>\
<button type="submit">Submit</button>\
</body>
""")
}

Expand Down