Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AddressFields.php 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 bool | Closure $isRequired = false;
  12. protected function setUp(): void
  13. {
  14. parent::setUp();
  15. $this->schema([
  16. TextInput::make('address_line_1')
  17. ->label('Address line 1')
  18. ->required(fn () => $this->isRequired())
  19. ->maxLength(255),
  20. TextInput::make('address_line_2')
  21. ->label('Address line 2')
  22. ->maxLength(255),
  23. CountrySelect::make('country_code')
  24. ->disabled(fn () => $this->isCountryDisabled())
  25. ->clearStateField()
  26. ->required(fn () => $this->isRequired()),
  27. StateSelect::make('state_id'),
  28. TextInput::make('city')
  29. ->label('City')
  30. ->required(fn () => $this->isRequired())
  31. ->maxLength(255),
  32. TextInput::make('postal_code')
  33. ->label('Postal code')
  34. ->maxLength(255),
  35. ]);
  36. }
  37. public function softRequired(bool $condition = true): static
  38. {
  39. $this->setSoftRequired($condition);
  40. return $this;
  41. }
  42. protected function setSoftRequired(bool $condition): void
  43. {
  44. $this->isSoftRequired = $condition;
  45. $childComponents = $this->getChildComponents();
  46. foreach ($childComponents as $component) {
  47. if ($component instanceof Field && $component->isRequired()) {
  48. $component->markAsRequired(! $condition);
  49. }
  50. }
  51. }
  52. public function required(bool | Closure $condition = true): static
  53. {
  54. $this->isRequired = $condition;
  55. return $this;
  56. }
  57. public function isRequired(): bool
  58. {
  59. return (bool) $this->evaluate($this->isRequired);
  60. }
  61. public function disabledCountry(bool | Closure $condition = true): static
  62. {
  63. $this->isCountryDisabled = $condition;
  64. return $this;
  65. }
  66. public function isCountryDisabled(): bool
  67. {
  68. return $this->evaluate($this->isCountryDisabled);
  69. }
  70. }