Skip to content

Commit

Permalink
Improve MoneyInput field UX + make mask optional
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Jan 17, 2024
1 parent 86279f4 commit b9ca6fa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
14 changes: 12 additions & 2 deletions config/filament-money-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),

/*
|---------------------------------------------------------------------------
Expand All @@ -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),
];
12 changes: 5 additions & 7 deletions src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
13 changes: 12 additions & 1 deletion src/MoneyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/hasMoneyAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b9ca6fa

Please sign in to comment.