Skip to content

Commit

Permalink
Add Localised Pills (#1797)
Browse files Browse the repository at this point in the history
* Add Localised Pills for DateRangeFilter

---------

Co-authored-by: lrljoe <[email protected]>
  • Loading branch information
lrljoe and lrljoe authored Jul 27, 2024
1 parent b9b5d0e commit 8bc636a
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/Views/Filters/DateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rappasoft\LaravelLivewireTables\Views\Filters;

use Carbon\Carbon;
use Illuminate\Support\Facades\Validator;
use Rappasoft\LaravelLivewireTables\Views\Filter;
use Rappasoft\LaravelLivewireTables\Views\Traits\Core\HasWireables;
use Rappasoft\LaravelLivewireTables\Views\Traits\Filters\{HasConfig,HasOptions};
Expand Down Expand Up @@ -57,17 +59,17 @@ public function validate(array|string $values): array|bool

$dateFormat = $this->getConfigs()['dateFormat'];

$validator = \Illuminate\Support\Facades\Validator::make($returnedValues, [
$validator = Validator::make($returnedValues, [
'minDate' => 'required|date_format:'.$dateFormat,
'maxDate' => 'required|date_format:'.$dateFormat,
]);
if ($validator->fails()) {
return false;
}
$startDate = \Carbon\Carbon::createFromFormat($dateFormat, $returnedValues['minDate']);
$endDate = \Carbon\Carbon::createFromFormat($dateFormat, $returnedValues['maxDate']);
$startDate = Carbon::createFromFormat($dateFormat, $returnedValues['minDate']);
$endDate = Carbon::createFromFormat($dateFormat, $returnedValues['maxDate']);

if (! ($startDate instanceof \Carbon\Carbon) || ! ($endDate instanceof \Carbon\Carbon)) {
if (! ($startDate instanceof Carbon) || ! ($endDate instanceof Carbon)) {
return false;
}
if ($startDate->gt($endDate)) {
Expand All @@ -79,20 +81,20 @@ public function validate(array|string $values): array|bool

if ($earliestDateString != '' && ! is_null($earliestDateString) && $latestDateString != '' && ! is_null($latestDateString)) {
$dateLimits = ['earliest' => $earliestDateString, 'latest' => $latestDateString];
$earlyLateValidator = \Illuminate\Support\Facades\Validator::make($dateLimits, [
$earlyLateValidator = Validator::make($dateLimits, [
'earliest' => 'date_format:'.$dateFormat,
'latest' => 'date_format:'.$dateFormat,
]);
if (! $earlyLateValidator->fails()) {
$earliestDate = \Carbon\Carbon::createFromFormat($dateFormat, $earliestDateString);
$latestDate = \Carbon\Carbon::createFromFormat($dateFormat, $latestDateString);
$earliestDate = Carbon::createFromFormat($dateFormat, $earliestDateString);
$latestDate = Carbon::createFromFormat($dateFormat, $latestDateString);

if ($earliestDate instanceof \Carbon\Carbon) {
if ($earliestDate instanceof Carbon) {
if ($startDate->lt($earliestDate)) {
return false;
}
}
if ($latestDate instanceof \Carbon\Carbon) {
if ($latestDate instanceof Carbon) {
if ($endDate->gt($latestDate)) {
return false;
}
Expand Down Expand Up @@ -154,15 +156,18 @@ public function getFilterPillValue($value): string|array|null
if (is_array($validatedValue)) {
$dateFormat = $this->getConfig('dateFormat');
$ariaDateFormat = $this->getConfig('ariaDateFormat');
$locale = $this->getConfig('locale') ?? config('app.locale', 'en');

$minDateCarbon = \Carbon\Carbon::createFromFormat($dateFormat, $validatedValue['minDate']);
$maxDateCarbon = \Carbon\Carbon::createFromFormat($dateFormat, $validatedValue['maxDate']);
$carbon = new Carbon;
$carbon->setLocale($locale);

if (($minDateCarbon instanceof \Carbon\Carbon) && $maxDateCarbon instanceof \Carbon\Carbon) {
$minDate = $minDateCarbon->format($ariaDateFormat);
$maxDate = $maxDateCarbon->format($ariaDateFormat);
$minDate = $carbon->createFromFormat($dateFormat, $validatedValue['minDate']);
$maxDate = $carbon->createFromFormat($dateFormat, $validatedValue['maxDate']);

return $minDate.' '.__('to').' '.$maxDate;
if (($minDate instanceof Carbon) && $maxDate instanceof Carbon) {
return $minDate->translatedFormat($ariaDateFormat)
.' '.__('to').' '.
$maxDate->translatedFormat($ariaDateFormat);
}
}

Expand Down

0 comments on commit 8bc636a

Please sign in to comment.