-
-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #794 [Live] Dispatch browser events (weaverryan)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Live] Dispatch browser events | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | None | License | MIT Hi! I figured we'd need this eventually :). While building an component that opens/closes a modal, I needed a way to close the modal from a `LiveAction` in PHP. The way to do that is to dispatch a DOM event from the action, then listen to that with a tiny bit of JavaScript that closes the modal. Modeled after Livewire, like with emit(). Super simple. Also fixed a bug with `attributes.add()` on non-live components. Cheers! Commits ------- e2ca326 [Live] Dispatch browser events
- Loading branch information
Showing
17 changed files
with
270 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/LiveComponent/assets/test/controller/dispatch-event.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import {createTest, initComponent, shutdownTests} from '../tools'; | ||
|
||
describe('LiveController Event Dispatching Tests', () => { | ||
afterEach(() => { | ||
shutdownTests() | ||
}); | ||
|
||
it('dispatches events sent from an AJAX request', async () => { | ||
const test = await createTest({ }, (data: any) => ` | ||
<div ${initComponent(data, { | ||
name: 'simple-component', | ||
})}>Simple Component!</div> | ||
`); | ||
|
||
let eventCalled = false; | ||
test.element.addEventListener('fooEvent', (event: any) => { | ||
eventCalled = true; | ||
expect(event.detail).toEqual({ foo: 'bar' }); | ||
}); | ||
|
||
test.expectsAjaxCall() | ||
.willReturn(() => ` | ||
<div ${initComponent({}, { browserDispatch: [ | ||
{ event: 'fooEvent', payload: { foo: 'bar' } } | ||
]} | ||
)}>Simple Component!</div> | ||
`); | ||
|
||
await test.component.render(); | ||
expect(eventCalled).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,13 @@ | |
use Symfony\Contracts\Service\Attribute\Required; | ||
|
||
/** | ||
* Trait with shortcut methods useful for live components. | ||
* | ||
* @author Ryan Weaver <[email protected]> | ||
* | ||
* @experimental | ||
*/ | ||
trait ComponentEmitsTrait | ||
trait ComponentToolsTrait | ||
{ | ||
private LiveResponder $liveResponder; | ||
|
||
|
@@ -45,4 +47,9 @@ public function emitSelf(string $eventName, array $data = []): void | |
{ | ||
$this->liveResponder->emitSelf($eventName, $data); | ||
} | ||
|
||
public function dispatchBrowserEvent(string $eventName, array $payload = []): void | ||
{ | ||
$this->liveResponder->dispatchBrowserEvent($eventName, $payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.