Skip to content

Commit

Permalink
Add callbacks to interactions to allow customizing of the fields
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbrouwers committed Sep 8, 2018
1 parent d5b8886 commit 3053d95
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
11 changes: 0 additions & 11 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,4 @@
use Illuminate\Support\Facades\Route;
use Maatwebsite\LaravelNovaExcel\Http\Controllers\ExcelController;

/*
|--------------------------------------------------------------------------
| Tool API Routes
|--------------------------------------------------------------------------
|
| Here is where you may register API routes for your tool. These routes
| are loaded by the ServiceProvider of your tool. They are protected
| by your tool's "Authorize" middleware by default. Now, go build!
|
*/

Route::get('download', ExcelController::class . '@download');
14 changes: 11 additions & 3 deletions src/Interactions/AskForFilename.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ trait AskForFilename
/**
* Ask the user for a filename.
*
* @param string $label Input label
* @param string $label Input label
* @param callable|null $callback
*
* @return $this
*/
public function askForFilename(string $label = null)
public function askForFilename(string $label = null, callable $callback = null)
{
$this->actionFields[] = Text::make($label ?? __('Filename'), 'filename');
$field = Text::make($label ?? __('Filename'), 'filename');

if (is_callable($callback)) {
$callback($field);
}

$this->actionFields[] = $field;

return $this;
}
Expand Down
15 changes: 11 additions & 4 deletions src/Interactions/AskForWriterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ trait AskForWriterType
/**
* Ask the user for a filename.
*
* @param array|null $options
* @param string|null $label
* @param array|null $options
* @param string|null $label
* @param callable|null $callback
*
* @return $this
*/
public function askForWriterType(array $options = null, string $label = null)
public function askForWriterType(array $options = null, string $label = null, callable $callback = null)
{
$options = $options ?: [
Excel::XLS => 'XLS',
Excel::XLSX => 'XLSX',
Excel::CSV => 'CSV',
];

$this->actionFields[] = Select::make(__($label ?: 'Type'), 'writer_type')->options($options);
$field = Select::make(__($label ?: 'Type'), 'writer_type')->options($options);

if (is_callable($callback)) {
$callback($field);
}

$this->actionFields[] = $field;

return $this;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Requests/ExportActionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maatwebsite\LaravelNovaExcel\Requests;

use Illuminate\Support\Collection;
use Laravel\Nova\Lenses\Lens;
use Laravel\Nova\Http\Requests\ActionRequest;
use Laravel\Nova\Http\Requests\LensActionRequest;
Expand All @@ -25,7 +26,7 @@ public function toExportQuery(bool $onlyIndexFields = false, array $only = [])
$query->whereKey(explode(',', $this->resources));
})->when($onlyIndexFields, function ($query) {
$query->select($this->indexFields());
})->when(\count($only) > 0, function ($query) use ($only) {
})->when(count($only) > 0, function ($query) use ($only) {
$query->select($only);
});
}
Expand Down Expand Up @@ -60,7 +61,7 @@ protected function setLens(Lens $lens)
protected function indexFields(): array
{
$fields = $this->lens
? collect($this->lens->fields($this))
? new Collection($this->lens->fields($this))
: $this->newResource()->indexFields($this);

return $fields->map->attribute->unique()->all();
Expand Down

0 comments on commit 3053d95

Please sign in to comment.