diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml index 8fc6538..ca2197d 100644 --- a/.github/workflows/dependabot-auto-merge.yml +++ b/.github/workflows/dependabot-auto-merge.yml @@ -13,7 +13,7 @@ jobs: - name: Dependabot metadata id: metadata - uses: dependabot/fetch-metadata@v1.4.0 + uses: dependabot/fetch-metadata@v1.6.0 with: github-token: "${{ secrets.GITHUB_TOKEN }}" diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml index 097689b..7520a18 100644 --- a/.github/workflows/fix-php-code-style-issues.yml +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -19,7 +19,7 @@ jobs: ref: ${{ github.head_ref }} - name: Fix PHP code style issues - uses: aglipanci/laravel-pint-action@2.2.0 + uses: aglipanci/laravel-pint-action@2.3.0 - name: Commit changes uses: stefanzweifel/git-auto-commit-action@v4 diff --git a/README.md b/README.md index 142b443..5eb1add 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,7 @@ PtbrCep::make('postal_code') ->viaCep( mode: 'suffix', // Determines whether the action should be appended to (suffix) or prepended to (prefix) the cep field, or not included at all (none). errorMessage: 'CEP inválido.', // Error message to display if the CEP is invalid. + eventFocus: 'cep', // Add focus in another element after cep search /** * Other form fields that can be filled by ViaCep. @@ -153,7 +154,10 @@ PtbrCep::make('postal_code') ), TextInput::make('street'), -TextInput::make('number'), +TextInput::make('number') + ->extraAlpineAttributes([ + 'x-on:cep.window' => "\$el.focus()", // listen to the focus event and add to the element + ]),, TextInput::make('complement'), TextInput::make('district'), TextInput::make('city'), diff --git a/composer.json b/composer.json index 107de06..7a07ac7 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": "^8.1", - "filament/filament": "^2.17", + "filament/filament": "^3.0", "illuminate/contracts": "^10.0", "laravellegends/pt-br-validator": "^10.0", "spatie/laravel-package-tools": "^1.14.0" diff --git a/src/FilamentPtbrFormFieldsServiceProvider.php b/src/FilamentPtbrFormFieldsServiceProvider.php index 4112a1f..b22b38b 100644 --- a/src/FilamentPtbrFormFieldsServiceProvider.php +++ b/src/FilamentPtbrFormFieldsServiceProvider.php @@ -7,10 +7,6 @@ class FilamentPtbrFormFieldsServiceProvider extends FilamentServiceProvider { - protected array $beforeCoreScripts = [ - 'filament-apex-charts-scripts' => __DIR__.'/../dist/mask.min.js', - ]; - public function configurePackage(Package $package): void { /* diff --git a/src/PtbrCep.php b/src/PtbrCep.php index 08849ff..b5bbcc6 100755 --- a/src/PtbrCep.php +++ b/src/PtbrCep.php @@ -4,37 +4,22 @@ use Filament\Forms\Components\Actions\Action; use Filament\Forms\Components\TextInput; +use Filament\Forms\Components\Component; +use Filament\Forms\Set; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Http; use Illuminate\Validation\ValidationException; +use Livewire\Component as Livewire; class PtbrCep extends TextInput { - /** - * Via CEP integration - * - * @param string $mode - * @param string $errorMessage - * @param array $setFields - */ - public function viaCep($mode = 'suffix', $errorMessage = 'CEP inválido.', $setFields = []): static + public function viaCep(string $mode = 'suffix', string $errorMessage = 'CEP inválido.', string $eventFocus = '', array $setFields = []): static { - /** - * @param string $state - * @param Livewire $livewire - * @param Closure $set - * @param TextInput $component - * @param string $errorMessage - * @param array $setFields - */ - $viaCepRequest = function ($state, $livewire, $set, $component, $errorMessage, $setFields) { - $name = $livewire->form->getStatePath() ? $livewire->form->getStatePath().'.'.$component->getName() : $component->getName(); - $livewire->validateOnly($name); + $viaCepRequest = function ($state, $livewire, $set, $component, $errorMessage, $eventFocus, array $setFields) { - /** - * viacep api - */ - $request = Http::get("viacep.com.br/ws/{$state}/json/")->json(); + $livewire->validateOnly($component->getKey()); + + $request = Http::get("viacep.com.br/ws/$state/json/")->json(); foreach ($setFields as $key => $value) { $set($key, $request[$value] ?? null); @@ -42,35 +27,42 @@ public function viaCep($mode = 'suffix', $errorMessage = 'CEP inválido.', $setF if (Arr::has($request, 'erro')) { throw ValidationException::withMessages([ - $name => $errorMessage, + $component->getKey() => $errorMessage, ]); } + + if ($eventFocus) { + $livewire->dispatch($eventFocus); + } + }; $this ->minLength(9) - ->extraAlpineAttributes(['x-mask' => '99999-999']) - ->afterStateUpdated(function ($state, $livewire, $set, $component) use ($errorMessage, $setFields, $viaCepRequest) { - $viaCepRequest($state, $livewire, $set, $component, $errorMessage, $setFields); + ->mask('99999-999') + ->afterStateUpdated(function ($state, Livewire $livewire, Set $set, Component $component) use ($errorMessage, $setFields, $eventFocus, $viaCepRequest) { + $viaCepRequest($state, $livewire, $set, $component, $errorMessage, $eventFocus, $setFields); }) - ->suffixAction(function ($state, $livewire, $set, $component) use ($mode, $errorMessage, $setFields, $viaCepRequest) { + ->suffixAction(function () use ($mode, $errorMessage, $eventFocus, $setFields, $viaCepRequest) { if ($mode === 'suffix') { return Action::make('search-action') ->label('Buscar CEP') - ->icon('heroicon-o-search') - ->action(function () use ($state, $livewire, $set, $component, $errorMessage, $setFields, $viaCepRequest) { - $viaCepRequest($state, $livewire, $set, $component, $errorMessage, $setFields); - }); + ->icon('heroicon-o-magnifying-glass') + ->action(function ($state, Livewire $livewire, Set $set, Component $component) use ($errorMessage, $eventFocus, $setFields, $viaCepRequest) { + $viaCepRequest($state, $livewire, $set, $component, $errorMessage, $eventFocus, $setFields); + }) + ->cancelParentActions(); } }) - ->prefixAction(function ($state, $livewire, $set, $component) use ($mode, $errorMessage, $setFields, $viaCepRequest) { + ->prefixAction(function () use ($mode, $errorMessage, $eventFocus, $setFields, $viaCepRequest) { if ($mode === 'prefix') { return Action::make('search-action') ->label('Buscar CEP') - ->icon('heroicon-o-search') - ->action(function () use ($state, $livewire, $set, $component, $errorMessage, $setFields, $viaCepRequest) { - $viaCepRequest($state, $livewire, $set, $component, $errorMessage, $setFields); - }); + ->icon('heroicon-o-magnifying-glass') + ->action(function ($state, Livewire $livewire, Set $set, Component $component) use ($errorMessage, $eventFocus, $setFields, $viaCepRequest) { + $viaCepRequest($state, $livewire, $set, $component, $errorMessage, $eventFocus, $setFields); + }) + ->cancelParentActions(); } }); diff --git a/src/PtbrCpfCnpj.php b/src/PtbrCpfCnpj.php index 36bd971..f644cd1 100644 --- a/src/PtbrCpfCnpj.php +++ b/src/PtbrCpfCnpj.php @@ -4,6 +4,7 @@ use Closure; use Filament\Forms\Components\TextInput; +use Filament\Support\RawJs; class PtbrCpfCnpj extends TextInput { @@ -15,9 +16,9 @@ protected function setUp(): void public function dynamic(bool $condition = true): static { if ($condition) { - $this->extraAlpineAttributes([ - 'x-mask:dynamic' => '$input.length >14 ? \'99.999.999/9999-99\' : \'999.999.999-99\'', - ])->minLength(14); + $this->mask(RawJs::make(<<<'JS' + $input.length > 14 ? '99.999.999/9999-99' : '999.999.999-99' + JS))->minLength(14); } return $this; @@ -26,9 +27,7 @@ public function dynamic(bool $condition = true): static public function cpf(string|Closure $format = '999.999.999-99'): static { $this->dynamic(false) - ->extraAlpineAttributes([ - 'x-mask' => $format, - ]); + ->mask($format); return $this; } @@ -36,9 +35,7 @@ public function cpf(string|Closure $format = '999.999.999-99'): static public function cnpj(string|Closure $format = '99.999.999/9999-99'): static { $this->dynamic(false) - ->extraAlpineAttributes([ - 'x-mask' => $format, - ]); + ->mask($format); return $this; } diff --git a/src/PtbrPhone.php b/src/PtbrPhone.php index 4d834f2..978e484 100644 --- a/src/PtbrPhone.php +++ b/src/PtbrPhone.php @@ -4,6 +4,7 @@ use Closure; use Filament\Forms\Components\TextInput; +use Filament\Support\RawJs; class PtbrPhone extends TextInput { @@ -15,9 +16,9 @@ protected function setUp(): void public function dynamic(bool $condition = true): static { if ($condition) { - $this->extraAlpineAttributes([ - 'x-mask:dynamic' => '$input.length >=14 ? \'(99)99999-9999\' : \'(99)9999-9999\'', - ])->minLength(13); + $this->mask(RawJs::make(<<<'JS' + $input.length > 14 ? '(99)99999-9999' : '(99)9999-9999' + JS)); } return $this; @@ -27,9 +28,7 @@ public function format(string|Closure $format = '(99)99999-9999'): static { $this->dynamic(false) ->minLength(0) - ->extraAlpineAttributes([ - 'x-mask' => $format, - ]); + ->mask($format); return $this; }