Merge branch 'pr/11383' into 3.x

This commit is contained in:
Dan Harrin
2024-02-19 13:15:54 +00:00
2 changed files with 45 additions and 12 deletions
@@ -124,6 +124,18 @@ ExportColumn::make('description')
->enabledByDefault(false)
```
### Disabling column selection
By default, user will be asked which columns they would like to export. You can disable this functionality using `columnMapping(false)`:
```php
use App\Filament\Exports\ProductExporter;
ExportAction::make()
->exporter(ProductExporter::class)
->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.
@@ -61,6 +61,8 @@ trait CanExportRecords
protected ?Closure $modifyQueryUsing = null;
protected bool | Closure $hasColumnMapping = true;
protected function setUp(): void
{
parent::setUp();
@@ -73,8 +75,8 @@ trait CanExportRecords
$this->groupedIcon(FilamentIcon::resolve('actions::export-action.grouped') ?? 'heroicon-m-arrow-down-tray');
$this->form(fn (ExportAction | ExportTableAction | ExportTableBulkAction $action): array => array_merge([
Fieldset::make(__('filament-actions::export.modal.form.columns.label'))
$this->form(fn (ExportAction | ExportTableAction | ExportTableBulkAction $action): array => [
...($action->hasColumnMapping() ? [Fieldset::make(__('filament-actions::export.modal.form.columns.label'))
->columns(1)
->inlineLabel()
->schema(function () use ($action): array {
@@ -99,8 +101,9 @@ trait CanExportRecords
$action->getExporter()::getColumns(),
);
})
->statePath('columnMap'),
], $action->getExporter()::getOptionsFormComponents()));
->statePath('columnMap')] : []),
...$action->getExporter()::getOptionsFormComponents(),
]);
$this->action(function (ExportAction | ExportTableAction | ExportTableBulkAction $action, array $data, Component $livewire) {
if ($livewire instanceof HasTable) {
@@ -139,14 +142,20 @@ trait CanExportRecords
Arr::except($data, ['columnMap']),
);
$columnMap = collect($data['columnMap'])
->dot()
->reduce(fn (Collection $carry, mixed $value, string $key): Collection => $carry->mergeRecursive([
Str::beforeLast($key, '.') => [Str::afterLast($key, '.') => $value],
]), collect())
->filter(fn (array $column): bool => $column['isEnabled'] ?? false)
->mapWithKeys(fn (array $column, string $columnName): array => [$columnName => $column['label']])
->all();
if ($action->hasColumnMapping()) {
$columnMap = collect($data['columnMap'])
->dot()
->reduce(fn (Collection $carry, mixed $value, string $key): Collection => $carry->mergeRecursive([
Str::beforeLast($key, '.') => [Str::afterLast($key, '.') => $value],
]), collect())
->filter(fn (array $column): bool => $column['isEnabled'] ?? false)
->mapWithKeys(fn (array $column, string $columnName): array => [$columnName => $column['label']])
->all();
} else {
$columnMap = collect($action->getExporter()::getColumns())
->mapWithKeys(fn (ExportColumn $column): array => [$column->getName() => $column->getLabel()])
->all();
}
$export = app(Export::class);
$export->user()->associate($user);
@@ -387,4 +396,16 @@ trait CanExportRecords
return $this;
}
public function columnMapping(bool | Closure $condition = true): static
{
$this->hasColumnMapping = $condition;
return $this;
}
public function hasColumnMapping(): bool
{
return (bool) $this->evaluate($this->hasColumnMapping);
}
}