-
-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #1946 [LiveComponent] Add 'live_action' twig function (pierre…
…dup) This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] Add 'live_action' twig function | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Issues | N/A | License | MIT Adds a new `live_action` twig function. This helps with creating the proper attributes to call a live action. I can never remember the syntax and need to look up the docs every time to add a live action. This will make the process much easier. Example Usage: ```diff - <button data-action="live#action" data-live-action-param="save">Save</button> + <button {{ live_action('save') }}>Save</button> ``` Additional Parameters: ```diff <button - data-action="live#action" - data-live-action-param="addItem" - data-live-id-param="{{ item.id }}" - data-live-item-name-param="CustomItem" + {{ live_action('addItem', {'id': item.id, 'itemName': 'CustomItem'}) }} >Add Item</button> ``` Adding Modifiers: ```diff <button - data-action="live#action:prevent" - data-live-action-param="debounce(300)|save" + {{ live_action('save', {}, {'debounce': 300, 'prevent': true}) }} >Save</button> ``` #### TODO: - [x] Add CHANGELOG entry - [x] Update docs Commits ------- b35807b [LiveComponent] Add 'live_action' twig function
- Loading branch information
Showing
8 changed files
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# CHANGELOG | ||
|
||
- Add `submitForm()` to `TestLiveComponent`. | ||
- Add `live_action` Twig function | ||
|
||
## 2.18.0 | ||
|
||
|
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
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
44 changes: 44 additions & 0 deletions
44
src/LiveComponent/tests/Unit/Twig/LiveComponentRuntimeTest.php
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,44 @@ | ||
<?php | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
namespace Symfony\UX\LiveComponent\Tests\Unit; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\UX\LiveComponent\Twig\LiveComponentRuntime; | ||
|
||
final class LiveComponentRuntimeTest extends KernelTestCase | ||
{ | ||
public function testGetLiveAction(): void | ||
{ | ||
$runtime = self::getContainer()->get('ux.live_component.twig.component_runtime'); | ||
\assert($runtime instanceof LiveComponentRuntime); | ||
|
||
$props = $runtime->liveAction('action-name'); | ||
$this->assertSame('data-action="live#action" data-live-action-param="action-name"', $props); | ||
|
||
$props = $runtime->liveAction('action-name', ['prop1' => 'val1', 'someProp' => 'val2']); | ||
$this->assertSame('data-action="live#action" data-live-prop1-param="val1" data-live-some-prop-param="val2" data-live-action-param="action-name"', $props); | ||
|
||
$props = $runtime->liveAction('action-name', ['prop1' => 'val1', 'prop2' => 'val2'], ['debounce' => 300]); | ||
$this->assertSame('data-action="live#action" data-live-prop1-param="val1" data-live-prop2-param="val2" data-live-action-param="debounce(300)|action-name"', \html_entity_decode($props)); | ||
$this->assertSame('data-action="live#action" data-live-prop1-param="val1" data-live-prop2-param="val2" data-live-action-param="debounce(300)|action-name"', $props); | ||
|
||
$props = $runtime->liveAction('action-name:prevent', ['pro1' => 'val1', 'prop2' => 'val2'], ['debounce' => 300]); | ||
$this->assertSame('data-action="live#action:prevent" data-live-pro1-param="val1" data-live-prop2-param="val2" data-live-action-param="debounce(300)|action-name"', \html_entity_decode($props)); | ||
$this->assertSame('data-action="live#action:prevent" data-live-pro1-param="val1" data-live-prop2-param="val2" data-live-action-param="debounce(300)|action-name"', $props); | ||
|
||
$props = $runtime->liveAction('action-name:prevent', [], ['debounce' => 300]); | ||
$this->assertSame('data-action="live#action:prevent" data-live-action-param="debounce(300)|action-name"', \html_entity_decode($props)); | ||
|
||
$props = $runtime->liveAction('action-name', [], [], 'keydown.esc'); | ||
$this->assertSame('data-action="keydown.esc->live#action" data-live-action-param="action-name"', \html_entity_decode($props)); | ||
} | ||
} |