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

[4.x] Add decimalPlaces argument to component money function #15841

Merged
merged 2 commits into from
Mar 12, 2025
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
7 changes: 4 additions & 3 deletions packages/infolists/src/Components/Concerns/CanFormatState.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ public function isoTimeTooltip(?string $format = null, ?string $timezone = null)
return $this;
}

public function money(string | Closure | null $currency = null, int $divideBy = 0, string | Closure | null $locale = null): static
public function money(string | Closure | null $currency = null, int $divideBy = 0, string | Closure | null $locale = null, int | Closure | null $decimalPlaces = null): static
{
$this->isMoney = true;

$this->formatStateUsing(static function (TextEntry $component, $state) use ($currency, $divideBy, $locale): ?string {
$this->formatStateUsing(static function (TextEntry $component, $state) use ($currency, $divideBy, $locale, $decimalPlaces): ?string {
if (blank($state)) {
return null;
}
Expand All @@ -232,12 +232,13 @@ public function money(string | Closure | null $currency = null, int $divideBy =

$currency = $component->evaluate($currency) ?? Schema::$defaultCurrency;
$locale = $component->evaluate($locale) ?? Schema::$defaultNumberLocale ?? config('app.locale');
$decimalPlaces = $component->evaluate($decimalPlaces);

if ($divideBy) {
$state /= $divideBy;
}

return Number::currency($state, $currency, $locale);
return Number::currency($state, $currency, $locale, $decimalPlaces);
});

return $this;
Expand Down
9 changes: 9 additions & 0 deletions packages/schemas/docs/03-infolists/02-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ use Filament\Infolists\Infolist;
Infolist::$defaultNumberLocale = 'nl';
```

If you would like to customize the number of decimal places used to format the number with, you can use the `decimalPlaces` argument:

```php
use Filament\Infolists\Components\TextEntry;

TextEntry::make('price')
->money('EUR', decimalPlaces: 3)
```

## Limiting text length

You may `limit()` the length of the entry's value:
Expand Down
9 changes: 9 additions & 0 deletions packages/tables/docs/02-columns/02-text.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ use Filament\Tables\Table;
Table::$defaultNumberLocale = 'nl';
```

If you would like to customize the number of decimal places used to format the number with, you can use the `decimalPlaces` argument:

```php
use Filament\Tables\Columns\TextColumn;

TextColumn::make('price')
->money('EUR', decimalPlaces: 3)
```

## Limiting text length

You may `limit()` the length of the cell's value:
Expand Down
9 changes: 9 additions & 0 deletions packages/tables/docs/06-summaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ use Filament\Tables\Table;
Table::$defaultNumberLocale = 'nl';
```

If you would like to customize the number of decimal places used to format the number with, you can use the `decimalPlaces` argument:

```php
use Filament\Tables\Columns\TextColumn;

TextColumn::make('price')
->summarize(Sum::make()->money('EUR', decimalPlaces: 3))
```

### Limiting text length

You may `limit()` the length of the summary's value:
Expand Down
7 changes: 4 additions & 3 deletions packages/tables/src/Columns/Concerns/CanFormatState.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ public function sinceTooltip(?string $timezone = null): static
return $this;
}

public function money(string | Closure | null $currency = null, int $divideBy = 0, string | Closure | null $locale = null): static
public function money(string | Closure | null $currency = null, int $divideBy = 0, string | Closure | null $locale = null, int | Closure | null $decimalPlaces = null): static
{
$this->isMoney = true;

$this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $divideBy, $locale): ?string {
$this->formatStateUsing(static function (TextColumn $column, $state) use ($currency, $divideBy, $locale, $decimalPlaces): ?string {
if (blank($state)) {
return null;
}
Expand All @@ -230,12 +230,13 @@ public function money(string | Closure | null $currency = null, int $divideBy =

$currency = $column->evaluate($currency) ?? Table::$defaultCurrency;
$locale = $column->evaluate($locale) ?? Table::$defaultNumberLocale ?? config('app.locale');
$decimalPlaces = $column->evaluate($decimalPlaces);

if ($divideBy) {
$state /= $divideBy;
}

return Number::currency($state, $currency, $locale);
return Number::currency($state, $currency, $locale, $decimalPlaces);
});

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function limit(int $length = 100, ?string $end = '...'): static
return $this;
}

public function money(string | Closure | null $currency = null, int $divideBy = 0, string | Closure | null $locale = null): static
public function money(string | Closure | null $currency = null, int $divideBy = 0, string | Closure | null $locale = null, int | Closure | null $decimalPlaces = null): static
{
$this->formatStateUsing(static function ($state, Summarizer $summarizer) use ($currency, $divideBy, $locale): ?string {
$this->formatStateUsing(static function ($state, Summarizer $summarizer) use ($currency, $divideBy, $locale, $decimalPlaces): ?string {
if (blank($state)) {
return null;
}
Expand All @@ -61,12 +61,13 @@ public function money(string | Closure | null $currency = null, int $divideBy =

$currency = $summarizer->evaluate($currency) ?? Table::$defaultCurrency;
$locale = $summarizer->evaluate($locale) ?? Table::$defaultNumberLocale ?? config('app.locale');
$decimalPlaces = $summarizer->evaluate($decimalPlaces);

if ($divideBy) {
$state /= $divideBy;
}

return Number::currency($state, $currency, $locale);
return Number::currency($state, $currency, $locale, $decimalPlaces);
});

return $this;
Expand Down