Skip to content

Commit a0defde

Browse files
committed
Simple implementation of event name normalizer
1 parent 1b5d5d2 commit a0defde

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/StimulusBundle/src/Dto/StimulusAttributes.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function addAction(string $controllerName, string $actionName, ?string $e
7575
$this->actions[] = [
7676
'controllerName' => $controllerName,
7777
'actionName' => $actionName,
78-
'eventName' => $eventName,
78+
'eventName' => $this->normalizeEventName($eventName),
7979
];
8080

8181
foreach ($parameters as $name => $value) {
@@ -218,6 +218,20 @@ private function normalizeControllerName(string $controllerName): string
218218
return preg_replace('/^@/', '', str_replace('_', '-', str_replace('/', '--', $controllerName)));
219219
}
220220

221+
/**
222+
* Normalize a Stimulus controller's action event name.
223+
*
224+
* @see https://stimulus.hotwired.dev/reference/actions
225+
*/
226+
private function normalizeEventName(?string $eventName): ?string
227+
{
228+
if (null === $eventName) {
229+
return null;
230+
}
231+
232+
return preg_replace_callback('/^.+(?=:)/', fn (array $matches): string => $this->normalizeControllerName($matches[0]), $eventName);
233+
}
234+
221235
/**
222236
* Normalize a Stimulus Value API key into its HTML equivalent ("kebab case").
223237
* Backport features from symfony/string.

0 commit comments

Comments
 (0)