Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Filament\Forms\Components;
  3. use Closure;
  4. use Filament\Forms\Components\Field;
  5. use Filament\Forms\Components\Grid;
  6. use Filament\Forms\Components\TextInput;
  7. class AddressFields extends Grid
  8. {
  9. protected bool $isSoftRequired = false;
  10. protected bool | Closure $isCountryDisabled = false;
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. $this->schema([
  15. TextInput::make('address_line_1')
  16. ->label('Address line 1')
  17. ->required()
  18. ->maxLength(255),
  19. TextInput::make('address_line_2')
  20. ->label('Address line 2')
  21. ->maxLength(255),
  22. CountrySelect::make('country_code')
  23. ->disabled(fn () => $this->isCountryDisabled())
  24. ->clearStateField()
  25. ->required(),
  26. StateSelect::make('state_id'),
  27. TextInput::make('city')
  28. ->label('City')
  29. ->required()
  30. ->maxLength(255),
  31. TextInput::make('postal_code')
  32. ->label('Postal code')
  33. ->maxLength(255),
  34. ]);
  35. }
  36. public function softRequired(bool $condition = true): static
  37. {
  38. $this->setSoftRequired($condition);
  39. return $this;
  40. }
  41. protected function setSoftRequired(bool $condition): void
  42. {
  43. $this->isSoftRequired = $condition;
  44. $childComponents = $this->getChildComponents();
  45. foreach ($childComponents as $component) {
  46. if ($component instanceof Field && $component->isRequired()) {
  47. $component->markAsRequired(! $condition);
  48. }
  49. }
  50. }
  51. public function disabledCountry(bool | Closure $condition = true): static
  52. {
  53. $this->isCountryDisabled = $condition;
  54. return $this;
  55. }
  56. public function isCountryDisabled(): bool
  57. {
  58. return $this->evaluate($this->isCountryDisabled);
  59. }
  60. }