Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #8

Merged
merged 14 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fix-php-code-style-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 0 additions & 4 deletions src/FilamentPtbrFormFieldsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/*
Expand Down
66 changes: 29 additions & 37 deletions src/PtbrCep.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,65 @@

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);
}

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();
}
});

Expand Down
15 changes: 6 additions & 9 deletions src/PtbrCpfCnpj.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Filament\Forms\Components\TextInput;
use Filament\Support\RawJs;

class PtbrCpfCnpj extends TextInput
{
Expand All @@ -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;
Expand All @@ -26,19 +27,15 @@ 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;
}

public function cnpj(string|Closure $format = '99.999.999/9999-99'): static
{
$this->dynamic(false)
->extraAlpineAttributes([
'x-mask' => $format,
]);
->mask($format);

return $this;
}
Expand Down
11 changes: 5 additions & 6 deletions src/PtbrPhone.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Filament\Forms\Components\TextInput;
use Filament\Support\RawJs;

class PtbrPhone extends TextInput
{
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down