generated from strangebuzz/MicroSymfony
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstimulus.html.twig
126 lines (107 loc) · 4 KB
/
stimulus.html.twig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{% extends 'base.html.twig' %}
{% set cdn_url = 'https://unpkg.com' %}
{% block preconnect %}
<link rel="preconnect" href="{{ preconnect(cdn_url) }}" />
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script type="module">
// @see https://stimulus.hotwired.dev/handbook/installing#using-other-build-systems
import { Application, Controller } from "{{ cdn_url }}/@hotwired/stimulus/dist/stimulus.js" // reference version: Stimulus 3.2.1
window.Stimulus = Application.start()
Stimulus.debug = {{ app.environment == 'dev' ? 'true' : 'false' }}
Stimulus.register("hello", class extends Controller {
static targets = [ "name", "greating", "dialog", "form"]
connect() {
console.log("Hello, Stimulus!", this.element)
}
greet() {
if (!this.formTarget.reportValidity()) {
return
}
this.greatingTarget.innerHTML = `Hello, ${this.nameTarget.value}!`
this.dialogTarget.showModal()
}
reset() {
this.nameTarget.value = ''
this.greatingTarget.innerHTML = ''
}
})
Stimulus.register("api", class extends Controller {
static targets = ["title", "slug"]
static values = {
"url": String
}
connect() {
console.log("Dataset: ", this.element.dataset) // list all data properties of the controller
}
slugify() {
let apiUrl = this.urlValue + '?title='+this.titleTarget.value
const slugTarget = this.slugTarget
fetch(apiUrl)
.then(function(response) {
return response.json()
})
.then(function(data) {
slugTarget.value = data.slug
})
.catch(function(error) {
console.log('An error occured. 😞', error);
})
}
})
</script>
{% endblock %}
{% block body %}
<div class="sub-heading">
<h2>Some cool and simple JavaScript with Stimulus 🪢</h2>
</div>
<p>...without the JS tooling stuff and complexity.</p>
<card>
<grid>
<div col="1/4">
<h3>Demo: form+dialog</h3>
</div>
<div col="3/4">
<div data-controller="hello">
<form data-hello-target="form">
<label for="name">Enter your name below and click on the <tag>Great</tag> button
<input name="name" required data-hello-target="name" type="text" />
</label>
<input type="submit" primary data-action="click->hello#greet:prevent" value="Great" />
</form>
<dialog data-hello-target="dialog">
<card>
<h2 data-hello-target="greating"></h2>
<form method="dialog">
<menu>
<button data-action="click->hello#reset">Close</button>
</menu>
</form>
</card>
</dialog>
</div>
</div>
</grid>
</card>
<card>
<grid>
<div col="1/4">
<h3>Demo: ajax call to API endpoint</h3>
</div>
<div col="3/4">
<div data-controller="api"
data-api-url-value="{{ path('app_slugifyacfion') }}"
>
<label for="title">Enter a blog post title below
<input name="title" required data-api-target="title" data-action="api#slugify" type="text" />
</label>
<label for="slug">
Slug (readonly):
<input name="slug" data-api-target="slug" type="text" readonly />
</label>
</div>
</div>
</grid>
</card>
{% endblock %}