Skip to content

Commit e53c810

Browse files
committed
Fix showlace example
1 parent 2c1120f commit e53c810

3 files changed

Lines changed: 43 additions & 23 deletions

File tree

shoelace/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ var bootstrap_version = "latest"
1717
//go:embed templates/* script/*
1818
var embed_fs embed.FS
1919

20+
type Rating struct {
21+
Value string `json:"value" binding:"required"`
22+
}
23+
2024
func main() {
2125

2226
router := gin.Default()
@@ -44,5 +48,18 @@ func main() {
4448
c.JSON(http.StatusOK, gin.H{"message": "shoelace example"})
4549
})
4650

51+
var my_rating Rating = Rating{Value: "3"}
52+
router.GET("/rating", func(c *gin.Context) {
53+
c.JSON(http.StatusOK, my_rating)
54+
})
55+
router.POST("/rating", func(c *gin.Context) {
56+
var rating Rating
57+
if err := c.Bind(&rating); err != nil {
58+
c.AbortWithError(http.StatusBadRequest, err)
59+
}
60+
my_rating.Value = rating.Value
61+
c.JSON(http.StatusOK, my_rating)
62+
})
63+
4764
router.Run(":8080")
4865
}

shoelace/script/htmx.ext.shoelace.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,8 @@ function shouldInclude(elt) {
2424

2525
htmx.defineExtension('shoelace', {
2626
onEvent : function(name, evt) {
27-
if ((name === "htmx:configRequest") && (evt.detail.elt.tagName === 'FORM')) {
28-
evt.detail.elt.querySelectorAll(slTypes).forEach((elt) => {
29-
if (shouldInclude(elt)) {
30-
if (elt.tagName == 'SL-RATING') {
31-
evt.detail.parameters[elt.getAttribute('name')] = elt.value
32-
33-
} else {
34-
evt.detail.parameters[elt.name] = elt.value
35-
}
36-
}
37-
})
38-
// Prevent form submission if one or more fields are invalid.
39-
// evt.detail.elt is always a form as per the main if statement
40-
if (!evt.detail.elt.checkValidity()) {
41-
return false
42-
}
27+
if ((name === "htmx:configRequest") && (evt.detail.elt.tagName === 'SL-RATING')) {
28+
evt.detail.parameters[evt.detail.elt.getAttribute('name')] = evt.detail.elt.value
4329
}
4430
}
45-
})
31+
})

shoelace/templates/index.html.tmpl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
<head>
55

66
<meta charset='UTF-8'>
7-
<title>debug</title>
7+
<title>shoelace</title>
88

99
<script src="https://unpkg.com/htmx.org@{[{ .htmx_version }]}"></script>
1010
<script src="https://unpkg.com/htmx-ext-client-side-templates@{[{ .htmx_ext_version }]}/client-side-templates.js"></script>
11-
<script src="script/htmx.ext.shoelace.js"></script>
11+
<script src="https://unpkg.com/htmx-ext-json-enc@latest/json-enc.js"></script>
1212
<script src="https://unpkg.com/nunjucks@{[{ .nunjucks_version }]}/browser/nunjucks.js"></script>
1313

1414
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.16.0/cdn/themes/light.css" />
1515
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.16.0/cdn/shoelace-autoloader.js"></script>
16+
<script src="script/htmx.ext.shoelace.js"></script>
1617
</head>
1718

1819
<body hx-ext="shoelace">
@@ -21,20 +22,36 @@
2122
<p>
2223
<h3>Shoelace web component integration with HTMX</h3>
2324
<sl-button class="btn btn-primary"
24-
hx-target="#show"
25-
hx-get="/get_example"
26-
hx-trigger="click"
27-
nunjucks-template="show-template">
25+
hx-target="#show"
26+
hx-get="/get_example"
27+
hx-trigger="click"
28+
nunjucks-template="show-template">
2829
Click me
2930
</sl-button>
3031

3132
<p>
3233
<sl-alert id="show" variant="primary" open></sl-alert>
34+
<p>
35+
36+
<form hx-get="/rating"
37+
hx-trigger="load"
38+
nunjucks-template="rate-template">
39+
</form>
40+
3341

3442
<script id="show-template" type="x-tmpl-nunjucks">
3543
<sl-icon slot="icon" name="info-circle"></sl-icon>{{ message }}</sl-icon>
3644
</script>
3745

46+
<script id="rate-template" type="x-tmpl-nunjucks">
47+
<sl-rating name="value"
48+
hx-post="/rating"
49+
hx-trigger="click"
50+
hx-swap="outerHTML"
51+
hx-ext="json-enc"
52+
value="{{value}}">
53+
></sl-rating>
54+
</script>
3855
</div>
3956
</body>
4057

0 commit comments

Comments
 (0)