Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Livewire set and first rule for QueryString to Url attributes #222

Merged
merged 18 commits into from
Jul 18, 2024

Conversation

peterfox
Copy link
Collaborator

Changes

  • adds a Livewire 3.0 set to the repository.
  • adds a new rule for Livewire to convert query strings to using Url attributes.

Why

Livewire 3.0 is an official package for Laravel and a popular one. Livewire 3.0 introduced some changes to components from past versions. It makes sense for those rules to still live within the Laravel package.

Example of Rule use

The rule will perform a simple conversion. It avoids applying if there's a mismatch between what's in the array and the properties existing.

use Livewire\Component;

class MyComponent extends Component
{
    public string $something = '';

    public string $another = '';

    protected $queryString = [
        'something',
        'another',
    ];
}

becomes

use Livewire\Component;

class MyComponent extends Component
{
    #[\Livewire\Attributes\Url]
    public string $something = '';

    #[\Livewire\Attributes\Url]
    public string $another = '';
}

@peterfox peterfox self-assigned this Jun 22, 2024
@peterfox peterfox requested a review from MrPunyapal June 23, 2024 09:34
@MrPunyapal
Copy link

what will happen in this case?

public $foo;
public $search = '';
public $page = 1;

protected $queryString = [
    'foo',
    'search' => ['except' => ''],
    'page' => ['except' => 1],
];

@peterfox
Copy link
Collaborator Author

@MrPunyapal currently it will ignore it. Do you know what the translation of that would be with the attribute? I forgot that was an option available

@MrPunyapal
Copy link

MrPunyapal commented Jun 24, 2024

That feature is no longer available.

So first do key to value.

- 'property' => ['except' => 'something'],
+ 'property' 

Then just the same as what we did previously

Maybe this helps: https://livewire.laravel.com/docs/upgrading#url-query-string

@peterfox
Copy link
Collaborator Author

Okay cool. That should be simple enough to resolve 👍

@peterfox
Copy link
Collaborator Author

@MrPunyapal I've now adapted for the scenario mentioned where the array's key might be the property.

@MrPunyapal
Copy link

I guess we missed something? 🤔

The whole queryString property isn't deprecated 🤔

https://livewire.laravel.com/docs/upgrading#url-query-string

@peterfox
Copy link
Collaborator Author

Doesn't really matter if it's not deprecated. The rule is still useful for those who want to convert from Livewire 2 to 3 as it covers most of the scenarios. I guess the only thing that should be done is to ignore any instances where 'keep' => true is used.

@MrPunyapal
Copy link

MrPunyapal commented Jun 27, 2024

@peterfox I haven't found much in documentation 😃

Then after looking into code i think we need to handle more than one case? 🤔
Attribute: https://github.com/livewire/livewire/blob/main/src%2FFeatures%2FSupportQueryString%2FBaseUrl.php#L12-L18

Legacy support: https://github.com/livewire/livewire/blob/main/src%2FFeatures%2FSupportQueryString%2FSupportQueryString.php#L20-L26

@peterfox
Copy link
Collaborator Author

Okay cool, so it's probably best to create the attributes with the parameters if set.

@MrPunyapal
Copy link

@MrPunyapal currently it will ignore it. Do you know what the translation of that would be with the attribute? I forgot that was an option available

Yeah revert to this for now 🤔

@peterfox
Copy link
Collaborator Author

@MrPunyapal new version of the rule. Now covers transforming cases of 'as' and 'except' to be converted with the attributes into arguments.

@MrPunyapal
Copy link

looks good

@peterfox peterfox merged commit 58b57ee into main Jul 18, 2024
5 checks passed
@peterfox peterfox deleted the feature/livewire-query-string-to-url-attributes branch July 18, 2024 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants