Skip to content

Commit 446fbe0

Browse files
committed
Simple implementation of event name normalizer
1 parent 1b80015 commit 446fbe0

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) {
@@ -236,6 +236,20 @@ private function normalizeControllerName(string $controllerName): string
236236
return preg_replace('/^@/', '', str_replace('_', '-', str_replace('/', '--', $controllerName)));
237237
}
238238

239+
/**
240+
* Normalize a Stimulus controller's action event name.
241+
*
242+
* @see https://stimulus.hotwired.dev/reference/actions
243+
*/
244+
private function normalizeEventName(?string $eventName): ?string
245+
{
246+
if (null === $eventName) {
247+
return null;
248+
}
249+
250+
return preg_replace_callback('/^.+(?=:)/', fn (array $matches): string => $this->normalizeControllerName($matches[0]), $eventName);
251+
}
252+
239253
/**
240254
* Normalize a Stimulus Value API key into its HTML equivalent ("kebab case").
241255
* Backport features from symfony/string.

0 commit comments

Comments
 (0)