Skip to content

Commit 241142a

Browse files
committed
Merge branch 'Wit3-feature-datetime-filter' into develop
2 parents d6fd046 + 21edf17 commit 241142a

File tree

9 files changed

+74
-2
lines changed

9 files changed

+74
-2
lines changed

docs/filters/creating-filters.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Creating filters is not required, and the filters box will be hidden if none are
77

88
To create filters, you return an array of `Filter` class objects from the `filters()` method.
99

10-
The current types of filters are: `select`, `multiSelect`, and `date`.
10+
The current types of filters are: `select`, `multiSelect`, `date`, and `datetime` (for supported browsers).
1111

1212
There are two steps to making a filter:
1313

@@ -49,7 +49,7 @@ public function filters(): array
4949
'tag2' => 'Tags 2',
5050
'tag3' => 'Tags 3',
5151
'tag4' => 'Tags 4',
52-
]),
52+
]),
5353
];
5454
}
5555
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div class="mb-3 mb-md-0 input-group">
2+
<input
3+
wire:model="filters.{{ $key }}"
4+
wire:key="filter-{{ $key }}"
5+
id="filter-{{ $key }}"
6+
type="datetime-local"
7+
@if(isset($filter->options['min']) && strlen($filter->options['min'])) min="{{ $filter->options['min'] }}" @endif
8+
@if(isset($filter->options['max']) && strlen($filter->options['max'])) max="{{ $filter->options['max'] }}" @endif
9+
class="form-control"
10+
/>
11+
</div>

resources/views/bootstrap-4/includes/filters.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class="dropdown-menu w-100 mt-md-3"
4242
@include('livewire-tables::bootstrap-4.includes.filter-type-multiselect')
4343
@elseif($filter->isDate())
4444
@include('livewire-tables::bootstrap-4.includes.filter-type-date')
45+
@elseif($filter->isDatetime())
46+
@include('livewire-tables::bootstrap-4.includes.filter-type-datetime')
4547
@endif
4648
</div>
4749
@endforeach
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div class="flex rounded-md shadow-sm mt-1">
2+
<input
3+
wire:model="filters.{{ $key }}"
4+
wire:key="filter-{{ $key }}"
5+
id="filter-{{ $key }}"
6+
type="datetime-local"
7+
@if(isset($filter->options['min']) && strlen($filter->options['min'])) min="{{ $filter->options['min'] }}" @endif
8+
@if(isset($filter->options['max']) && strlen($filter->options['max'])) max="{{ $filter->options['max'] }}" @endif
9+
class="form-control"
10+
/>
11+
</div>

resources/views/bootstrap-5/includes/filters.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class="dropdown-menu w-100"
4242
@include('livewire-tables::bootstrap-5.includes.filter-type-multiselect')
4343
@elseif($filter->isDate())
4444
@include('livewire-tables::bootstrap-5.includes.filter-type-date')
45+
@elseif($filter->isDatetime())
46+
@include('livewire-tables::bootstrap-5.includes.filter-type-datetime')
4547
@endif
4648
</div>
4749
@endforeach
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div class="flex rounded-md shadow-sm mt-1">
2+
<input
3+
wire:model.stop="filters.{{ $key }}"
4+
wire:key="filter-{{ $key }}"
5+
id="filter-{{ $key }}"
6+
type="datetime-local"
7+
@if(isset($filter->options['min']) && strlen($filter->options['min'])) min="{{ $filter->options['min'] }}" @endif
8+
@if(isset($filter->options['max']) && strlen($filter->options['max'])) max="{{ $filter->options['max'] }}" @endif
9+
class="block w-full border-gray-300 rounded-md shadow-sm transition duration-150 ease-in-out focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50 dark:bg-gray-800 dark:text-white dark:border-gray-600"
10+
/>
11+
</div>

resources/views/tailwind/includes/filters.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class="block text-sm font-medium leading-5 text-gray-700 dark:text-white">
6262
@include('livewire-tables::tailwind.includes.filter-type-multiselect')
6363
@elseif($filter->isDate())
6464
@include('livewire-tables::tailwind.includes.filter-type-date')
65+
@elseif($filter->isDatetime())
66+
@include('livewire-tables::tailwind.includes.filter-type-datetime')
6567
@endif
6668
</div>
6769
</div>

src/Traits/WithFilters.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ public function cleanFilters(): void
192192
return $dt !== false && ! array_sum($dt::getLastErrors());
193193
}
194194

195+
if ($filterDefinitions[$filterName]->isDatetime()) {
196+
// array_sum trick is a terse way of ensuring that PHP
197+
// did not do "month shifting"
198+
// (e.g. consider that January 32 is February 1)
199+
$dt = DateTime::createFromFormat("Y-m-d\TH:i", $filterValue);
200+
201+
return $dt !== false && ! array_sum($dt::getLastErrors());
202+
}
203+
195204
return false;
196205
})->toArray();
197206
}

src/Views/Filter.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Filter
99
{
1010
public const TYPE_DATE = 'date';
1111

12+
public const TYPE_DATETIME_LOCAL = 'datetime-local';
13+
1214
public const TYPE_SELECT = 'select';
1315

1416
public const TYPE_MULTISELECT = 'multiselect';
@@ -96,6 +98,20 @@ public function date(array $options = []): Filter
9698
return $this;
9799
}
98100

101+
/**
102+
* @param array $options
103+
*
104+
* @return $this
105+
*/
106+
public function datetime(array $options = []): Filter
107+
{
108+
$this->type = self::TYPE_DATETIME_LOCAL;
109+
110+
$this->options = $options;
111+
112+
return $this;
113+
}
114+
99115
/**
100116
* @return string
101117
*/
@@ -135,4 +151,12 @@ public function isDate(): bool
135151
{
136152
return $this->type === self::TYPE_DATE;
137153
}
154+
155+
/**
156+
* @return bool
157+
*/
158+
public function isDatetime(): bool
159+
{
160+
return $this->type === self::TYPE_DATETIME_LOCAL;
161+
}
138162
}

0 commit comments

Comments
 (0)