Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CountrySelect.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Filament\Forms\Components;
  3. use App\Models\Locale\Country;
  4. use Filament\Forms\Components\Select;
  5. use Filament\Forms\Set;
  6. class CountrySelect extends Select
  7. {
  8. protected ?string $stateFieldName = null;
  9. protected function setUp(): void
  10. {
  11. parent::setUp();
  12. $this
  13. ->localizeLabel('Country')
  14. ->searchable()
  15. ->options($options = Country::getAvailableCountryOptions())
  16. ->getSearchResultsUsing(static fn (string $search): array => Country::getSearchResultsUsing($search))
  17. ->getOptionLabelUsing(static fn (string $value): ?string => $options[$value] ?? $value);
  18. $this->afterStateUpdated(function (self $component, Set $set) {
  19. if ($component->shouldClearStateField()) {
  20. $set($component->getStateFieldName(), null);
  21. }
  22. });
  23. }
  24. public function clearStateField(string $fieldName = 'state_id'): static
  25. {
  26. $this->stateFieldName = $fieldName;
  27. $this->live();
  28. return $this;
  29. }
  30. public function getStateFieldName(): ?string
  31. {
  32. return $this->stateFieldName;
  33. }
  34. public function shouldClearStateField(): bool
  35. {
  36. return (bool) $this->stateFieldName;
  37. }
  38. }