From 04c7598372ec19db4f4ce1d11256a5fee8ee85d8 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Wed, 20 Nov 2024 20:20:56 +0100 Subject: [PATCH 1/7] Update CanExportRecords.php --- .../actions/src/Concerns/CanExportRecords.php | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/actions/src/Concerns/CanExportRecords.php b/packages/actions/src/Concerns/CanExportRecords.php index 088c019ca9c..058afb78ba7 100644 --- a/packages/actions/src/Concerns/CanExportRecords.php +++ b/packages/actions/src/Concerns/CanExportRecords.php @@ -60,7 +60,7 @@ trait CanExportRecords protected ?Closure $modifyQueryUsing = null; - protected bool | Closure $hasColumnMapping = true; + protected bool | Closure | array $columnMapping = true; protected function setUp(): void { @@ -156,8 +156,11 @@ protected function setUp(): void ->mapWithKeys(fn (array $column, string $columnName): array => [$columnName => $column['label']]) ->all(); } else { + $columnMapping = $this->getColumnMapping(); + $columnMap = collect($exporter::getColumns()) ->mapWithKeys(fn (ExportColumn $column): array => [$column->getName() => $column->getLabel()]) + ->when($columnMapping)->only($columnMapping) ->all(); } @@ -401,15 +404,32 @@ public function modifyQueryUsing(?Closure $callback): static return $this; } - public function columnMapping(bool | Closure $condition = true): static + public function columnMapping(bool | Closure | array $condition = true): static { - $this->hasColumnMapping = $condition; + $this->columnMapping = $condition; return $this; } public function hasColumnMapping(): bool { - return (bool) $this->evaluate($this->hasColumnMapping); + $columnMapping = $this->evaluate($this->columnMapping); + + if (is_array($columnMapping)) { + return false; + } + + return (bool) $columnMapping; + } + + public function getColumnMapping(): array + { + $columnMapping = $this->evaluate($this->columnMapping); + + if (! is_array($columnMapping)) { + return []; + } + + return $columnMapping; } } From 2f4c2568fdafa8ddd1c3ace24f8b03ccdc3c2f32 Mon Sep 17 00:00:00 2001 From: ralphjsmit Date: Wed, 20 Nov 2024 19:28:24 +0000 Subject: [PATCH 2/7] chore: fix code style --- packages/actions/resources/lang/id/import.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/actions/resources/lang/id/import.php b/packages/actions/resources/lang/id/import.php index 738d5f8bf3d..ef3dd4ec834 100644 --- a/packages/actions/resources/lang/id/import.php +++ b/packages/actions/resources/lang/id/import.php @@ -11,15 +11,15 @@ 'form' => [ 'file' => [ - + 'label' => 'Berkas', - + 'placeholder' => 'Unggah berkas CSV', 'rules' => [ 'duplicate_columns' => '{0} Berkas tidak boleh memiliki lebih dari satu kolom header yang kosong.|{1,*} Berkas tidak boleh memiliki kolom header yang duplikat: :columns.', ], - + ], 'columns' => [ From 8bd47ae298a262a903ac19dc99955f2bc6e0c9d9 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Wed, 20 Nov 2024 20:36:20 +0100 Subject: [PATCH 3/7] WIP --- .../actions/src/Concerns/CanExportRecords.php | 76 ++++++++++--------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/packages/actions/src/Concerns/CanExportRecords.php b/packages/actions/src/Concerns/CanExportRecords.php index 058afb78ba7..a1e76b0767a 100644 --- a/packages/actions/src/Concerns/CanExportRecords.php +++ b/packages/actions/src/Concerns/CanExportRecords.php @@ -60,7 +60,9 @@ trait CanExportRecords protected ?Closure $modifyQueryUsing = null; - protected bool | Closure | array $columnMapping = true; + protected bool | Closure $hasColumnMapping = true; + + protected array | Closure $columns = []; protected function setUp(): void { @@ -79,26 +81,31 @@ protected function setUp(): void ->columns(1) ->inlineLabel() ->schema(function () use ($action): array { - return array_map( - fn (ExportColumn $column): Split => Split::make([ - Forms\Components\Checkbox::make('isEnabled') - ->label(__('filament-actions::export.modal.form.columns.form.is_enabled.label', ['column' => $column->getName()])) - ->hiddenLabel() - ->default($column->isEnabledByDefault()) - ->live() - ->grow(false), - Forms\Components\TextInput::make('label') - ->label(__('filament-actions::export.modal.form.columns.form.label.label', ['column' => $column->getName()])) - ->hiddenLabel() - ->default($column->getLabel()) - ->placeholder($column->getLabel()) - ->disabled(fn (Forms\Get $get): bool => ! $get('isEnabled')) - ->required(fn (Forms\Get $get): bool => (bool) $get('isEnabled')), - ]) - ->verticallyAlignCenter() - ->statePath($column->getName()), - $action->getExporter()::getColumns(), - ); + $columns = $action->getColumns(); + $exporterColumns = $action->getExporter()::getColumns(); + + return collect($exporterColumns) + ->when($columns)->filter(fn (ExportColumn $column) => in_array($column->getName(), $columns)) + ->map(function (ExportColumn $column): Split { + return Split::make([ + Forms\Components\Checkbox::make('isEnabled') + ->label(__('filament-actions::export.modal.form.columns.form.is_enabled.label', ['column' => $column->getName()])) + ->hiddenLabel() + ->default($column->isEnabledByDefault()) + ->live() + ->grow(false), + Forms\Components\TextInput::make('label') + ->label(__('filament-actions::export.modal.form.columns.form.label.label', ['column' => $column->getName()])) + ->hiddenLabel() + ->default($column->getLabel()) + ->placeholder($column->getLabel()) + ->disabled(fn (Forms\Get $get): bool => ! $get('isEnabled')) + ->required(fn (Forms\Get $get): bool => (bool) $get('isEnabled')), + ]) + ->verticallyAlignCenter() + ->statePath($column->getName()); + }) + ->all(); }) ->statePath('columnMap')] : []), ...$action->getExporter()::getOptionsFormComponents(), @@ -156,11 +163,11 @@ protected function setUp(): void ->mapWithKeys(fn (array $column, string $columnName): array => [$columnName => $column['label']]) ->all(); } else { - $columnMapping = $this->getColumnMapping(); + $columns = $this->getColumns(); $columnMap = collect($exporter::getColumns()) ->mapWithKeys(fn (ExportColumn $column): array => [$column->getName() => $column->getLabel()]) - ->when($columnMapping)->only($columnMapping) + ->when($columns)->only($columns) ->all(); } @@ -406,30 +413,25 @@ public function modifyQueryUsing(?Closure $callback): static public function columnMapping(bool | Closure | array $condition = true): static { - $this->columnMapping = $condition; + $this->hasColumnMapping = $condition; return $this; } public function hasColumnMapping(): bool { - $columnMapping = $this->evaluate($this->columnMapping); - - if (is_array($columnMapping)) { - return false; - } - - return (bool) $columnMapping; + return (bool) $this->evaluate($this->hasColumnMapping); } - public function getColumnMapping(): array + public function columns(array | Closure $columns): static { - $columnMapping = $this->evaluate($this->columnMapping); + $this->columns = $columns; - if (! is_array($columnMapping)) { - return []; - } + return $this; + } - return $columnMapping; + public function getColumns(): array + { + return $this->evaluate($this->columns); } } From b1169d3c62515cde9821417f53a2bf5dbf951e71 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Wed, 20 Nov 2024 20:39:05 +0100 Subject: [PATCH 4/7] Update CanExportRecords.php --- packages/actions/src/Concerns/CanExportRecords.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/actions/src/Concerns/CanExportRecords.php b/packages/actions/src/Concerns/CanExportRecords.php index a1e76b0767a..f4099abe8a6 100644 --- a/packages/actions/src/Concerns/CanExportRecords.php +++ b/packages/actions/src/Concerns/CanExportRecords.php @@ -411,7 +411,7 @@ public function modifyQueryUsing(?Closure $callback): static return $this; } - public function columnMapping(bool | Closure | array $condition = true): static + public function columnMapping(bool | Closure $condition = true): static { $this->hasColumnMapping = $condition; From 9e6eed44e31d99ca96c155613d219cd5840036d2 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Wed, 20 Nov 2024 20:53:54 +0100 Subject: [PATCH 5/7] Fix PhpStan --- .../actions/src/Concerns/CanExportRecords.php | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/actions/src/Concerns/CanExportRecords.php b/packages/actions/src/Concerns/CanExportRecords.php index f4099abe8a6..832bc0164ed 100644 --- a/packages/actions/src/Concerns/CanExportRecords.php +++ b/packages/actions/src/Concerns/CanExportRecords.php @@ -62,6 +62,9 @@ trait CanExportRecords protected bool | Closure $hasColumnMapping = true; + /** + * @var array|Closure + */ protected array | Closure $columns = []; protected function setUp(): void @@ -82,10 +85,11 @@ protected function setUp(): void ->inlineLabel() ->schema(function () use ($action): array { $columns = $action->getColumns(); - $exporterColumns = $action->getExporter()::getColumns(); - return collect($exporterColumns) - ->when($columns)->filter(fn (ExportColumn $column) => in_array($column->getName(), $columns)) + return collect($action->getExporter()::getColumns()) + ->when($columns, function (Collection $exporterColumns) use ($columns): Collection { + return $exporterColumns->filter(fn (ExportColumn $column) => in_array($column->getName(), $columns)); + }) ->map(function (ExportColumn $column): Split { return Split::make([ Forms\Components\Checkbox::make('isEnabled') @@ -167,7 +171,9 @@ protected function setUp(): void $columnMap = collect($exporter::getColumns()) ->mapWithKeys(fn (ExportColumn $column): array => [$column->getName() => $column->getLabel()]) - ->when($columns)->only($columns) + ->when($columns, function (Collection $exporterColumns) use ($columns) { + return $exporterColumns->only($columns); + }) ->all(); } @@ -423,6 +429,9 @@ public function hasColumnMapping(): bool return (bool) $this->evaluate($this->hasColumnMapping); } + /** + * @param array|Closure $columns + */ public function columns(array | Closure $columns): static { $this->columns = $columns; @@ -430,6 +439,9 @@ public function columns(array | Closure $columns): static return $this; } + /** + * @return array + */ public function getColumns(): array { return $this->evaluate($this->columns); From 9a102d0c28b8a75701f5e2c7ee0c43b1c18c7e0c Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Wed, 20 Nov 2024 20:56:38 +0100 Subject: [PATCH 6/7] Docs --- .../docs/07-prebuilt-actions/09-export.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/actions/docs/07-prebuilt-actions/09-export.md b/packages/actions/docs/07-prebuilt-actions/09-export.md index 2827aefc9df..68ff82003bf 100644 --- a/packages/actions/docs/07-prebuilt-actions/09-export.md +++ b/packages/actions/docs/07-prebuilt-actions/09-export.md @@ -142,6 +142,31 @@ ExportAction::make() ->columnMapping(false) ``` +### Limiting selectable columns + +By default, the user will be asked which columns to export. You can limit the list of columns shown for export by using the `columns()` method: + +```php +use App\Filament\Exports\ProductExporter; + +ExportAction::make() + ->exporter(ProductExporter::class) + ->columns([ 'name', 'sku', 'price']) +]) +``` + +In case you would like to disable the column selection functionality and only export the defined columns, you can combine the `columns()` method with `columnMapping(false)`: + +```php +use App\Filament\Exports\ProductExporter; + +ExportAction::make() + ->exporter(ProductExporter::class) + ->columns([ 'name', 'sku', 'price']) + ->columnMapping(false) +]) +``` + ### Calculated export column state Sometimes you need to calculate the state of a column, instead of directly reading it from a database column. From 8474d39563b6fd05c5b4048e2bba35db33d5a2c7 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Wed, 20 Nov 2024 21:18:16 +0100 Subject: [PATCH 7/7] Fix doc --- packages/actions/docs/07-prebuilt-actions/09-export.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/actions/docs/07-prebuilt-actions/09-export.md b/packages/actions/docs/07-prebuilt-actions/09-export.md index 68ff82003bf..ae0744d8be5 100644 --- a/packages/actions/docs/07-prebuilt-actions/09-export.md +++ b/packages/actions/docs/07-prebuilt-actions/09-export.md @@ -151,7 +151,7 @@ use App\Filament\Exports\ProductExporter; ExportAction::make() ->exporter(ProductExporter::class) - ->columns([ 'name', 'sku', 'price']) + ->columns(['name', 'sku', 'price']) ]) ``` @@ -162,7 +162,7 @@ use App\Filament\Exports\ProductExporter; ExportAction::make() ->exporter(ProductExporter::class) - ->columns([ 'name', 'sku', 'price']) + ->columns(['name', 'sku', 'price']) ->columnMapping(false) ]) ```