Skip to content

Commit

Permalink
Add Customisation for "Search" in QueryString (#2017)
Browse files Browse the repository at this point in the history
* AddSearchQueryString

* Add tests for hasQueryStringAliasFor...

* Additional Tests For SearchQueryString

* Move Livewire to Visuals section

* Simplify the queryString methods

* Add hasQueryStringAliasForSearch test

* PCOV Runs as PHPUnit not ParaTest

* Remove PCOV From Workflows Where Not Used

* Fix styling

---------

Co-authored-by: lrljoe <lrljoe@users.noreply.github.com>
  • Loading branch information
lrljoe and lrljoe authored Oct 27, 2024
1 parent 92dc8fb commit 351757e
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests-pcov-pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Run Unit Tests
run: php ./vendor/bin/paratest --cache-directory=".phpunit.cache/code-coverage" --strict-coverage --coverage-clover ./coverage.xml
run: php ./vendor/bin/phpunit --cache-directory=".phpunit.cache/code-coverage" --strict-coverage --coverage-clover ./coverage.xml

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run-tests-pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
name: PULL PHP-${{ matrix.php }} - Laravel-10
env:
extensionKey: phpextensions-${{ matrix.os }}-P${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pcov, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, :psr
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, :psr

steps:
- name: Checkout code
Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
name: PULL PHP-${{ matrix.php }} - Laravel-11
env:
extensionKey: phpextensions-${{ matrix.os }}-P${{ matrix.php }}-L${{ matrix.laravel }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pcov, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, :psr
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, :psr

steps:
- name: Checkout code
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
name: PHP-${{ matrix.php }} - Laravel-10
env:
extensionKey: phpextensions-${{ matrix.os }}-P${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pcov, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, :psr
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, :psr

steps:
- name: Checkout code
Expand Down Expand Up @@ -102,7 +102,7 @@ jobs:
name: PHP-${{ matrix.php }} - Laravel-11
env:
extensionKey: phpextensions-${{ matrix.os }}-P${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pcov, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, :psr
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo, :psr

steps:
- name: Checkout code
Expand Down
51 changes: 51 additions & 0 deletions docs/datatable/query-string.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,55 @@ public function configure(): void
{
$this->setQueryStringAliasForFilter('filtervalues');
}
```

## Search

The search query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods:

### setQueryStringStatusForSearch

Enable/disable the query string for search

```php
public function configure(): void
{
$this->setQueryStringStatusForSearch(true);
$this->setQueryStringStatusForSearch(false);
}
```

### setQueryStringForSearchEnabled

Enable the query string for search

```php
public function configure(): void
{
// Shorthand for $this->setQueryStringStatusForSearch(true)
$this->setQueryStringForSearchEnabled();
}
```

### setQueryStringForSearchDisabled

Disable the query string for search

```php
public function configure(): void
{
// Shorthand for $this->setQueryStringStatusForSearch(false)
$this->setQueryStringForSearchDisabled();
}
```

### setQueryStringAliasForSearch

Change the Alias in the URL for the search, otherwise defaults to "$tablename-search"

```php
public function configure(): void
{
$this->setQueryStringAliasForSearch('search');
}
```
9 changes: 3 additions & 6 deletions src/Traits/Core/QueryStrings/HasQueryStringForFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ trait HasQueryStringForFilter

protected function queryStringHasQueryStringForFilter(): array
{
if ($this->queryStringForFilterIsEnabled()) {
return [
return ($this->queryStringForFilterIsEnabled()) ?
[
'appliedFilters' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForFilter()],
'filterComponents' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForFilter()],
];
}

return [];
] : [];
}

public function setupQueryStringStatusForFilter(): void
Expand Down
81 changes: 81 additions & 0 deletions src/Traits/Core/QueryStrings/HasQueryStringForSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings;

use Livewire\Attributes\Locked;

trait HasQueryStringForSearch
{
#[Locked]
public ?bool $queryStringStatusForSearch;

protected ?string $queryStringAliasForSearch;

protected function queryStringHasQueryStringForSearch(): array
{
return ($this->queryStringForSearchEnabled() && $this->searchIsEnabled()) ? ['search' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSearch()]] : [];

}

public function setupQueryStringStatusForSearch(): void
{
if (! $this->hasQueryStringStatusForSearch()) {
$this->setQueryStringForSearchEnabled();
}
}

public function hasQueryStringStatusForSearch(): bool
{
return isset($this->queryStringStatusForSearch);
}

public function getQueryStringStatusForSearch(): bool
{
return $this->queryStringStatusForSearch ?? true;
}

public function queryStringForSearchEnabled(): bool
{
$this->setupQueryStringStatusForSearch();

return $this->getQueryStringStatusForSearch() && $this->searchIsEnabled();
}

public function setQueryStringStatusForSearch(bool $status): self
{
$this->queryStringStatusForSearch = $status;

return $this;
}

public function setQueryStringForSearchEnabled(): self
{
$this->setQueryStringStatusForSearch(true);

return $this;
}

public function setQueryStringForSearchDisabled(): self
{
$this->setQueryStringStatusForSearch(false);

return $this;
}

public function hasQueryStringAliasForSearch(): bool
{
return isset($this->queryStringAliasForSearch);
}

public function getQueryStringAliasForSearch(): string
{
return $this->queryStringAliasForSearch ?? $this->getQueryStringAlias().'-search';
}

public function setQueryStringAliasForSearch(string $alias): self
{
$this->queryStringAliasForSearch = $alias;

return $this;
}
}
13 changes: 2 additions & 11 deletions src/Traits/WithSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use Livewire\Attributes\Locked;
use Rappasoft\LaravelLivewireTables\Events\SearchApplied;
use Rappasoft\LaravelLivewireTables\Traits\Configuration\SearchConfiguration;
use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryStringForSearch;
use Rappasoft\LaravelLivewireTables\Traits\Helpers\SearchHelpers;

trait WithSearch
{
use SearchConfiguration,
SearchHelpers;
use HasQueryStringForSearch;

public string $search = '';

Expand All @@ -38,17 +40,6 @@ trait WithSearch

protected bool $trimSearchString = false;

protected function queryStringWithSearch(): array
{
if ($this->queryStringIsEnabled() && $this->searchIsEnabled()) {
return [
'search' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAlias().'-search'],
];
}

return [];
}

// TODO
public function applySearch(): Builder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ public function configure(): void
$this->assertSame('table-filters', $mock->getQueryStringAliasForFilter());
$mock->setQueryStringAliasForFilter('pet-filters');
$this->assertSame('pet-filters', $mock->getQueryStringAliasForFilter());
$this->assertTrue($mock->hasQueryStringAliasForFilter());
}
}
110 changes: 110 additions & 0 deletions tests/Traits/Core/QueryStrings/QueryStringForSearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Core\QueryStrings;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Depends;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
use Rappasoft\LaravelLivewireTables\Tests\TestCase;

final class QueryStringForSearchTest extends TestCase
{
public function test_can_get_default_search_query_string_status(): void
{
$mock = new class extends PetsTable
{
public ?array $testAttributesArray;

public function configure(): void
{
$this->setDataTableFingerprint('test');
}
};

$mock->configure();
$mock->boot();

$this->assertSame(true, $mock->getQueryStringStatusForSearch());
}

public function test_can_disable_search_query_string_status(): void
{
$mock = new class extends PetsTable
{
public ?array $testAttributesArray;

public function configure(): void
{
$this->setDataTableFingerprint('test');
$this->setQueryStringForSearchDisabled();
}
};

$mock->configure();
$mock->boot();

$this->assertSame(false, $mock->getQueryStringStatusForSearch());
}

public function test_can_enable_search_query_string_status(): void
{
$mock = new class extends PetsTable
{
public ?array $testAttributesArray;

public function configure(): void
{
$this->setDataTableFingerprint('test');
$this->setQueryStringForSearchDisabled();
}
};

$mock->configure();
$mock->boot();

$this->assertSame(false, $mock->getQueryStringStatusForSearch());
$mock->setQueryStringForSearchEnabled();
$this->assertSame(true, $mock->getQueryStringStatusForSearch());

}

public function test_can_get_default_search_query_string_alias(): void
{
$mock = new class extends PetsTable
{
public ?array $testAttributesArray;

public function configure(): void
{
$this->setDataTableFingerprint('test');
}
};

$mock->configure();
$mock->boot();

$this->assertSame('table-search', $mock->getQueryStringAliasForSearch());
}

public function test_can_change_default_search_query_string_alias(): void
{
$mock = new class extends PetsTable
{
public ?array $testAttributesArray;

public function configure(): void
{
$this->setDataTableFingerprint('test');
}
};

$mock->configure();
$mock->boot();

$this->assertFalse($mock->hasQueryStringAliasForSearch());
$this->assertSame('table-search', $mock->getQueryStringAliasForSearch());
$mock->setQueryStringAliasForSearch('pet-search');
$this->assertSame('pet-search', $mock->getQueryStringAliasForSearch());
$this->assertTrue($mock->hasQueryStringAliasForSearch());
}
}
Loading

0 comments on commit 351757e

Please sign in to comment.