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.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. ->disabled(app()->environment('demo'))
  16. ->options($options = Country::getAvailableCountryOptions())
  17. ->getSearchResultsUsing(static fn (string $search): array => Country::getSearchResultsUsing($search))
  18. ->getOptionLabelUsing(static fn (string $value): ?string => $options[$value] ?? $value);
  19. $this->afterStateUpdated(function (self $component, Set $set) {
  20. if ($component->shouldClearStateField()) {
  21. $set($component->getStateFieldName(), null);
  22. }
  23. });
  24. }
  25. public function clearStateField(string $fieldName = 'state_id'): static
  26. {
  27. $this->stateFieldName = $fieldName;
  28. $this->live();
  29. return $this;
  30. }
  31. public function getStateFieldName(): ?string
  32. {
  33. return $this->stateFieldName;
  34. }
  35. public function shouldClearStateField(): bool
  36. {
  37. return (bool) $this->stateFieldName;
  38. }
  39. }