Skip to content

Commit

Permalink
Merge pull request #48 from pelmered/feature/symbol-placement-on-fiel…
Browse files Browse the repository at this point in the history
…d+symbol-placement-none

Set symbol placement on field + support for symbol placement=hidden
  • Loading branch information
pelmered authored Jul 24, 2024
2 parents eeb6198 + cb84835 commit 1c26b1a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ MoneyInput::make('price')
->maxValue(10000) // Add min and max value (in minor units, i.e. cents) to the input field. In this case no values over 100
->step(100) // Step value for the input field. In this case only multiples of 100 are allowed.
->decimals(0)
->getSymbolPlacement('after'), // Possible options: 'after', 'before', 'none'. Defaults to 'before'
```

### Table column
Expand Down
2 changes: 1 addition & 1 deletion config/filament-money-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
| Currency symbol placement
|---------------------------------------------------------------------------
|
| Where the dunit should be on form fields. Options are 'before' (prefix), 'after' (suffix) or 'none'.
| Where the dunit should be on form fields. Options are 'before' (prefix), 'after' (suffix) or 'hidden'.
| Note: In most non-English speaking European countries,
| the currency symbol is after the amount and is preceded by a space (as in "10 €")
|
Expand Down
35 changes: 26 additions & 9 deletions src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class MoneyInput extends TextInput
{
use HasMoneyAttributes;

protected ?string $symbolPlacement = null;

protected function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -52,21 +54,20 @@ protected function setUp(): void

protected function prepare(): void
{
$symbolPlacement = Config::get('filament-money-field.form_currency_symbol_placement', 'before');

$symbolPlacement = $this->getSymbolPlacement();
$getCurrencySymbol = function (MoneyInput $component) {
return MoneyFormatter::getFormattingRules($component->getLocale())->currencySymbol;
return MoneyFormatter::getFormattingRules($component->getLocale(), $component->getCurrency())->currencySymbol;
};

if ($symbolPlacement === 'before') {
$this->prefix($getCurrencySymbol);
} else {
$this->suffix($getCurrencySymbol);
}
match ($symbolPlacement) {
'before' => $this->prefix($getCurrencySymbol)->suffix(null),
'after' => $this->suffix($getCurrencySymbol)->prefix(null),
'hidden' => $this->suffix(null)->prefix(null),
};

if (config('filament-money-field.use_input_mask')) {
$this->mask(function (MoneyInput $component) {
$formattingRules = MoneyFormatter::getFormattingRules($component->getLocale());
$formattingRules = MoneyFormatter::getFormattingRules($component->getLocale(), $component->getCurrency());

return RawJs::make(
strtr(
Expand All @@ -82,6 +83,22 @@ protected function prepare(): void
}
}

public function getSymbolPlacement()
{
return $this->symbolPlacement ?? config('filament-money-field.form_currency_symbol_placement', 'before');
}

public function symbolPlacement(string|Closure|null $symbolPlacement = null): static
{
$this->symbolPlacement = $this->evaluate($symbolPlacement);

if (! in_array($this->symbolPlacement, ['before', 'after', 'hidden'])) {
throw new \InvalidArgumentException('Symbol placement must be either "before", "after" or "hidden".');
}

return $this;
}

public function minValue(mixed $value): static
{
$this->rule(new MinValueRule((int) $this->evaluate($value), $this));
Expand Down
1 change: 1 addition & 0 deletions src/HasMoneyAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function locale(string|Closure|null $locale = null): static
return $this;
}


public function decimals(int|Closure $decimals): static
{
$this->decimals = $this->evaluate($decimals);
Expand Down
66 changes: 61 additions & 5 deletions tests/FormInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,92 @@ public function testNonNumericState(): void
$component->getState();
}

public function testCurrencySymbolPlacementAfter()
public function testCurrencySymbolPlacementAfterInGlobalConfig()
{
config(['filament-money-field.form_currency_symbol_placement' => 'after']);

$component = ComponentContainer::make(FormTestComponent::make())
->statePath('data')
->components([
MoneyInput::make('price'),
])->fill(['price' => 20]);

/** @var MoneyInput $field */
$field = $component->getComponent('data.price');
$this->assertEquals('$', $field->getSuffixLabel());
$this->assertNull($field->getPrefixLabel());
}

public function testCurrencySymbolPlacementBeforeInGlobalConfig()
{
config(['filament-money-field.form_currency_symbol_placement' => 'before']);

$component = ComponentContainer::make(FormTestComponent::make())
->statePath('data')
->components([
MoneyInput::make('price'),
])->fill(['price' => 20]);

$field = $component->getComponent('data.price');
$this->assertEquals('$', $field->getSuffixLabel());
$this->assertNull($field->getPrefixLabel());
$this->assertEquals('$', $field->getPrefixLabel());
$this->assertNull($field->getSuffixLabel());
}

public function testCurrencySymbolPlacementBefore()
public function testCurrencySymbolPlacementHiddenInGlobalConfig()
{
config(['filament-money-field.form_currency_symbol_placement' => 'before']);
config(['filament-money-field.form_currency_symbol_placement' => 'hidden']);

$component = ComponentContainer::make(FormTestComponent::make())
->statePath('data')
->components([
MoneyInput::make('price'),
])->fill(['price' => 20]);

$field = $component->getComponent('data.price');
$this->assertNull($field->getPrefixLabel());
$this->assertNull($field->getSuffixLabel());
}

public function testCurrencySymbolPlacementAfterOnField()
{
$component = ComponentContainer::make(FormTestComponent::make())
->statePath('data')
->components([
MoneyInput::make('price')->symbolPlacement('after'),
])->fill(['price' => 20]);

$field = $component->getComponent('data.price');
//dd($field);
$this->assertEquals('$', $field->getSuffixLabel());
$this->assertNull($field->getPrefixLabel());
}

public function testCurrencySymbolPlacementBeforeOnField()
{
$component = ComponentContainer::make(FormTestComponent::make())
->statePath('data')
->components([
MoneyInput::make('price')->symbolPlacement('before'),
])->fill(['price' => 20]);

$field = $component->getComponent('data.price');
$this->assertEquals('$', $field->getPrefixLabel());
$this->assertNull($field->getSuffixLabel());
}

public function testCurrencySymbolPlacementHiddenOnField()
{
$component = ComponentContainer::make(FormTestComponent::make())
->statePath('data')
->components([
MoneyInput::make('price')->symbolPlacement('hidden'),
])->fill(['price' => 20]);

$field = $component->getComponent('data.price');
$this->assertNull($field->getPrefixLabel());
$this->assertNull($field->getSuffixLabel());
}

public function testInputMask()
{
config(['filament-money-field.use_input_mask' => true]);
Expand Down

0 comments on commit 1c26b1a

Please sign in to comment.