Skip to content

Commit 8bcca7c

Browse files
authored
v3.3.4 - Development to Master (#1799)
* Add SetFilterDefaultValue to DateRange Filter (#1796) --------- Co-authored-by: lrljoe <[email protected]> * Add Localised Pills for DateRangeFilter --------- Co-authored-by: lrljoe <[email protected]> * Tidying Filters - Migrating Carbon usage into Trait, Adding Lifecycle Hooks (#1798) * Tweaks to handle dates centrally * Add localisation for DateFilter, DateTimeFilter, migrate DateRangeFilter to use Core Trait * Add Filter Lifecycle Hooks * Add Search Lifecycle Hook * Update DateFilter, DateTimeFilter validate returns * Update DateRangeFilterTest * Add Pills Locale Tests to DateTests * Remove superfluous method --------- Co-authored-by: lrljoe <[email protected]> * Update ChangeLog * Update docs for setPillsLocale (#1800) --------- Co-authored-by: lrljoe <[email protected]>
1 parent 5a3d471 commit 8bcca7c

20 files changed

+525
-204
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All notable changes to `laravel-livewire-tables` will be documented in this file
44

5+
## [v3.3.4] - 2024-07-27
6+
### New Features
7+
- Added capability to setFilterDefaultValue for a DateRangeFilter by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1796
8+
- Add localised pill values for DateFilter, DateTimeFilter, DateRangeFilter by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1797
9+
10+
### Tweaks
11+
- Migrating Carbon usage into Trait, Adding Filter/Search Lifecycle Hooks by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1798
12+
513
## [v3.3.3] - 2024-07-23
614
### New Features
715
- Add additional DateRangeFilter options by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1793

docs/filter-types/filters-date.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function filters(): array
3333
}
3434
```
3535

36+
## setFilterDefaultValue
3637
Date filters also support the setFilterDefaultValue() method, which must be a valid date in the "Y-m-d" format. This will apply as a default until removed.
3738
```php
3839
public function filters(): array
@@ -47,5 +48,22 @@ public function filters(): array
4748
];
4849
}
4950
```
50-
51+
52+
## setPillsLocale
53+
Date Filters also support the setPillsLocale method, which allows you to set a locale for use in generating the Filter Pills values
54+
```php
55+
public function filters(): array
56+
{
57+
return [
58+
DateFilter::make('Verified From')
59+
->setPillsLocale('fr ') // Use French localisation for the Filter Pills values
60+
->config([
61+
'min' => '2020-01-01', // Earliest Acceptable Date
62+
'max' => '2021-12-31', // Latest Acceptable Date
63+
'pillFormat' => 'd M Y', // Format for use in Filter Pills
64+
'placeholder' => 'Enter Date', // A placeholder value
65+
])
66+
];
67+
}
68+
```
5169

docs/filter-types/filters-daterange.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,52 @@ A full list of options is below, please see the Flatpickr documentation for refe
6666
| time_24hr | Boolean | false | Displays time picker in 24 hour mode without AM/PM selection when enabled. |
6767
| weekNumbers | Boolean | false | Enables display of week numbers in calendar. |
6868

69+
## setFilterDefaultValue
70+
71+
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:
72+
73+
```
74+
DateRangeFilter::make('EMail Verified Range')
75+
->setFilterDefaultValue(['minDate' => '2024-05-05', 'maxDate' => '2024-06-06'])
76+
```
77+
or
78+
```
79+
DateRangeFilter::make('EMail Verified Range')
80+
->setFilterDefaultValue(['min' => '2024-05-05', 'max' => '2024-06-06'])
81+
```
82+
or
83+
```
84+
DateRangeFilter::make('EMail Verified Range')
85+
->setFilterDefaultValue(['2024-05-05', '2024-06-06'])
86+
```
87+
88+
## setPillsLocale
89+
DateRange Filters also support the setPillsLocale method, which allows you to set a locale for use in generating the Filter Pills values
90+
```php
91+
public function filters(): array
92+
{
93+
return [
94+
DateRangeFilter::make('EMail Verified Range')
95+
->setPillsLocale('fr ') // Use French localisation for the Filter Pills values
96+
->config([
97+
'allowInput' => true, // Allow manual input of dates
98+
'altFormat' => 'F j, Y', // Date format that will be displayed once selected
99+
'ariaDateFormat' => 'F j, Y', // An aria-friendly date format
100+
'dateFormat' => 'Y-m-d', // Date format that will be received by the filter
101+
'earliestDate' => '2020-01-01', // The earliest acceptable date
102+
'latestDate' => '2023-08-01', // The latest acceptable date
103+
'placeholder' => 'Enter Date Range', // A placeholder value
104+
'locale' => 'en',
105+
])
106+
->setFilterPillValues([0 => 'minDate', 1 => 'maxDate']) // The values that will be displayed for the Min/Max Date Values
107+
->filter(function (Builder $builder, array $dateRange) { // Expects an array.
108+
$builder
109+
->whereDate('users.email_verified_at', '>=', $dateRange['minDate']) // minDate is the start date selected
110+
->whereDate('users.email_verified_at', '<=', $dateRange['maxDate']); // maxDate is the end date selected
111+
}),
112+
];
113+
}
114+
```
69115

70116
## Configuration
71117
By default, this filter will inject the Flatpickr JS Library and CSS. However, you can customise this behaviour using the configuration file.

docs/filter-types/filters-datetime.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function filters(): array
3333
}
3434
```
3535

36+
## setFilterDefaultValue
3637
DateTime filters also support the setFilterDefaultValue() method, which must be a valid datetime in the "Y-m-dTH:i" format. This will apply as a default until removed.
3738
```php
3839
public function filters(): array
@@ -47,6 +48,23 @@ public function filters(): array
4748
->setFilterDefaultValue('2023-07-07T06:27')
4849
];
4950
}
51+
```
52+
53+
## setPillsLocale
54+
DateTime Filters also support the setPillsLocale method, which allows you to set a locale for use in generating the Filter Pills values
55+
```php
56+
public function filters(): array
57+
{
58+
return [
59+
DateTimeFilter::make('Verified From')
60+
->setPillsLocale('fr ') // Use French localisation for the Filter Pills values
61+
->config([
62+
'min' => '2020-01-01', // Earliest Acceptable Date
63+
'max' => '2021-12-31', // Latest Acceptable Date
64+
'pillFormat' => 'd M Y - H:i', // Format for use in Filter Pills
65+
'placeholder' => 'Enter Date', // A placeholder value
66+
])
67+
];
68+
}
5069
```
51-
5270

docs/misc/lifecycle-hooks.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ This is called immediately after the Columns are set up
2222
## rowsRetrieved
2323
This is called immediately after the query is executed, and is passed the result from the executed query.
2424

25+
## searchUpdated
26+
This is called whenever the search is updated, and is passed the value that has been searched for
27+
28+
## filterApplying
29+
This is called whenever a Filter is applying
30+
31+
## filterReset
32+
This is called whenever a Filter is reset
33+
34+
## filterSet
35+
This is called whenever a Filter is set
36+
37+
## filterUpdated
38+
This is called whenever a Filter is updated/used
39+
40+
## filterRemoved
41+
This is called whenever a Filter is removed from the table
42+
2543
## Use in Traits
2644
To use these in a trait, allowing you to easily set defaults across multiple tables, you should ensure that you append the Lifecycle Hook with your trait name, e.g.
2745

src/Commands/MakeCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function handle(): void
6363
$this->argument('name')
6464
);
6565

66-
$livewireMakeCommand = new LivewireMakeCommand();
66+
$livewireMakeCommand = new LivewireMakeCommand;
6767

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

189189
if ($model instanceof Model === false) {
190190
throw new \Exception('Invalid model given.');

src/Traits/Helpers/FilterHelpers.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ public function getFilterByKey(string $key)
135135
public function setFilter(string $filterKey, mixed $value): void
136136
{
137137
$this->appliedFilters[$filterKey] = $this->filterComponents[$filterKey] = $value;
138+
139+
$this->callHook('filterSet', ['filter' => $filterKey, 'value' => $value]);
140+
$this->callTraitHook('filterSet', ['filter' => $filterKey, 'value' => $value]);
141+
138142
}
139143

140144
public function selectAllFilterOptions(string $filterKey): void
@@ -238,6 +242,8 @@ public function resetFilter($filter): void
238242
if (! $filter instanceof Filter) {
239243
$filter = $this->getFilterByKey($filter);
240244
}
245+
$this->callHook('filterReset', ['filter' => $filter->getKey()]);
246+
$this->callTraitHook('filterReset', ['filter' => $filter->getKey()]);
241247

242248
$this->setFilter($filter->getKey(), $filter->getDefaultValue());
243249
}

src/Traits/WithFilters.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function applyFilters(): Builder
6161
continue;
6262
}
6363

64+
$this->callHook('filterApplying', ['filter' => $filter->getKey(), 'value' => $value]);
65+
$this->callTraitHook('filterApplying', ['filter' => $filter->getKey(), 'value' => $value]);
66+
6467
($filter->getFilterCallback())($this->getBuilder(), $value);
6568
}
6669
}
@@ -84,7 +87,14 @@ public function updatedFilterComponents(string|array|null $value, string $filter
8487
$filter = $this->getFilterByKey($filterName);
8588

8689
if ($filter && $filter->isEmpty($value)) {
90+
$this->callHook('filterRemoved', ['filter' => $filter->getKey()]);
91+
$this->callTraitHook('filterRemoved', ['filter' => $filter->getKey()]);
92+
8793
$this->resetFilter($filterName);
94+
} elseif ($filter) {
95+
$this->callHook('filterUpdated', ['filter' => $filter->getKey(), 'value' => $value]);
96+
$this->callTraitHook('filterUpdated', ['filter' => $filter->getKey(), 'value' => $value]);
97+
8898
}
8999
}
90100
}

src/Traits/WithSearch.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public function applySearch(): Builder
5050
if ($this->searchIsEnabled() && $this->hasSearch()) {
5151
$searchableColumns = $this->getSearchableColumns();
5252

53+
$this->callHook('searchUpdated', ['value' => $this->getSearch()]);
54+
$this->callTraitHook('searchUpdated', ['value' => $this->getSearch()]);
55+
5356
if ($searchableColumns->count()) {
5457
$this->setBuilder($this->getBuilder()->where(function ($query) use ($searchableColumns) {
5558
foreach ($searchableColumns as $index => $column) {

src/Views/Filters/DateFilter.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Views\Filters;
44

5-
use DateTime;
65
use Rappasoft\LaravelLivewireTables\Views\Filter;
76
use Rappasoft\LaravelLivewireTables\Views\Traits\Core\HasWireables;
8-
use Rappasoft\LaravelLivewireTables\Views\Traits\Filters\{HasConfig, IsStringFilter};
7+
use Rappasoft\LaravelLivewireTables\Views\Traits\Filters\{HandlesDates, HasConfig, IsStringFilter};
98

109
class DateFilter extends Filter
1110
{
12-
use HasConfig,
11+
use HandlesDates,
12+
HasConfig,
1313
IsStringFilter;
1414
use HasWireables;
1515

@@ -21,17 +21,16 @@ class DateFilter extends Filter
2121

2222
public function validate(string $value): string|bool
2323
{
24-
if (DateTime::createFromFormat('Y-m-d', $value) === false) {
25-
return false;
26-
}
24+
$this->setInputDateFormat('Y-m-d')->setOutputDateFormat($this->getConfig('pillFormat') ?? 'Y-m-d');
25+
$carbonDate = $this->createCarbonDate($value);
2726

28-
return $value;
27+
return ($carbonDate === false) ? false : $carbonDate->format('Y-m-d');
2928
}
3029

3130
public function getFilterPillValue($value): string|array|null
3231
{
3332
if ($this->validate($value)) {
34-
return DateTime::createFromFormat('Y-m-d', $value)->format($this->getConfig('pillFormat'));
33+
return $this->outputTranslatedDate($this->createCarbonDate($value));
3534
}
3635

3736
return null;

0 commit comments

Comments
 (0)