Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Jun 14, 2024
1 parent 6a8ed61 commit 582a2fc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/actions/docs/07-prebuilt-actions/08-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ ImportColumn::make('sku')
->example('ABC123')
```

Or if you want to add more than one example row, you can pass array to `examples()` method:
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;
Expand Down
24 changes: 13 additions & 11 deletions packages/actions/src/Concerns/CanImportRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,24 +334,26 @@ protected function setUp(): void
$columns,
));

$rowsCount = array_reduce(
$columnExamples = array_map(
fn (ImportColumn $column): array => $column->getExamples(),
$columns,
function (?int $maxCount, ImportColumn $column) {
return max($maxCount, count($column->getExamples()));
},
0
);

$examples = [];
foreach ($columns as $column) {
$columnExamples = $column->getExamples();
$exampleRowsCount = array_reduce(
$columnExamples,
fn (int $count, array $exampleData): int => max($count, count($exampleData)),
initial: 0,
);

$exampleRows = [];

for($i = 0; $i < $rowsCount; $i++) {
$examples[$i][] = $columnExamples[$i] ?? '';
foreach ($columnExamples as $exampleData) {
for ($i = 0; $i < $exampleRowsCount; $i++) {
$exampleRows[$i][] = $exampleData[$i] ?? '';
}
}

$csv->insertAll($examples);
$csv->insertAll($exampleRows);

return response()->streamDownload(function () use ($csv) {
echo $csv->toString();
Expand Down
8 changes: 4 additions & 4 deletions packages/actions/src/Imports/ImportColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ public function example(mixed $example): static
return $this;
}

/**
* @param mixed $examples
*/
public function examples(mixed $examples): static
{
if (!is_array($examples) && !$examples instanceof Closure) {
if (
(! is_array($examples)) &&
(! $examples instanceof Closure)
) {
$examples = Arr::wrap($examples);
}

Expand Down

0 comments on commit 582a2fc

Please sign in to comment.