File tree Expand file tree Collapse file tree 3 files changed +38
-9
lines changed Expand file tree Collapse file tree 3 files changed +38
-9
lines changed Original file line number Diff line number Diff line change 44
55use Rappasoft \LaravelLivewireTables \Views \Filter ;
66use Rappasoft \LaravelLivewireTables \Views \Traits \Core \HasWireables ;
7- use Rappasoft \LaravelLivewireTables \Views \Traits \Filters \{IsStringFilter };
7+ use Rappasoft \LaravelLivewireTables \Views \Traits \Filters \{IsNumericFilter };
88
99class NumberFilter extends Filter
1010{
11- use IsStringFilter ;
11+ use IsNumericFilter ;
1212 use HasWireables;
1313
1414 public string $ wireMethod = 'blur ' ;
@@ -17,17 +17,20 @@ class NumberFilter extends Filter
1717
1818 public function validate (float |int |string |array $ value ): float |int |string |false
1919 {
20+ $ floatValue = (float ) $ value ;
21+ $ intValue = (int ) $ value ;
22+
2023 if (is_array ($ value )) {
2124 return false ;
2225 } elseif (is_float ($ value )) {
23- return ( float ) $ value ;
26+ return $ floatValue ;
2427 } elseif (is_int ($ value )) {
25- return (int ) $ value ;
26- } elseif (ctype_digit ($ value )) {
27- return (float ) $ value ;
28+ return $ intValue ;
29+ } elseif (is_numeric ($ value )) {
30+ return (($ floatValue - $ intValue ) == 0 ) ? $ intValue : $ floatValue ;
31+ } else if (ctype_digit ($ value )) {
32+ return $ intValue ;
2833 }
29-
3034 return false ;
31-
3235 }
3336}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rappasoft \LaravelLivewireTables \Views \Traits \Filters ;
4+
5+ use Closure ;
6+ use Illuminate \View \ComponentAttributeBag ;
7+ use Rappasoft \LaravelLivewireTables \Views \{Column ,Filter };
8+
9+ trait IsNumericFilter
10+ {
11+ public function isEmpty (float |int |string |array |null $ value ): bool
12+ {
13+ return (!is_null ($ value ) ? ($ this ->validate ($ value ) == false ) : true );
14+ }
15+
16+ /**
17+ * Gets the Default Value for this Filter via the Component
18+ */
19+ public function getFilterDefaultValue (): ?string
20+ {
21+ return $ this ->filterDefaultValue ?? null ;
22+ }
23+ }
Original file line number Diff line number Diff line change @@ -158,13 +158,16 @@ public function test_can_set_number_filter_to_number(): void
158158 $ filter = NumberFilter::make ('BreedID ' );
159159 $ this ->assertSame (123 , $ filter ->validate (123 ));
160160 $ this ->assertSame (123.51 , $ filter ->validate (123.51 ));
161- $ this ->assertFalse ($ filter ->validate ('123 ' ));
161+ $ this ->assertSame (123 , $ filter ->validate ('123 ' ));
162+ $ this ->assertSame (123.51 , $ filter ->validate ('123.51 ' ));
163+
162164 }
163165
164166 public function test_can_get_if_number_filter_empty (): void
165167 {
166168 $ filter = NumberFilter::make ('Active ' );
167169 $ this ->assertTrue ($ filter ->isEmpty ('' ));
170+ $ this ->assertTrue ($ filter ->isEmpty ('q ' ));
168171 $ this ->assertFalse ($ filter ->isEmpty ('123 ' ));
169172 }
170173
You can’t perform that action at this time.
0 commit comments