generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
501160f
commit a769fd9
Showing
10 changed files
with
284 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Leandrocfe\FilamentPtbrFormFields; | ||
|
||
use Filament\Forms\Components\Actions\Action; | ||
use Filament\Forms\Components\Component; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Forms\Set; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Validation\ValidationException; | ||
use Livewire\Component as Livewire; | ||
|
||
class Cep extends TextInput | ||
{ | ||
public function viaCep(string $mode = 'suffix', string $errorMessage = 'CEP inválido.', array $setFields = []): static | ||
{ | ||
$viaCepRequest = function ($state, $livewire, $set, $component, $errorMessage, array $setFields) { | ||
|
||
$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 (blank($request) || Arr::has($request, 'erro')) { | ||
throw ValidationException::withMessages([ | ||
$component->getKey() => $errorMessage, | ||
]); | ||
} | ||
}; | ||
|
||
$this | ||
->minLength(9) | ||
->mask('99999-999') | ||
->afterStateUpdated(function ($state, Livewire $livewire, Set $set, Component $component) use ($errorMessage, $setFields, $viaCepRequest) { | ||
$viaCepRequest($state, $livewire, $set, $component, $errorMessage, $setFields); | ||
}) | ||
->suffixAction(function () use ($mode, $errorMessage, $setFields, $viaCepRequest) { | ||
if ($mode === 'suffix') { | ||
return Action::make('search-action') | ||
->label('Buscar CEP') | ||
->icon('heroicon-o-magnifying-glass') | ||
->action(function ($state, Livewire $livewire, Set $set, Component $component) use ($errorMessage, $setFields, $viaCepRequest) { | ||
$viaCepRequest($state, $livewire, $set, $component, $errorMessage, $setFields); | ||
}) | ||
->cancelParentActions(); | ||
} | ||
}) | ||
->prefixAction(function () use ($mode, $errorMessage, $setFields, $viaCepRequest) { | ||
if ($mode === 'prefix') { | ||
return Action::make('search-action') | ||
->label('Buscar CEP') | ||
->icon('heroicon-o-magnifying-glass') | ||
->action(function ($state, Livewire $livewire, Set $set, Component $component) use ($errorMessage, $setFields, $viaCepRequest) { | ||
$viaCepRequest($state, $livewire, $set, $component, $errorMessage, $setFields); | ||
}) | ||
->cancelParentActions(); | ||
} | ||
}); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Leandrocfe\FilamentPtbrFormFields; | ||
|
||
use Closure; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Support\RawJs; | ||
|
||
class Document extends TextInput | ||
{ | ||
public bool $validation = true; | ||
|
||
public function dynamic(bool $condition = true): static | ||
{ | ||
if (self::getValidation()) { | ||
$this->rule('cpf_ou_cnpj'); | ||
} | ||
|
||
if ($condition) { | ||
$this->mask(RawJs::make(<<<'JS' | ||
$input.length > 14 ? '99.999.999/9999-99' : '999.999.999-99' | ||
JS))->minLength(14); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function cpf(string|Closure $format = '999.999.999-99'): static | ||
{ | ||
$this->dynamic(false) | ||
->mask($format); | ||
|
||
if (self::getValidation()) { | ||
$this->rule('cpf'); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function cnpj(string|Closure $format = '99.999.999/9999-99'): static | ||
{ | ||
$this->dynamic(false) | ||
->mask($format); | ||
|
||
if (self::getValidation()) { | ||
$this->rule('cnpj'); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function validation(bool|Closure $condition = true): static | ||
{ | ||
$this->validation = $condition; | ||
|
||
return $this; | ||
} | ||
|
||
public function getValidation(): bool | ||
{ | ||
return $this->validation; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Leandrocfe\FilamentPtbrFormFields; | ||
|
||
use Closure; | ||
use Filament\Forms\Components\TextInput; | ||
use Illuminate\Support\Str; | ||
|
||
class Money extends TextInput | ||
{ | ||
protected string|int|float|null $initialValue = '0,00'; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this | ||
->prefix('R$') | ||
->maxLength(13) | ||
->extraAlpineAttributes([ | ||
|
||
'x-on:keypress' => 'function() { | ||
var charCode = event.keyCode || event.which; | ||
if (charCode < 48 || charCode > 57) { | ||
event.preventDefault(); | ||
return false; | ||
} | ||
return true; | ||
}', | ||
|
||
'x-on:keyup' => 'function() { | ||
var money = $el.value.replace(/\D/g, ""); | ||
money = (money / 100).toFixed(2) + ""; | ||
money = money.replace(".", ","); | ||
money = money.replace(/(\d)(\d{3})(\d{3}),/g, "$1.$2.$3,"); | ||
money = money.replace(/(\d)(\d{3}),/g, "$1.$2,"); | ||
$el.value = money; | ||
}', | ||
]) | ||
->dehydrateMask() | ||
->default(0.00) | ||
->formatStateUsing(fn ($state) => $state ? number_format(floatval($state), 2, ',', '.') : $this->initialValue); | ||
} | ||
|
||
public function dehydrateMask(bool|Closure $condition = true): static | ||
{ | ||
|
||
if ($condition) { | ||
$this->dehydrateStateUsing( | ||
fn ($state): ?float => $state ? | ||
floatval( | ||
Str::of($state) | ||
->replace('.', '') | ||
->replace(',', '.') | ||
->toString() | ||
) * 10 : | ||
null | ||
); | ||
} else { | ||
$this->dehydrateStateUsing(null); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function initialValue(null|string|int|float|Closure $value = '0,00'): static | ||
{ | ||
$this->initialValue = $value; | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.