Skip to content

Commit

Permalink
Don’t split event strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Sep 11, 2024
1 parent 58fd695 commit 2f6e18f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
9 changes: 2 additions & 7 deletions src/base/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use craft\base\Component as BaseComponent;
use craft\helpers\Html;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\web\View;
use putyourlightson\sprig\services\ComponentsService;
use putyourlightson\sprig\Sprig;
Expand Down Expand Up @@ -248,15 +247,11 @@ public static function swapOob(string $selector, string $template, array $variab
* Triggers client-side events.
* https://htmx.org/headers/hx-trigger/
*
* @param array|string $events An array of events, a string of comma-separated events, or a JSON encoded string (that is passed along as-is).
* @param array|string $events An array of events, one or more comma-separated events as a string, or a JSON encoded string (that is passed along as-is).
*/
public static function triggerEvents(array|string $events, string $on = 'load'): void
{
$decoded = Json::decodeIfJson($events);
if ($events === $decoded) {
if (is_string($events)) {
$events = StringHelper::split($events);
}
if (is_array($events)) {
$events = Json::encode(array_combine($events, $events));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ _Tests the existence and inclusion of the htmx script._

_Tests the Sprig variable methods._

![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Trigger events as strings.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Trigger events as string.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Trigger events as JSON string.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Trigger events as arrays.
![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Trigger events as arrays with key-value pairs.
Expand Down
6 changes: 3 additions & 3 deletions tests/pest/Feature/VariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
Craft::$app->getView()->setTemplatesPath(Craft::getAlias('@putyourlightson/sprig/test/templates'));
});

test('Trigger events as strings', function() {
test('Trigger events as string', function() {
getVariable()->triggerEvents('a,b,c');

expect(Craft::$app->getResponse()->getHeaders()->get('HX-Trigger'))
->toEqual('{"a":"a","b":"b","c":"c"}');
->toEqual('a,b,c');
});

test('Trigger events as JSON string', function() {
Expand Down Expand Up @@ -47,7 +47,7 @@
getVariable()->triggerEvents('a,b,c', 'swap');

expect(Craft::$app->getResponse()->getHeaders()->get('HX-Trigger-After-Swap'))
->toEqual('{"a":"a","b":"b","c":"c"}');
->toEqual('a,b,c');
});

describe('Sprig request', function() {
Expand Down

0 comments on commit 2f6e18f

Please sign in to comment.