Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AppServiceProvider.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Providers;
  3. use App\Enums\PrimaryColor;
  4. use Closure;
  5. use Filament\Forms\Components\{Select, TextInput};
  6. use Filament\Notifications\Livewire\Notifications;
  7. use Filament\Support\Enums\Alignment;
  8. use Filament\Support\RawJs;
  9. use Filament\Tables\Columns\TextColumn;
  10. use Illuminate\Support\{ServiceProvider, Str};
  11. class AppServiceProvider extends ServiceProvider
  12. {
  13. /**
  14. * Register any application services.
  15. */
  16. public function register(): void
  17. {
  18. //
  19. }
  20. /**
  21. * Bootstrap any application services.
  22. */
  23. public function boot(): void
  24. {
  25. Notifications::alignment(Alignment::Center);
  26. TextColumn::macro('currency', function (string | Closure | null $currency = null, ?bool $convert = null): static {
  27. $this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $convert): ?string {
  28. if (blank($state)) {
  29. return null;
  30. }
  31. $currency = $column->evaluate($currency);
  32. $convert = $column->evaluate($convert);
  33. return money($state, $currency, $convert);
  34. });
  35. return $this;
  36. });
  37. TextInput::macro('currency', function (string | Closure | null $currency = null): static {
  38. $this->extraAttributes(['wire:key' => Str::random()])
  39. ->prefix(static function (TextInput $component) use ($currency) {
  40. $currency = $component->evaluate($currency);
  41. return currency($currency)->getPrefix();
  42. })
  43. ->suffix(static function (TextInput $component) use ($currency) {
  44. $currency = $component->evaluate($currency);
  45. return currency($currency)->getSuffix();
  46. })
  47. ->mask(static function (TextInput $component) use ($currency) {
  48. $currency = $component->evaluate($currency);
  49. $decimal_mark = currency($currency)->getDecimalMark();
  50. $thousands_separator = currency($currency)->getThousandsSeparator();
  51. $precision = currency($currency)->getPrecision();
  52. $jsCode = "\$money(\$input, '" . $decimal_mark . "', '" . $thousands_separator . "', " . $precision . ');';
  53. return RawJs::make($jsCode);
  54. });
  55. return $this;
  56. });
  57. Select::macro('color', function (string | Closure | null $color = null): static {
  58. $this->options(
  59. collect(PrimaryColor::caseValues())
  60. ->mapWithKeys(static function ($color) {
  61. return [$color => Str::title($color)];
  62. })
  63. ->toArray()
  64. )
  65. ->extraAttributes(['wire:key' => Str::random()])
  66. ->prefix(static function (Select $component) use ($color) {
  67. $color = $component->evaluate($color);
  68. return '<span class="text-' . $color . '-500">●</span>';
  69. });
  70. return $this;
  71. });
  72. }
  73. }