Skip to content

Commit

Permalink
Merge pull request #14 from kirschbaum-development/dev
Browse files Browse the repository at this point in the history
Add query string support
  • Loading branch information
dvanscott authored Jan 11, 2023
2 parents 706ad4c + 8e100f6 commit af0558a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- name: Dependabot metadata
id: metadata
uses: dependabot/[email protected].3
uses: dependabot/[email protected].5
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `livewire-filters` will be documented in this file.

## 0.4 - 2023-01-10

- Added state preservation support via query string, controllable through `query_string_enabled` configuration key.

## 0.3 - 2022-03-05

- Added `getOptionId` method for better handling of setting an option's ID attribute when dealing with associative arrays
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ The included filters are made with [Tailwind CSS](https://tailwindcss.com) and t
php artisan vendor:publish --tag=livewire-filters-views
```

### Publishing config

Publishing the config file is only necessary to enable query string usage.
```bash
php artisan vendor:publish --tag=livewire-filters-config
```

## Usage

### Define your filters
Expand Down Expand Up @@ -277,4 +284,4 @@ Development of this package is sponsored by Kirschbaum, a developer driven compa

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
5 changes: 5 additions & 0 deletions config/livewire-filters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'query_string_enabled' => false,
];
24 changes: 21 additions & 3 deletions src/FilterComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,34 @@ abstract class FilterComponent extends Component

public array $options = [];

public mixed $value;
public mixed $value = null;

public function mount(Filter $filter): void
{
$this->key = $filter->key();
$this->meta = $filter->meta();
$this->options = $filter->options();
$this->value = $filter->value();

$this->initialValue = $this->value;
if (config('livewire-filters.query_string_enabled')) {
$this->value = request()?->query($filter->key()) ?? $filter->value();
} else {
$this->value = $filter->value();
}

$this->initialValue = $filter->value();
}

protected function queryString(): array
{
if (! config('livewire-filters.query_string_enabled')) {
return [];
}

return [
'value' => [
'as' => $this->key,
],
];
}

public function resetValue(): void
Expand Down
1 change: 1 addition & 0 deletions src/LivewireFiltersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function configurePackage(Package $package): void
{
$package
->name('livewire-filters')
->hasConfigFile()
->hasViews();
}

Expand Down

0 comments on commit af0558a

Please sign in to comment.