Skip to content

Commit 8514346

Browse files
committed
Custom action support
1 parent 468dc67 commit 8514346

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/Turbo/src/Helper/TurboStream.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ public static function prepend(string $target, string $html): string
3737
*/
3838
public static function replace(string $target, string $html, bool $morph = false): string
3939
{
40-
return self::wrap('replace', $target, $html, $morph ? ' method="morph"' : '');
40+
return self::wrap('replace', $target, $html, $morph ? ['method="morph"'] : []);
4141
}
4242

4343
/**
4444
* Updates the content of the element(s) designated by the target CSS selector.
4545
*/
4646
public static function update(string $target, string $html, bool $morph = false): string
4747
{
48-
return self::wrap('update', $target, $html, $morph ? ' method="morph"' : '');
48+
return self::wrap('update', $target, $html, $morph ? ['method="morph"'] : []);
4949
}
5050

5151
/**
@@ -86,12 +86,28 @@ public static function refresh(?string $requestId = null): string
8686
return \sprintf('<turbo-stream action="refresh" request-id="%s"></turbo-stream>', htmlspecialchars($requestId));
8787
}
8888

89-
private static function wrap(string $action, string $target, string $html, string $attr = ''): string
89+
/**
90+
* Custom Action.
91+
*
92+
* @param array<string> $attr
93+
*/
94+
public static function custom(string $action, string $target, string $html, array $attr = []): string
9095
{
96+
return self::wrap($action, $target, $html, $attr);
97+
}
98+
99+
/**
100+
* @param array<string> $attr
101+
*/
102+
private static function wrap(string $action, string $target, string $html, array $attr = []): string
103+
{
104+
// Join array elements with a space and prepend a leading space
105+
$atrrString = empty($attr) ? '' : ' '.implode(' ', $attr);
106+
91107
return \sprintf(<<<EOHTML
92108
<turbo-stream action="%s" targets="%s"%s>
93109
<template>%s</template>
94110
</turbo-stream>
95-
EOHTML, $action, htmlspecialchars($target), $attr, $html);
111+
EOHTML, $action, htmlspecialchars($target), $atrrString, $html);
96112
}
97113
}

src/Turbo/src/TurboStreamResponse.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,16 @@ public function refresh(?string $requestId = null): static
104104

105105
return $this;
106106
}
107+
108+
/**
109+
* @param array<string> $attr
110+
*
111+
* @return $this
112+
*/
113+
public function custom(string $action, string $target, string $html, array $attr = []): static
114+
{
115+
$this->setContent($this->getContent().TurboStream::custom($action, $target, $html, $attr));
116+
117+
return $this;
118+
}
107119
}

src/Turbo/tests/Helper/TurboStreamTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,15 @@ public function testRefreshWithId(): void
7676
TurboStream::refresh('a"b')
7777
);
7878
}
79+
80+
public function testCustom(): void
81+
{
82+
$this->assertSame(<<<EOHTML
83+
<turbo-stream action="customAction" targets="some[&quot;selector&quot;]" someAttr="someValue" boolAttr>
84+
<template><div>content</div></template>
85+
</turbo-stream>
86+
EOHTML,
87+
TurboStream::custom('customAction', 'some["selector"]', '<div>content</div>', ['someAttr="someValue"', 'boolAttr'])
88+
);
89+
}
7990
}

0 commit comments

Comments
 (0)