mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Merge pull request #13171 from Sergey-Karapetyan/Sergey-Karapetyan-3.x
Allow multiple lines to be added to the import example file.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()]), [
|
||||
|
||||
@@ -51,7 +51,10 @@ class ImportColumn extends Component
|
||||
|
||||
protected ?Importer $importer = null;
|
||||
|
||||
protected mixed $example = null;
|
||||
/**
|
||||
* @var array<mixed> | 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<mixed>
|
||||
*/
|
||||
public function getExamples(): array
|
||||
{
|
||||
return Arr::wrap($this->evaluate($this->examples));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user