Skip to content

Commit 0fabf86

Browse files
Antoine MakdessiAntoine Makdessi
Antoine Makdessi
authored and
Antoine Makdessi
committed
Add a basic Symfony Stimulus demo
1 parent 77a47d3 commit 0fabf86

File tree

7 files changed

+50
-18
lines changed

7 files changed

+50
-18
lines changed

.DS_Store

6 KB
Binary file not shown.

assets/.DS_Store

6 KB
Binary file not shown.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// A Stimulus JavaScript controller file
2+
// https://stimulus.hotwired.dev
3+
// @see templates/security/login.html.twig
4+
// More info on Symfony UX https://github.com/symfony/ux
5+
6+
import { Controller } from '@hotwired/stimulus';
7+
8+
/*
9+
* The following line makes this controller "lazy": it won't be downloaded until needed
10+
* See https://github.com/symfony/stimulus-bridge#lazy-controllers
11+
*/
12+
/* stimulusFetch: 'lazy' */
13+
export default class extends Controller {
14+
static targets = ['username', 'password']
15+
16+
// in a real application, the user/password should never be hardcoded
17+
// but for the demo application it's very convenient to do so
18+
19+
prefillJohnUser() {
20+
this.usernameTarget.value = 'john_user'
21+
this.passwordTarget.value = 'kitten'
22+
}
23+
24+
prefillJaneAdmin() {
25+
this.usernameTarget.value = 'jane_admin'
26+
this.passwordTarget.value = 'kitten'
27+
}
28+
29+
togglePasswordInputType() {
30+
if ('password' === this.passwordTarget.type) {
31+
this.passwordTarget.type = 'text'
32+
} else {
33+
this.passwordTarget.type = 'password'
34+
}
35+
}
36+
}

assets/login.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"devDependencies": {
33
"@fortawesome/fontawesome-free": "^5.8.1",
4-
"@symfony/stimulus-bridge": "^2.0.0",
5-
"@symfony/webpack-encore": "^1.0.0",
4+
"@hotwired/stimulus": "^3.0",
5+
"@symfony/stimulus-bridge": "^3.0",
6+
"@symfony/webpack-encore": "^1.7",
67
"bloodhound-js": "^1.2.3",
78
"bootstrap-sass": "^3.3.7",
89
"bootstrap-tagsinput": "^0.7.1",

templates/security/login.html.twig

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
{% block javascripts %}
66
{{ parent() }}
7-
{{ encore_entry_script_tags('login') }}
87
{% endblock %}
98

109
{% block main %}
@@ -14,20 +13,21 @@
1413
</div>
1514
{% endif %}
1615

17-
<div class="row">
16+
<div class="row" {{ stimulus_controller('login') }}{# @see assets/controllers/login-controller.js #}>
1817
<div class="col-sm-5">
1918
<div class="well">
2019
<form action="{{ path('security_login') }}" method="post">
2120
<fieldset>
2221
<legend><i class="fa fa-lock" aria-hidden="true"></i> {{ 'title.login'|trans }}</legend>
2322
<div class="form-group">
2423
<label for="username">{{ 'label.username'|trans }}</label>
25-
<input type="text" id="username" name="_username" value="{{ last_username }}" class="form-control"/>
24+
<input type="text" id="username" name="_username" value="{{ last_username }}" class="form-control" {{ stimulus_target('login', 'username') }} />
2625
</div>
2726
<div class="form-group">
2827
<label for="password">{{ 'label.password'|trans }}</label>
29-
<input type="password" id="password" name="_password" class="form-control" />
28+
<input type="password" id="password" name="_password" class="form-control" {{ stimulus_target('login', 'password') }} />
3029
</div>
30+
<button class="btn btn-primary pull-right" type="button" {{ stimulus_action('login', 'togglePasswordInputType') }}><i class="fa fa-eye"></i></button>
3131
<input type="hidden" name="_target_path" value="{{ app.request.get('redirect_to') }}"/>
3232
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}"/>
3333
<button type="submit" class="btn btn-primary">
@@ -50,18 +50,25 @@
5050
<th scope="col">{{ 'label.username'|trans }}</th>
5151
<th scope="col">{{ 'label.password'|trans }}</th>
5252
<th scope="col">{{ 'label.role'|trans }}</th>
53+
<th scope="col"></th>
5354
</tr>
5455
</thead>
5556
<tbody>
5657
<tr>
5758
<td>john_user</td>
5859
<td>kitten</td>
5960
<td><code>ROLE_USER</code> ({{ 'help.role_user'|trans }})</td>
61+
<td>
62+
<button class="btn btn-primary btn-sm" {{ stimulus_action('login', 'prefillJohnUser') }}><i class="fa fa-user"></i></button>
63+
</td>
6064
</tr>
6165
<tr>
6266
<td>jane_admin</td>
6367
<td>kitten</td>
6468
<td><code>ROLE_ADMIN</code> ({{ 'help.role_admin'|trans }})</td>
69+
<td>
70+
<button class="btn btn-primary btn-sm" {{ stimulus_action('login', 'prefillJaneAdmin') }}><i class="fa fa-user"></i></button>
71+
</td>
6572
</tr>
6673
</tbody>
6774
</table>

webpack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Encore
2121
* and one CSS file (e.g. app.css) if your JavaScript imports CSS.
2222
*/
2323
.addEntry('app', './assets/app.js')
24-
.addEntry('login', './assets/login.js')
2524
.addEntry('admin', './assets/admin.js')
2625
.addEntry('search', './assets/search.js')
2726

0 commit comments

Comments
 (0)