Skip to content

Commit

Permalink
Add SetFilterDefaultValue to DateRange Filter (#1796)
Browse files Browse the repository at this point in the history
* Add SetFilterDefaultValue to DateRange Filter

* Fix broken test

* Add docs

* Update ChangeLog

* Add short named array

* Update Docs

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Jul 27, 2024
1 parent 60409c8 commit b9b5d0e
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 13 deletions.
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 `laravel-livewire-tables` will be documented in this file

## [v3.3.4] - UNRELEASED
### New Features
- Added capability to setFilterDefaultValue for a DateRangeFilter by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1796

## [v3.3.3] - 2024-07-23
### New Features
- Add additional DateRangeFilter options by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1793
Expand Down
19 changes: 19 additions & 0 deletions docs/filter-types/filters-daterange.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ A full list of options is below, please see the Flatpickr documentation for refe
| time_24hr | Boolean | false | Displays time picker in 24 hour mode without AM/PM selection when enabled. |
| weekNumbers | Boolean | false | Enables display of week numbers in calendar. |

## setFilterDefaultValue

You may use this to set a default value for the filter that will be applied on first load (but may be cleared by the user). This should be an array:

```
DateRangeFilter::make('EMail Verified Range')
->setFilterDefaultValue(['minDate' => '2024-05-05', 'maxDate' => '2024-06-06'])
```
or
```
DateRangeFilter::make('EMail Verified Range')
->setFilterDefaultValue(['min' => '2024-05-05', 'max' => '2024-06-06'])
```
or
```
DateRangeFilter::make('EMail Verified Range')
->setFilterDefaultValue(['2024-05-05', '2024-06-06'])
```


## Configuration
By default, this filter will inject the Flatpickr JS Library and CSS. However, you can customise this behaviour using the configuration file.
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function handle(): void
$this->argument('name')
);

$livewireMakeCommand = new LivewireMakeCommand();
$livewireMakeCommand = new LivewireMakeCommand;

if ($livewireMakeCommand->isReservedClassName($name = $this->parser->className())) {
$this->line("<fg=red;options=bold>Class is reserved:</> {$name}");
Expand Down Expand Up @@ -184,7 +184,7 @@ private function getClassesList(string $file): array
*/
private function generateColumns(string $modelName): string
{
$model = new $modelName();
$model = new $modelName;

if ($model instanceof Model === false) {
throw new \Exception('Invalid model given.');
Expand Down
39 changes: 39 additions & 0 deletions src/Views/Filters/DateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,45 @@ public function getDefaultValue(): array
return [];
}

public function getFilterDefaultValue(): array
{
return $this->filterDefaultValue ?? [];
}

public function hasFilterDefaultValue(): bool
{
return ! is_null($this->filterDefaultValue);
}

public function setFilterDefaultValue($value): self
{
if (is_array($value)) {
$minDate = '';
$maxDate = '';

if (array_key_exists('minDate', $value)) {
$minDate = $value['minDate'];
} elseif (array_key_exists('min', $value)) {
$minDate = $value['min'];
} elseif (array_key_exists(0, $value)) {
$minDate = $value[0];
}

if (array_key_exists('maxDate', $value)) {
$maxDate = $value['maxDate'];
} elseif (array_key_exists('max', $value)) {
$maxDate = $value['max'];
} elseif (array_key_exists(1, $value)) {
$maxDate = $value[1];
}
$this->filterDefaultValue = ['minDate' => $minDate, 'maxDate' => $maxDate];
} else {
$this->filterDefaultValue = ['minDate' => $value, 'maxDate' => $value];
}

return $this;
}

public function getFilterPillValue($value): string|array|null
{
$validatedValue = $this->validate($value);
Expand Down
6 changes: 3 additions & 3 deletions tests/DataTableComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function test_default_fingerprint_will_always_be_the_same_for_same_datata

public function test_default_datatable_fingerprints_will_be_different_for_each_table(): void
{
$mockTable = new class() extends PetsTable {};
$mockTable = new class extends PetsTable {};

$this->assertNotSame($this->basicTable->getDataTableFingerprint(), $mockTable->getDataTableFingerprint());
}
Expand All @@ -64,7 +64,7 @@ public function test_default_fingerprint_will_be_url_friendy(): void
{
$mocks = [];
for ($i = 0; $i < 9; $i++) {
$mocks[$i] = new class() extends PetsTable {};
$mocks[$i] = new class extends PetsTable {};
$this->assertFalse(filter_var('http://'.$mocks[$i]->getDataTableFingerprint().'.dev', FILTER_VALIDATE_URL) === false);
}
// control
Expand All @@ -74,7 +74,7 @@ public function test_default_fingerprint_will_be_url_friendy(): void
public function test_minimum_one_column_expected(): void
{
$this->expectException(\Rappasoft\LaravelLivewireTables\Exceptions\NoColumnsException::class);
$table = new NoColumnsTable();
$table = new NoColumnsTable;
$table->boot();
$table->bootedComponentUtilities();
$table->bootedWithData();
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ protected function setUp(): void

if (! Breed::where('id', 1)->get()) {
include_once __DIR__.'/../database/migrations/create_test_tables.php.stub';
(new \CreateTestTables())->down();
(new \CreateTestTables())->up();
(new \CreateTestTables)->down();
(new \CreateTestTables)->up();

Species::insert([
['id' => 1, 'name' => 'Cat'],
Expand Down Expand Up @@ -88,7 +88,7 @@ protected function setUp(): void
protected function setupBasicTable()
{
$view = view('livewire-tables::datatable');
$this->basicTable = new PetsTable();
$this->basicTable = new PetsTable;
$this->basicTable->boot();
$this->basicTable->bootedComponentUtilities();
$this->basicTable->bootedWithData();
Expand All @@ -104,7 +104,7 @@ protected function setupBasicTable()
protected function setupSpeciesTable()
{
$view = view('livewire-tables::datatable');
$this->speciesTable = new SpeciesTable();
$this->speciesTable = new SpeciesTable;
$this->speciesTable->boot();
$this->speciesTable->bootedComponentUtilities();
$this->speciesTable->bootedWithData();
Expand All @@ -121,7 +121,7 @@ protected function setupUnpaginatedTable()
{

$view = view('livewire-tables::datatable');
$this->unpaginatedTable = new PetsTableUnpaginated();
$this->unpaginatedTable = new PetsTableUnpaginated;
$this->unpaginatedTable->boot();
$this->unpaginatedTable->bootedComponentUtilities();
$this->unpaginatedTable->bootedWithData();
Expand Down
6 changes: 3 additions & 3 deletions tests/Traits/WithMountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function test_mounttable_gets_correct_first_item(): void
{
$view = view('livewire-tables::datatable');

$table = new PetsTableMount();
$table = new PetsTableMount;
$table->boot();
$table->mount(102);
$table->bootedComponentUtilities();
Expand All @@ -29,7 +29,7 @@ public function test_mounttable_gets_correct_first_item(): void
$this->assertNotSame(strtoupper($rows->first()->name), 'CHICO');
$this->assertNotSame(strtoupper($rows->first()->name), 'CARTMAN');

$table2 = new PetsTableMount();
$table2 = new PetsTableMount;
$table2->boot();
$table2->mount(202);
$table2->bootedComponentUtilities();
Expand All @@ -46,7 +46,7 @@ public function test_mounttable_gets_correct_first_item(): void
$this->assertNotSame(strtoupper($rows2->first()->name), 'CARTMAN');
$this->assertNotSame(strtoupper($rows2->first()->name), 'MAY');

$table3 = new PetsTableMount();
$table3 = new PetsTableMount;
$table3->boot();
$table3->mount();
$table3->bootedComponentUtilities();
Expand Down
39 changes: 39 additions & 0 deletions tests/Views/Filters/DateRangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,43 @@ public function test_can_set_custom_filter_view(): void
$filter->setCustomView('test-custom-filter-view');
$this->assertSame('test-custom-filter-view', $filter->getViewPath());
}

public function test_can_set_default_value_by_string(): void
{
$filter = DateRangeFilter::make('Active');
$this->assertFalse($filter->hasFilterDefaultValue());
$filter->setFilterDefaultValue('2024-04-04');
$this->assertTrue($filter->hasFilterDefaultValue());
$this->assertSame(['minDate' => '2024-04-04', 'maxDate' => '2024-04-04'], $filter->getFilterDefaultValue());

}

public function test_can_set_default_value_by_named_array(): void
{
$filter = DateRangeFilter::make('Active');
$this->assertFalse($filter->hasFilterDefaultValue());
$filter->setFilterDefaultValue(['minDate' => '2024-05-04', 'maxDate' => '2024-06-04']);
$this->assertTrue($filter->hasFilterDefaultValue());
$this->assertSame(['minDate' => '2024-05-04', 'maxDate' => '2024-06-04'], $filter->getFilterDefaultValue());

}

public function test_can_set_default_value_by_short_named_array(): void
{
$filter = DateRangeFilter::make('Active');
$this->assertFalse($filter->hasFilterDefaultValue());
$filter->setFilterDefaultValue(['min' => '2024-05-04', 'max' => '2024-06-04']);
$this->assertTrue($filter->hasFilterDefaultValue());
$this->assertSame(['minDate' => '2024-05-04', 'maxDate' => '2024-06-04'], $filter->getFilterDefaultValue());

}

public function test_can_set_default_value_by_numbered_array(): void
{
$filter = DateRangeFilter::make('Active');
$this->assertFalse($filter->hasFilterDefaultValue());
$filter->setFilterDefaultValue(['2024-06-04', '2024-07-04']);
$this->assertTrue($filter->hasFilterDefaultValue());
$this->assertSame(['minDate' => '2024-06-04', 'maxDate' => '2024-07-04'], $filter->getFilterDefaultValue());
}
}

0 comments on commit b9b5d0e

Please sign in to comment.