Skip to content

Commit

Permalink
Merge pull request #24 from abishekrsrikaanth/add-infolist-macro
Browse files Browse the repository at this point in the history
Adding a new macro to format TextEntry for Infolists
  • Loading branch information
ariaieboy authored May 15, 2024
2 parents b1d0e78 + a8b3a0f commit 940b4f9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@

Filament V3 unlike V2 that uses [laravel-money](https://github.com/akaunting/laravel-money) package for formatting money TextColumns uses PHP native [NumberFormatter](https://www.php.net/manual/en/class.numberformatter.php) class.

This package will add a new `currency(string | Closure $currency = null, bool $shouldConvert = false)` method to the `TextColumn` that uses the Filament V2 money formatter.
### Text Column (Table Builder)

Additionally, you can use the `currency(string | Closure $currency = null, bool $shouldConvert = false)` method when adding Summarizers to the table. Currently this method only exists for the `Sum` and `Average` Summarizer.
A new `currency(string | Closure $currency = null, bool $shouldConvert = false)` method to the `TextColumn` that uses the Filament V2 money formatter.

### Summary (Table Builder)

The summarizer classes `Sum` and `Average` contains the method `currency(string | Closure $currency = null, bool $shouldConvert = false)` to display the value in the configured currency format.

### Text Entry (InfoLists)

A new `currency(string | Closure $currency = null, bool $shouldConvert = false)` method to the `TextEntry`

### Text Input (Form Builder)

We also have a `currencyMask()` method for `TextInput` that lets you mask your numbers in front-end and return the plain number to back-end.

By using this package you can configure the formatter using [laravel-money config](https://github.com/akaunting/laravel-money/blob/master/config/money.php).

For example, you can customize the `symbol`, `symbol_first`, `decimal_mark`, and `thousands_separator` for each currency. Or if you want you can add your custom currency to the config and use it in the `currency()` method instead of standard money.

We also have a `currencyMask()` method for `TextInput` that lets you mask your numbers in front-end and return the plain number to back-end.
## Installation

You can install the package via Composer:
Expand Down Expand Up @@ -43,6 +54,9 @@ php artisan vendor:publish --tag=money
->currency('USD')
->summarize(\Filament\Tables\Columns\Summarizers\Average::make()->currency());

\Filament\Infolists\Components\TextEntry::make('money')
->currency('USD');

\Filament\Forms\Components\TextInput::make('money')
->currencyMask(thousandSeparator: ',',decimalSeparator: '.',precision: 2)
```
Expand Down
14 changes: 14 additions & 0 deletions ide-helper.stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function currency(string | Closure | null $currency = null, bool $shouldC
}

namespace Filament\Forms\Components {

class TextInput
{
public function currencyMask($thousandSeparator = ',', $decimalSeparator = '.', $precision = 2): self
Expand Down Expand Up @@ -42,3 +43,16 @@ public function currency(string | Closure | null $currency = null, bool $shouldC
}
}
}

namespace Filament\Infolists\Components {

use Closure;

class TextEntry
{
public function currency(string | Closure | null $currency = null, bool $shouldConvert = false): self
{
return $this;
}
}
}
15 changes: 15 additions & 0 deletions src/FilamentCurrencyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Filament\Tables\Columns\Column;
use Filament\Tables\Columns\Summarizers;
use Filament\Tables\Columns\TextColumn;
use Filament\Infolists\Components\TextEntry;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand Down Expand Up @@ -41,6 +42,7 @@ public function bootingPackage(): void
$shouldConvert,
))->format();
};

TextColumn::macro('currency', function (string | Closure | null $currency = null, bool $shouldConvert = false) use ($formatter): TextColumn {
/**
* @var TextColumn $this
Expand Down Expand Up @@ -88,5 +90,18 @@ public function bootingPackage(): void

return $this;
});

TextEntry::macro('currency', function (string | Closure | null $currency = null, bool $shouldConvert = false) use ($formatter): TextEntry {
/**
* @var TextEntry $this
*/
$this->formatStateUsing(static function (TextEntry $column, $state) use ($currency, $shouldConvert, $formatter): ?string {

return $formatter($state, $column, $currency, $shouldConvert);

});

return $this;
});
}
}

0 comments on commit 940b4f9

Please sign in to comment.