|
1 | 1 | <?php |
2 | 2 | namespace CustomD\LaravelHelpers\Filament\Plugins; |
3 | 3 |
|
| 4 | +use Closure; |
4 | 5 | use Filament\Panel; |
5 | | -use Livewire\Livewire; |
6 | 6 | use Filament\Contracts\Plugin; |
| 7 | +use Illuminate\Support\HtmlString; |
7 | 8 | use Filament\Tables\Columns\TextColumn; |
| 9 | +use Illuminate\Contracts\Support\Htmlable; |
8 | 10 | use Filament\Infolists\Components\TextEntry; |
9 | 11 | use Filament\Forms\Components\DateTimePicker; |
10 | 12 | use CustomD\LaravelHelpers\Http\Middleware\UserTimeZone; |
11 | | -use Illuminate\Contracts\Support\Htmlable; |
12 | | -use Illuminate\Support\HtmlString; |
13 | 13 |
|
14 | 14 | class UserTimeZonePlugin implements Plugin |
15 | 15 | { |
| 16 | + |
| 17 | + protected bool $withDateHelperText = true; |
| 18 | + |
| 19 | + protected ?Closure $additionalDateConfigs = null; |
| 20 | + |
| 21 | + |
16 | 22 | public static function make(): self |
17 | 23 | { |
18 | 24 | return new self(); |
19 | 25 | } |
20 | 26 |
|
| 27 | + public function hideDateHelperText(bool $disabled = true): self |
| 28 | + { |
| 29 | + $this->withDateHelperText = !$disabled; |
| 30 | + return $this; |
| 31 | + } |
| 32 | + |
| 33 | + public function setAdditionalDateConfigs(Closure $callback): self |
| 34 | + { |
| 35 | + $this->additionalDateConfigs = $callback; |
| 36 | + return $this; |
| 37 | + } |
| 38 | + |
21 | 39 | public function getId(): string |
22 | 40 | { |
23 | 41 | return 'custom-date-time-input-timezone'; |
24 | 42 | } |
25 | 43 |
|
26 | 44 | public function register(Panel $panel): void |
27 | 45 | { |
28 | | - Livewire::addPersistentMiddleware([ |
29 | | - UserTimeZone::class, |
30 | | - ]); |
31 | 46 |
|
32 | 47 | $panel->middleware([ |
33 | 48 | UserTimeZone::class, |
34 | | - ]); |
| 49 | + ], true); |
35 | 50 | } |
36 | 51 |
|
37 | 52 | public function boot(Panel $panel): void |
38 | 53 | { |
39 | 54 |
|
40 | | - $timezone = config('request.user.timezone') ?? config('app.timezone') ?? 'UTC'; |
41 | | - if (is_string($timezone) === false || blank($timezone)) { |
42 | | - $timezone = 'UTC'; |
43 | | - } |
| 55 | + |
| 56 | + $timezone = function() { |
| 57 | + $tz = config('request.user.timezone') ?? config('app.timezone') ?? 'UTC'; |
| 58 | + if (is_string($tz) === false || blank($tz)) { |
| 59 | + $tz = 'UTC'; |
| 60 | + } |
| 61 | + return $tz; |
| 62 | + }; |
| 63 | + |
44 | 64 |
|
45 | 65 | DateTimePicker::configureUsing( |
46 | | - fn (DateTimePicker $dateTimePicker): DateTimePicker => $dateTimePicker->timezone($timezone) |
47 | | - ->helperText( |
48 | | - fn(DateTimePicker $component): Htmlable => new HtmlString("Using the <b><i>{$component->getTimezone()}</i></b> timezone") |
49 | | - ) |
| 66 | + function (DateTimePicker $dateTimePicker) use ($timezone): DateTimePicker { |
| 67 | + $dateTimePicker->timezone($timezone) |
| 68 | + |
| 69 | + ->helperText( |
| 70 | + fn(DateTimePicker $component): ?Htmlable => $this->withDateHelperText ? new HtmlString("Using the <b><i>{$component->getTimezone()}</i></b> timezone") : null |
| 71 | + ); |
| 72 | + |
| 73 | + if($this->additionalDateConfigs){ |
| 74 | + $call = $this->additionalDateConfigs; |
| 75 | + $call($dateTimePicker); |
| 76 | + } |
| 77 | + |
| 78 | + return $dateTimePicker; |
| 79 | + |
| 80 | + } |
50 | 81 | ); |
51 | 82 |
|
52 | 83 | TextColumn::configureUsing( |
|
0 commit comments