Skip to content

Commit de538da

Browse files
Add custom filter attributes (#1890)
* Add custom filter attributes for input text * wip
1 parent 4e6d814 commit de538da

File tree

4 files changed

+59
-32
lines changed

4 files changed

+59
-32
lines changed

resources/config/livewire-powergrid.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
use Illuminate\Support\Js;
4+
use Illuminate\View\ComponentAttributeBag;
5+
36
return [
47

58
/*
@@ -95,6 +98,53 @@
9598

9699
'filter' => 'inline',
97100

101+
/*
102+
|--------------------------------------------------------------------------
103+
| Filters Attributes
104+
|--------------------------------------------------------------------------
105+
106+
| You can add custom attributes to the filters.
107+
| The key is the filter type and the value is a callback function.
108+
| like: input_text, select, datetime, etc.
109+
| The callback function receives the field and title as parameters.
110+
| The callback function must return an array with the attributes.
111+
*/
112+
113+
'filter_attributes' => [
114+
'input_text' => function (string $field, string $title): array {
115+
return [
116+
'inputAttributes' => new ComponentAttributeBag([
117+
'wire:model' => 'filters.input_text.' . $field,
118+
'wire:input.live.debounce.600ms' => 'filterInputText(\'' . $field . '\', $event.target.value, \'' . $title . '\')',
119+
]),
120+
'selectAttributes' => new ComponentAttributeBag([
121+
'wire:model' => 'filters.input_text_options.' . $field,
122+
'wire:input.live.debounce.600ms' => 'filterInputTextOptions(\'' . $field . '\', $event.target.value, \'' . $title . '\')',
123+
]),
124+
];
125+
},
126+
'boolean' => function (string $field, string $title): array {
127+
return [
128+
'selectAttributes' => new ComponentAttributeBag([
129+
'wire:input.live.debounce.600ms' => 'filterBoolean(\'' . $field . '\', $event.target.value, \'' . $title . '\')',
130+
'wire:model' => 'filters.boolean.' . $field,
131+
]),
132+
];
133+
},
134+
'number' => function (string $field, array $filter): array {
135+
return [
136+
'inputStartAttributes' => new ComponentAttributeBag([
137+
'wire:model' => 'filters.number.' . $field . '.start',
138+
'wire:input.live.debounce.600ms' => 'filterNumberStart(\'' . $field . '\', ' . Js::from($filter) . ', $event.target.value)',
139+
]),
140+
'inputEndAttributes' => new ComponentAttributeBag([
141+
'wire:model' => 'filters.number.' . $field . '.end',
142+
'wire:input.live.debounce.600ms' => 'filterNumberEnd(\'' . $field . '\', ' . Js::from($filter) . ', $event.target.value)',
143+
]),
144+
];
145+
},
146+
],
147+
98148
/*
99149
|--------------------------------------------------------------------------
100150
| Persisting

src/Components/Filters/FilterBoolean.php

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

33
namespace PowerComponents\LivewirePowerGrid\Components\Filters;
44

5-
use Illuminate\View\ComponentAttributeBag;
6-
75
class FilterBoolean extends FilterBase
86
{
97
public string $key = 'boolean';
@@ -22,10 +20,8 @@ public function label(string $trueLabel, string $falseLabel): FilterBoolean
2220

2321
public static function getWireAttributes(string $field, string $title): array
2422
{
25-
return collect()
26-
->put('selectAttributes', new ComponentAttributeBag([
27-
'wire:input.live.debounce.600ms' => 'filterBoolean(\'' . $field . '\', $event.target.value, \'' . $title . '\')',
28-
'wire:model' => 'filters.boolean.' . $field,
29-
]))->toArray();
23+
$configAttributes = config('livewire-powergrid.filter_attributes.boolean');
24+
25+
return is_callable($configAttributes) ? $configAttributes($field, $title) : [];
3026
}
3127
}

src/Components/Filters/FilterInputText.php

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

33
namespace PowerComponents\LivewirePowerGrid\Components\Filters;
44

5-
use Illuminate\View\ComponentAttributeBag;
6-
75
class FilterInputText extends FilterBase
86
{
97
public string $key = 'input_text';
@@ -40,16 +38,9 @@ public function operators(array $value = []): FilterInputText
4038

4139
public static function getWireAttributes(string $field, string $title): array
4240
{
43-
return collect()
44-
->put('selectAttributes', new ComponentAttributeBag([
45-
'wire:model' => 'filters.input_text_options.' . $field,
46-
'wire:input.live.debounce.600ms' => 'filterInputTextOptions(\'' . $field . '\', $event.target.value, \'' . $title . '\')',
47-
]))
48-
->put('inputAttributes', new ComponentAttributeBag([
49-
'wire:model' => 'filters.input_text.' . $field,
50-
'wire:input.live.debounce.600ms' => 'filterInputText(\'' . $field . '\', $event.target.value, \'' . $title . '\')',
51-
]))
52-
->toArray();
41+
$configAttributes = config('livewire-powergrid.filter_attributes.input_text');
42+
43+
return is_callable($configAttributes) ? $configAttributes($field, $title) : [];
5344
}
5445

5546
public function placeholder(string $placeholder): FilterInputText

src/Components/Filters/FilterNumber.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace PowerComponents\LivewirePowerGrid\Components\Filters;
44

5-
use Illuminate\Support\Js;
6-
use Illuminate\View\ComponentAttributeBag;
7-
85
class FilterNumber extends FilterBase
96
{
107
public string $key = 'number';
@@ -41,15 +38,8 @@ public function placeholder(string $min, string $max): FilterNumber
4138

4239
public static function getWireAttributes(string $field, array $filter): array
4340
{
44-
return collect()
45-
->put('inputStartAttributes', new ComponentAttributeBag([
46-
'wire:model' => 'filters.number.' . $field . '.start',
47-
'wire:input.live.debounce.600ms' => 'filterNumberStart(\'' . $field . '\', ' . Js::from($filter) . ', $event.target.value)',
48-
]))
49-
->put('inputEndAttributes', new ComponentAttributeBag([
50-
'wire:model' => 'filters.number.' . $field . '.end',
51-
'wire:input.live.debounce.600ms' => 'filterNumberEnd(\'' . $field . '\', ' . Js::from($filter) . ', $event.target.value)',
52-
]))
53-
->toArray();
41+
$configAttributes = config('livewire-powergrid.filter_attributes.number');
42+
43+
return is_callable($configAttributes) ? $configAttributes($field, $filter) : [];
5444
}
5545
}

0 commit comments

Comments
 (0)