Skip to content

Commit 803a99c

Browse files
committed
Custom action support
1 parent 468dc67 commit 803a99c

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/Turbo/src/Helper/TurboStream.php

Lines changed: 15 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,23 @@ 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+
public static function custom(string $action, string $target, string $html, array $attr = []): string
93+
{
94+
return self::wrap($action, $target, $html, $attr);
95+
}
96+
97+
private static function wrap(string $action, string $target, string $html, array $attr = []): string
9098
{
99+
// Join array elements with a space and prepend a leading space
100+
$atrrString = empty($attr) ? '' : ' ' . implode(' ', $attr);
101+
91102
return \sprintf(<<<EOHTML
92103
<turbo-stream action="%s" targets="%s"%s>
93104
<template>%s</template>
94105
</turbo-stream>
95-
EOHTML, $action, htmlspecialchars($target), $attr, $html);
106+
EOHTML, $action, htmlspecialchars($target), $atrrString, $html);
96107
}
97108
}

src/Turbo/src/TurboStreamResponse.php

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

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

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)