Skip to content

Commit

Permalink
Merge pull request #13171 from Sergey-Karapetyan/Sergey-Karapetyan-3.x
Browse files Browse the repository at this point in the history
Allow multiple lines to be added to the import example file.
  • Loading branch information
danharrin authored Jun 14, 2024
2 parents ffa4abd + 582a2fc commit ee26b8f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
9 changes: 9 additions & 0 deletions packages/actions/docs/07-prebuilt-actions/08-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 16 additions & 7 deletions packages/actions/src/Concerns/CanImportRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,27 @@ protected function setUp(): void
$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()]), [
Expand Down
34 changes: 31 additions & 3 deletions packages/actions/src/Imports/ImportColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -111,7 +114,21 @@ public function label(string | Closure | null $label): static

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;
}
Expand Down Expand Up @@ -432,9 +449,20 @@ public function getImporter(): ?Importer
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));
}

/**
Expand Down

0 comments on commit ee26b8f

Please sign in to comment.