Skip to content

Commit

Permalink
Added an attribute to represent the button types. (#69)
Browse files Browse the repository at this point in the history
* Added `HTMLButtonType`.
* Added a test for button type.
  • Loading branch information
daveverwer authored Jun 1, 2021
1 parent 1b10c15 commit 0320a8d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Sources/Plot/API/HTMLAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,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 @@ -608,11 +608,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

0 comments on commit 0320a8d

Please sign in to comment.