Skip to content

Commit

Permalink
added form, mouse, and keyboard events (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
sliemeobn authored Jun 25, 2024
1 parent 1cc0cf6 commit fe837e0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Sources/Elementary/HtmlAttributes+Events.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
public extension HTMLAttribute where Tag: HTMLTrait.Attributes.Global {
static func on(_ event: HTMLAttributeValue.MouseEvent, _ script: String) -> Self { .init(on: event, script: script) }
static func on(_ event: HTMLAttributeValue.FormEvent, _ script: String) -> Self { .init(on: event, script: script) }
static func on(_ event: HTMLAttributeValue.KeyboardEvent, _ script: String) -> Self { .init(on: event, script: script) }
}

// TODO: window events, drag events, media events (more scoped)

public extension HTMLAttributeValue {
struct MouseEvent: HTMLEventName {
public var rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}

public static var click: Self { .init(rawValue: "click") }
public static var dblclick: Self { .init(rawValue: "dblclick") }
public static var mousedown: Self { .init(rawValue: "mousedown") }
public static var mousemove: Self { .init(rawValue: "mousemove") }
public static var mouseout: Self { .init(rawValue: "mouseout") }
public static var mouseover: Self { .init(rawValue: "mouseover") }
public static var mouseup: Self { .init(rawValue: "mouseup") }
public static var wheel: Self { .init(rawValue: "wheel") }
}

struct KeyboardEvent: HTMLEventName {
public var rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}

public static var keydown: Self { .init(rawValue: "keydown") }
public static var keypress: Self { .init(rawValue: "keypress") }
public static var keyup: Self { .init(rawValue: "keyup") }
}

struct FormEvent: HTMLEventName {
public var rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}

public static var blur: Self { .init(rawValue: "blur") }
public static var change: Self { .init(rawValue: "change") }
public static var contextmenu: Self { .init(rawValue: "contextmenu") }
public static var focus: Self { .init(rawValue: "focus") }
public static var input: Self { .init(rawValue: "input") }
public static var invalid: Self { .init(rawValue: "invalid") }
public static var reset: Self { .init(rawValue: "reset") }
public static var search: Self { .init(rawValue: "search") }
public static var select: Self { .init(rawValue: "select") }
public static var submit: Self { .init(rawValue: "submit") }
}
}

protocol HTMLEventName: RawRepresentable {}

extension HTMLAttribute {
init(on eventName: some HTMLEventName, script: String) {
self.init(name: "on\(eventName.rawValue)", value: script)
}
}
21 changes: 21 additions & 0 deletions Tests/ElementaryTests/AttributeRenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,25 @@ final class AttributeRenderingTests: XCTestCase {
#"<br id="1-2" data-bar="bazbaq">"#
)
}

func testRendersMouseEventAttribute() {
HTMLAssertEqual(
p(.on(.click, "doIt()")) {},
#"<p onclick="doIt()"></p>"#
)
}

func testRendersKeyboardEventAttribute() {
HTMLAssertEqual(
p(.on(.keydown, "doIt()")) {},
#"<p onkeydown="doIt()"></p>"#
)
}

func testRendersFormEventAttribute() {
HTMLAssertEqual(
p(.on(.blur, "doIt()")) {},
#"<p onblur="doIt()"></p>"#
)
}
}

0 comments on commit fe837e0

Please sign in to comment.