From b9ca6faed76e9b138155aa9951ed979572974a51 Mon Sep 17 00:00:00 2001 From: Peter Elmered Date: Wed, 17 Jan 2024 14:41:42 +0100 Subject: [PATCH] Improve MoneyInput field UX + make mask optional --- config/filament-money-field.php | 14 ++++++++++++-- src/Forms/Components/MoneyInput.php | 12 +++++------- src/MoneyFormatter.php | 13 ++++++++++++- src/hasMoneyAttributes.php | 2 +- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/config/filament-money-field.php b/config/filament-money-field.php index eaab5db..72369aa 100644 --- a/config/filament-money-field.php +++ b/config/filament-money-field.php @@ -8,10 +8,21 @@ |--------------------------------------------------------------------------- | | If not set, it will use the Laravel app locale. + | For example: en_US, en_GB, sv_SE, etc. | */ 'default_locale' => env('MONEY_DEFAULT_LOCALE', 'en_US'), + /* + |--------------------------------------------------------------------------- + | Default currency + |--------------------------------------------------------------------------- + | + | The currency ISO code to use if not set on the field. + | For example: USD, EUR, SEK, etc. + | + */ + 'default_currency' => env('MONEY_DEFAULT_CURRENCY', 'USD'), /* |--------------------------------------------------------------------------- @@ -21,6 +32,5 @@ | The currency code to use if not set on the field. | */ - 'default_currency' => env('MONEY_DEFAULT_CURRENCY', ), - + 'use_input_mask' => env('MONEY_USE_INPUT_MASK', true), ]; diff --git a/src/Forms/Components/MoneyInput.php b/src/Forms/Components/MoneyInput.php index 08cd2f0..a1a91f7 100644 --- a/src/Forms/Components/MoneyInput.php +++ b/src/Forms/Components/MoneyInput.php @@ -23,20 +23,18 @@ protected function setUp(): void $this->prefix($formattingRules->currencySymbol); - //$this->mask(RawJs::make('$money($input, \'' . $formattingRules->decimalSeparator . '\', \'' . $formattingRules->groupingSeparator . '\', '.$formattingRules->fractionDigits.')')); + if(config('filament-money-field.use_input_mask')) { + $this->mask(RawJs::make('$money($input, \'' . $formattingRules->decimalSeparator . '\', \'' . $formattingRules->groupingSeparator . '\', ' . $formattingRules->fractionDigits . ')')); + } $this->stripCharacters($formattingRules->groupingSeparator); $this->inputMode('decimal'); $this->rule('numeric'); $this->step(0.01); $this->minValue = 0; - /* - $this->afterStateHydrated(static function (MoneyInput $component, $state): void { - $component->state($state/100); + $this->formatStateUsing(static function (MoneyInput $component, $state): string { + return MoneyFormatter::decimalToMoneyString($state/100, $component->getLocale()); }); - */ - - $this->formatStateUsing(fn (string $state): string => (int) $state/100); $this->dehydrateStateUsing(static function (MoneyInput $component, $state): string { diff --git a/src/MoneyFormatter.php b/src/MoneyFormatter.php index 1ee050b..68a3b40 100644 --- a/src/MoneyFormatter.php +++ b/src/MoneyFormatter.php @@ -14,7 +14,6 @@ public static function format($value, $currency, $locale): string $numberFormatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); $numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 2); - //$numberFormatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $currency); $moneyFormatter = new IntlMoneyFormatter($numberFormatter, $currencies); @@ -34,4 +33,16 @@ public static function getFormattingRules($locale): MoneyFormattingRules groupingSeparator: $numberFormatter->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL), ); } + + public static function decimalToMoneyString($moneyString, $locale): string + { + $formattingRules = self::getFormattingRules($locale); + $moneyString = (string) $moneyString; + + if($formattingRules->decimalSeparator === ',') { + $moneyString = str_replace('.', ',', (string) $moneyString); + } + + return $moneyString; + } } diff --git a/src/hasMoneyAttributes.php b/src/hasMoneyAttributes.php index ac54dba..3a8a4fa 100644 --- a/src/hasMoneyAttributes.php +++ b/src/hasMoneyAttributes.php @@ -26,7 +26,7 @@ public function currency(string|\Closure|null $currencyCode = null): static $currencies = new ISOCurrencies(); if (! $currencies->contains($this->currency)) { - throw new \Exception('Currency not supported: '.$currencyCode); + throw new \RuntimeException('Currency not supported: ' . $currencyCode); } return $this;