Skip to content

Commit

Permalink
Merge pull request #22 from kainiklas/main
Browse files Browse the repository at this point in the history
defer configuration using closure
  • Loading branch information
pelmered authored Apr 22, 2024
2 parents 821912b + 533c69e commit 77ee5f7
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ protected function setUp(): void
{
parent::setUp();

$this->prepare($this);
$this->prepare();

$this->formatStateUsing(function (MoneyInput $component, $state): ?string {

$this->prepare($component);
$this->prepare();

$currency = $component->getCurrency();
$locale = $component->getLocale();

if (is_null($state)) {
return '';
}
if(!is_numeric($state)) {
if (!is_numeric($state)) {
return $state;
}

Expand All @@ -40,25 +40,32 @@ protected function setUp(): void
$currency = $component->getCurrency();
$state = MoneyFormatter::parseDecimal($state, $currency, $component->getLocale());

$this->prepare($component);
$this->prepare();

return $state;
});
}

protected function prepare(MoneyInput $component): void
protected function prepare(): void
{
$formattingRules = MoneyFormatter::getFormattingRules($component->getLocale());
$symbolPlacement = Config::get('filament-money-field.form_currency_symbol_placement', 'before');

$getCurrencySymbol = function (MoneyInput $component) {
$formattingRules = MoneyFormatter::getFormattingRules($component->getLocale());
return $formattingRules->currencySymbol;
};

if ($symbolPlacement === 'before') {
$this->prefix($formattingRules->currencySymbol);
$this->prefix($getCurrencySymbol);
} else {
$this->suffix($formattingRules->currencySymbol);
$this->suffix($getCurrencySymbol);
}

if (config('filament-money-field.use_input_mask')) {
$this->mask(RawJs::make('$money($input, \'' . $formattingRules->decimalSeparator . '\', \'' . $formattingRules->groupingSeparator . '\', ' . $formattingRules->fractionDigits . ')'));
$this->mask(function (MoneyInput $component) {
$formattingRules = MoneyFormatter::getFormattingRules($component->getLocale());
return RawJs::make('$money($input, \'' . $formattingRules->decimalSeparator . '\', \'' . $formattingRules->groupingSeparator . '\', ' . $formattingRules->fractionDigits . ')');
});
}
}

Expand Down

0 comments on commit 77ee5f7

Please sign in to comment.