Skip to content

Commit 2ad990b

Browse files
committed
Custom action support
1 parent 468dc67 commit 2ad990b

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/Turbo/src/Helper/TurboStream.php

Lines changed: 16 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,24 @@ 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+
* @param array<string> $attr
92+
*/
93+
public static function custom(string $action, string $target, string $html, array $attr = []): string
94+
{
95+
return self::wrap($action, $target, $html, $attr);
96+
}
97+
98+
private static function wrap(string $action, string $target, string $html, array $attr = []): string
9099
{
100+
// Join array elements with a space and prepend a leading space
101+
$atrrString = empty($attr) ? '' : ' ' . implode(' ', $attr);
102+
91103
return \sprintf(<<<EOHTML
92104
<turbo-stream action="%s" targets="%s"%s>
93105
<template>%s</template>
94106
</turbo-stream>
95-
EOHTML, $action, htmlspecialchars($target), $attr, $html);
107+
EOHTML, $action, htmlspecialchars($target), $atrrString, $html);
96108
}
97109
}

src/Turbo/src/TurboStreamResponse.php

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

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

src/Turbo/tests/Helper/TurboStreamTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,17 @@ 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" someAttr="someValue" boolAttr>
84+
<template>
85+
<div>content</div>
86+
</template>
87+
</turbo-stream>
88+
EOHTML,
89+
TurboStream::custom('customAction', 'some["selector"]', '<div>content</div>', ['someAttr="someValue"', 'boolAttr'])
90+
);
91+
}
7992
}

0 commit comments

Comments
 (0)