From 98564189e3ba5dcc9530eb77270c925aa6c36388 Mon Sep 17 00:00:00 2001 From: "Leandro C. Ferreira" Date: Wed, 27 Mar 2024 17:38:11 -0300 Subject: [PATCH 1/3] wip --- src/Money.php | 62 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 19 deletions(-) diff --git a/src/Money.php b/src/Money.php index 4218e3e..19ca5be 100644 --- a/src/Money.php +++ b/src/Money.php @@ -5,6 +5,7 @@ use Closure; use Filament\Forms\Components\TextInput; use Illuminate\Support\Str; +use Illuminate\Support\Stringable; class Money extends TextInput { @@ -27,13 +28,10 @@ protected function setUp(): void }', '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; + var money = $el.value; + money = money.replace(/\D/g, \'\'); + money = (parseFloat(money) / 100).toLocaleString(\'pt-BR\', { minimumFractionDigits: 2 }); + $el.value = money === \'NaN\' ? \'0,00\' : money; }', ]) ->dehydrateMask() @@ -43,20 +41,10 @@ protected function setUp(): void 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 - ); + $this->dehydrateStateUsing(fn (?string $state): ?float => $this->convertToFloat($state)); } else { - $this->dehydrateStateUsing(null); + $this->dehydrateStateUsing(fn (?string $state): ?string => $this->convertToNumberFormat($state)); } return $this; @@ -68,4 +56,40 @@ public function initialValue(null|string|int|float|Closure $value = '0,00'): sta return $this; } + + private function sanitizeState(?string $state): ?Stringable + { + $state = Str::of($state) + ->replace('.', '') + ->replace(',', ''); + + return $state ?? null; + } + + private function convertToFloat(Stringable|string|null $state): float + { + $state = $this->sanitizeState($state); + + if (! $state) { + return 0; + } + + if ($state->length() > 2) { + $state = $state + ->substr(0, $state->length() - 2) + ->append('.') + ->append($state->substr($state->length() - 2, 2)); + } else { + $state = $state->prepend('0.'); + } + + return floatval($state->toString()) ?? 0; + } + + private function convertToNumberFormat(string $state): string + { + $state = $this->convertToFloat($state); + + return number_format($state, 2, ',', '.') ?? 0; + } } From 00f5f26f2db4c206ef61efc9df94d3dfef65c6a0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 25 Mar 2024 20:08:25 +0000 Subject: [PATCH 2/3] Bump aglipanci/laravel-pint-action from 2.3.0 to 2.3.1 Bumps [aglipanci/laravel-pint-action](https://github.com/aglipanci/laravel-pint-action) from 2.3.0 to 2.3.1. - [Release notes](https://github.com/aglipanci/laravel-pint-action/releases) - [Commits](https://github.com/aglipanci/laravel-pint-action/compare/2.3.0...2.3.1) --- updated-dependencies: - dependency-name: aglipanci/laravel-pint-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- .github/workflows/fix-php-code-style-issues.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml index 7520a18..c958238 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.3.0 + uses: aglipanci/laravel-pint-action@2.3.1 - name: Commit changes uses: stefanzweifel/git-auto-commit-action@v4 From 963144c6dd9bc653eb55a74006570e9cb85bde9c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 15 Apr 2024 20:08:56 +0000 Subject: [PATCH 3/3] Bump aglipanci/laravel-pint-action from 2.3.1 to 2.4 Bumps [aglipanci/laravel-pint-action](https://github.com/aglipanci/laravel-pint-action) from 2.3.1 to 2.4. - [Release notes](https://github.com/aglipanci/laravel-pint-action/releases) - [Commits](https://github.com/aglipanci/laravel-pint-action/compare/2.3.1...2.4) --- updated-dependencies: - dependency-name: aglipanci/laravel-pint-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- .github/workflows/fix-php-code-style-issues.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml index c958238..78fc54a 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.3.1 + uses: aglipanci/laravel-pint-action@2.4 - name: Commit changes uses: stefanzweifel/git-auto-commit-action@v4