feat(html): add autofocus helper
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
1d71ac4676
commit
f7843ae180
3 changed files with 15 additions and 0 deletions
|
|
@ -39,6 +39,7 @@ Accessibility-oriented helpers are also provided for common attribute patterns:
|
|||
- `Alt(node, text)`
|
||||
- `AriaHidden(node, hidden)`
|
||||
- `TabIndex(node, index)`
|
||||
- `AutoFocus(node)`
|
||||
|
||||
### Safety Guarantees
|
||||
|
||||
|
|
|
|||
5
node.go
5
node.go
|
|
@ -192,6 +192,11 @@ func TabIndex(n Node, index int) Node {
|
|||
return Attr(n, "tabindex", strconv.Itoa(index))
|
||||
}
|
||||
|
||||
// AutoFocus sets the autofocus attribute on an element node.
|
||||
func AutoFocus(n Node) Node {
|
||||
return Attr(n, "autofocus", "autofocus")
|
||||
}
|
||||
|
||||
func (n *elNode) Render(ctx *Context) string {
|
||||
if n == nil {
|
||||
return ""
|
||||
|
|
|
|||
|
|
@ -286,6 +286,15 @@ func TestTabIndexHelper(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAutoFocusHelper(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
node := AutoFocus(El("input"))
|
||||
got := node.Render(ctx)
|
||||
if !strings.Contains(got, `autofocus="autofocus"`) {
|
||||
t.Errorf("AutoFocus() = %q, want autofocus=\"autofocus\"", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestElNode_MultipleAttrs(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
node := Attr(Attr(El("a", Raw("link")), "href", "/home"), "class", "nav")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue