diff --git a/packages/actions/docs/07-prebuilt-actions/08-import.md b/packages/actions/docs/07-prebuilt-actions/08-import.md index 15e4feb2e4..e60a3246c3 100644 --- a/packages/actions/docs/07-prebuilt-actions/08-import.md +++ b/packages/actions/docs/07-prebuilt-actions/08-import.md @@ -494,6 +494,15 @@ ImportColumn::make('sku') ->example('ABC123') ``` +Or if you want to add more than one example row, you can pass an array to the `examples()` method: + +```php +use Filament\Actions\Imports\ImportColumn; + +ImportColumn::make('sku') + ->examples(['ABC123', 'DEF456']) +``` + By default, the name of the column is used in the header of the example CSV. You can customize the header per-column using `exampleHeader()`: ```php diff --git a/packages/actions/src/Concerns/CanImportRecords.php b/packages/actions/src/Concerns/CanImportRecords.php index fcd050bae7..06d755ee33 100644 --- a/packages/actions/src/Concerns/CanImportRecords.php +++ b/packages/actions/src/Concerns/CanImportRecords.php @@ -334,18 +334,27 @@ trait CanImportRecords $columns, )); - $example = array_map( - fn (ImportColumn $column) => $column->getExample(), + $columnExamples = array_map( + fn (ImportColumn $column): array => $column->getExamples(), $columns, ); - if (array_filter( - $example, - fn ($value): bool => filled($value), - )) { - $csv->insertOne($example); + $exampleRowsCount = array_reduce( + $columnExamples, + fn (int $count, array $exampleData): int => max($count, count($exampleData)), + initial: 0, + ); + + $exampleRows = []; + + foreach ($columnExamples as $exampleData) { + for ($i = 0; $i < $exampleRowsCount; $i++) { + $exampleRows[$i][] = $exampleData[$i] ?? ''; + } } + $csv->insertAll($exampleRows); + return response()->streamDownload(function () use ($csv) { echo $csv->toString(); }, __('filament-actions::import.example_csv.file_name', ['importer' => (string) str($this->getImporter())->classBasename()->kebab()]), [ diff --git a/packages/actions/src/Imports/ImportColumn.php b/packages/actions/src/Imports/ImportColumn.php index 654f9d33f7..e6f05a6e0d 100644 --- a/packages/actions/src/Imports/ImportColumn.php +++ b/packages/actions/src/Imports/ImportColumn.php @@ -51,7 +51,10 @@ class ImportColumn extends Component protected ?Importer $importer = null; - protected mixed $example = null; + /** + * @var array | Closure + */ + protected array | Closure $examples = []; protected string | Closure | null $exampleHeader = null; @@ -111,7 +114,21 @@ class ImportColumn extends Component public function example(mixed $example): static { - $this->example = $example; + $this->examples($example); + + return $this; + } + + public function examples(mixed $examples): static + { + if ( + (! is_array($examples)) && + (! $examples instanceof Closure) + ) { + $examples = Arr::wrap($examples); + } + + $this->examples = $examples; return $this; } @@ -432,9 +449,20 @@ class ImportColumn extends Component return $this->importer; } + /** + * @deprecated Use `getExamples()` instead. + */ public function getExample(): mixed { - return $this->evaluate($this->example); + return Arr::first($this->getExamples()); + } + + /** + * @return array + */ + public function getExamples(): array + { + return Arr::wrap($this->evaluate($this->examples)); } /**