Skip to content

Commit b397d83

Browse files
committed
update LiveComponent docs
1 parent 12686c0 commit b397d83

File tree

1 file changed

+32
-49
lines changed

1 file changed

+32
-49
lines changed

src/LiveComponent/README.md

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,10 @@ use Symfony\UX\LiveComponent\Attribute\LiveProp;
180180
#[AsLiveComponent('random_number')]
181181
class RandomNumberComponent
182182
{
183-
/** @LiveProp */
183+
#[LiveProp]
184184
public int $min = 0;
185-
/** @LiveProp */
185+
186+
#[LiveProp]
186187
public int $max = 1000;
187188

188189
public function getRandomNumber(): string
@@ -201,14 +202,14 @@ when rendering the component:
201202
{{ component('random_number', { min: 5, max: 500 }) }}
202203
```
203204

204-
But what's up with those `@LiveProp` annotations? A property with
205-
the `@LiveProp` annotation (or `LiveProp` PHP 8 attribute) becomes
206-
a "stateful" property for this component. In other words, each time
207-
we click the "Generate a new number!" button, when the component
208-
re-renders, it will _remember_ the original values for the `$min` and
209-
`$max` properties and generate a random number between 5 and 500.
210-
If you forgot to add `@LiveProp`, when the component re-rendered,
211-
those two values would _not_ be set on the object.
205+
But what's up with those `LiveProp` attributes? A property with
206+
the `LiveProp` attribute becomes a "stateful" property for this
207+
component. In other words, each time we click the "Generate a
208+
new number!" button, when the component re-renders, it will
209+
_remember_ the original values for the `$min` and `$max` properties
210+
and generate a random number between 5 and 500. If you forgot to
211+
add `LiveProp`, when the component re-rendered, those two values
212+
would _not_ be set on the object.
212213

213214
In short: LiveProps are "stateful properties": they will always
214215
be set when rendering. Most properties will be LiveProps, with
@@ -264,11 +265,12 @@ the `writable=true` option:
264265

265266
class RandomNumberComponent
266267
{
267-
- /** @LiveProp() */
268-
+ /** @LiveProp(writable=true) */
268+
- #[LiveProp]
269+
+ #[LiveProp(writable: true)]
269270
public int $min = 0;
270-
- /** @LiveProp() */
271-
+ /** @LiveProp(writable=true) */
271+
272+
- #[LiveProp]
273+
+ #[LiveProp(writable: true)]
272274
public int $max = 1000;
273275

274276
// ...
@@ -423,8 +425,8 @@ want to add a "Reset Min/Max" button to our "random number"
423425
component that, when clicked, sets the min/max numbers back
424426
to a default value.
425427

426-
First, add a method with a `LiveAction` annotation (or PHP 8 attribute)
427-
above it that does the work:
428+
First, add a method with a `LiveAction` attribute above it that
429+
does the work:
428430

429431
```php
430432
// src/Components/RandomNumberComponent.php
@@ -437,9 +439,7 @@ class RandomNumberComponent
437439
{
438440
// ...
439441

440-
/**
441-
* @LiveAction
442-
*/
442+
#[LiveAction]
443443
public function resetMinMax()
444444
{
445445
$this->min = 0;
@@ -502,9 +502,7 @@ class RandomNumberComponent
502502
{
503503
// ...
504504

505-
/**
506-
* @LiveAction
507-
*/
505+
#[LiveAction]
508506
public function resetMinMax(LoggerInterface $logger)
509507
{
510508
$this->min = 0;
@@ -547,9 +545,7 @@ class RandomNumberComponent extends AbstractController
547545
{
548546
// ...
549547

550-
/**
551-
* @LiveAction
552-
*/
548+
#[LiveAction]
553549
public function resetMinMax()
554550
{
555551
// ...
@@ -698,9 +694,8 @@ class PostFormComponent extends AbstractController
698694
* the form renders fields with names like `name="post[title]"`.
699695
* We set fieldName="" so that this live prop doesn't collide
700696
* with that data. The value - initialFormData - could be anything.
701-
*
702-
* @LiveProp(fieldName="initialFormData")
703697
*/
698+
#[LiveProp(fieldName: 'initialFormData')]
704699
public ?Post $post = null;
705700

706701
/**
@@ -870,9 +865,7 @@ class PostFormComponent extends AbstractController
870865
{
871866
// ...
872867

873-
/**
874-
* @LiveAction()
875-
*/
868+
#[LiveAction]
876869
public function save(EntityManagerInterface $entityManager)
877870
{
878871
// shortcut to submit the form with form values
@@ -929,9 +922,7 @@ use Symfony\UX\LiveComponent\Attribute\LiveProp;
929922
#[AsLiveComponent('edit_post')]
930923
class EditPostComponent
931924
{
932-
/**
933-
* @LiveProp()
934-
*/
925+
#[LiveProp]
935926
public Post $post;
936927
}
937928
```
@@ -974,10 +965,8 @@ you can enable it via the `exposed` option:
974965

975966
class EditPostComponent
976967
{
977-
/**
978-
- * @LiveProp(exposed={})
979-
+ * @LiveProp(exposed={"title", "content"})
980-
*/
968+
- #[LiveProp]
969+
+ #[LiveProp(exposed: ['title', 'content'])]
981970
public Post $post;
982971

983972
// ...
@@ -1017,21 +1006,17 @@ class EditUserComponent
10171006
{
10181007
use ValidatableComponentTrait;
10191008

1020-
/**
1021-
* @LiveProp(exposed={"email", "plainPassword"})
1022-
* @Assert\Valid()
1023-
*/
1009+
#[LiveProp(exposed: ['email', 'plainPassword'])]
1010+
#[Assert\Valid]
10241011
public User $user;
10251012

1026-
/**
1027-
* @LiveProp()
1028-
* @Assert\IsTrue()
1029-
*/
1013+
#[LiveProp]
1014+
#[Assert\IsTrue]
10301015
public bool $agreeToTerms = false;
10311016
}
10321017
```
10331018

1034-
Be sure to add the `@Assert\IsValid` to any property where you want
1019+
Be sure to add the `Assert\IsValid` to any property where you want
10351020
the object on that property to also be validated.
10361021

10371022
Thanks to this setup, the component will now be automatically validated
@@ -1051,9 +1036,7 @@ class EditUserComponent
10511036
{
10521037
// ...
10531038

1054-
/**
1055-
* @LiveAction()
1056-
*/
1039+
#[LiveAction]
10571040
public function save()
10581041
{
10591042
// this will throw an exception if validation fails

0 commit comments

Comments
 (0)