Skip to content

Commit ccb221a

Browse files
committed
[StimulusBundle] feat: normalize parameters names given to twig helper 'stimulus_action'
This allows giving a parameter name as 'myParam', and it will be printed as 'data-controller-name-my-param-param', which will be interpreted as 'myParam' by the Stimulus controller.
1 parent e29d5c2 commit ccb221a

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/StimulusBundle/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.13.0
4+
5+
- Normalize parameters names given to twig helper 'stimulus_action()'
6+
37
## 2.10.0
48

59
- Handle Stimulus outlets

src/StimulusBundle/src/Dto/StimulusAttributes.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public function addAction(string $controllerName, string $actionName, string $ev
7777
];
7878

7979
foreach ($parameters as $name => $value) {
80-
$this->attributes['data-'.$controllerName.'-'.$name.'-param'] = $this->getFormattedValue($value);
80+
$key = $this->normalizeKeyName($name);
81+
82+
$this->attributes['data-'.$controllerName.'-'.$key.'-param'] = $this->getFormattedValue($value);
8183
}
8284
}
8385

src/StimulusBundle/tests/Twig/StimulusTwigExtensionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ public static function provideRenderStimulusAction(): iterable
197197
'expectedString' => 'data-action="click->symfony--ux-dropzone--dropzone#onClick"',
198198
'expectedArray' => ['data-action' => 'click->symfony--ux-dropzone--dropzone#onClick'],
199199
];
200+
201+
yield 'normalize-name, with normalized parameters names' => [
202+
'controllerName' => 'my-controller',
203+
'actionName' => 'onClick',
204+
'eventName' => null,
205+
'parameters' => ['boolParam' => true, 'intParam' => 4, 'stringParam' => 'test'],
206+
'expectedString' => 'data-action="onClick"',
207+
'expectedString' => 'data-action="my-controller#onClick" data-my-controller-bool-param-param="true" data-my-controller-int-param-param="4" data-my-controller-string-param-param="test"',
208+
'expectedArray' => ['data-action' => 'my-controller#onClick', 'data-my-controller-bool-param-param' => 'true', 'data-my-controller-int-param-param' => '4', 'data-my-controller-string-param-param' => 'test'],
209+
];
200210
}
201211

202212
public function testAppendStimulusAction(): void

0 commit comments

Comments
 (0)