Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set symbol placement on field + support for symbol placement=hidden #48

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
{
use HasMoneyAttributes;

protected ?string $symbolPlacement = null;

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

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 @@
}
}

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".');

Check warning on line 96 in src/Forms/Components/MoneyInput.php

View check run for this annotation

Codecov / codecov/patch

src/Forms/Components/MoneyInput.php#L96

Added line #L96 was not covered by tests
}

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