Skip to content

Commit

Permalink
Fixes for symbolPlacement + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmered committed Jul 24, 2024
1 parent f741db8 commit a35c796
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/Forms/Components/MoneyInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MoneyInput extends TextInput
{
use HasMoneyAttributes;

protected string $symbolPlacement;
protected ?string $symbolPlacement = null;

protected function setUp(): void
{
Expand Down Expand Up @@ -60,9 +60,9 @@ protected function prepare(): void
};

match ($symbolPlacement) {
'before' => $this->prefix($getCurrencySymbol),
'after' => $this->suffix($getCurrencySymbol),
'hidden' => null,
'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')) {
Expand All @@ -85,7 +85,7 @@ protected function prepare(): void

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

public function symbolPlacement(string|Closure|null $symbolPlacement = null): static
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 a35c796

Please sign in to comment.