Skip to content

Commit

Permalink
Custom action support
Browse files Browse the repository at this point in the history
  • Loading branch information
DRaichev committed Oct 24, 2024
1 parent 468dc67 commit 8514346
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Turbo/src/Helper/TurboStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public static function prepend(string $target, string $html): string
*/
public static function replace(string $target, string $html, bool $morph = false): string
{
return self::wrap('replace', $target, $html, $morph ? ' method="morph"' : '');
return self::wrap('replace', $target, $html, $morph ? ['method="morph"'] : []);
}

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

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

private static function wrap(string $action, string $target, string $html, string $attr = ''): string
/**
* Custom Action.
*
* @param array<string> $attr
*/
public static function custom(string $action, string $target, string $html, array $attr = []): string
{
return self::wrap($action, $target, $html, $attr);
}

/**
* @param array<string> $attr
*/
private static function wrap(string $action, string $target, string $html, array $attr = []): string
{
// Join array elements with a space and prepend a leading space
$atrrString = empty($attr) ? '' : ' '.implode(' ', $attr);

return \sprintf(<<<EOHTML
<turbo-stream action="%s" targets="%s"%s>
<template>%s</template>
</turbo-stream>
EOHTML, $action, htmlspecialchars($target), $attr, $html);
EOHTML, $action, htmlspecialchars($target), $atrrString, $html);
}
}
12 changes: 12 additions & 0 deletions src/Turbo/src/TurboStreamResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,16 @@ public function refresh(?string $requestId = null): static

return $this;
}

/**
* @param array<string> $attr
*
* @return $this
*/
public function custom(string $action, string $target, string $html, array $attr = []): static
{
$this->setContent($this->getContent().TurboStream::custom($action, $target, $html, $attr));

return $this;
}
}
11 changes: 11 additions & 0 deletions src/Turbo/tests/Helper/TurboStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,15 @@ public function testRefreshWithId(): void
TurboStream::refresh('a"b')
);
}

public function testCustom(): void
{
$this->assertSame(<<<EOHTML
<turbo-stream action="customAction" targets="some[&quot;selector&quot;]" someAttr="someValue" boolAttr>
<template><div>content</div></template>
</turbo-stream>
EOHTML,
TurboStream::custom('customAction', 'some["selector"]', '<div>content</div>', ['someAttr="someValue"', 'boolAttr'])
);
}
}

0 comments on commit 8514346

Please sign in to comment.