Skip to content

Commit 2068b86

Browse files
Add User prompt for missing inputs (rappasoft#1681)
* PromptsForMissingInput added for Input field * Custom Prompt added * PossibleModels function added * Renamed to setArgument * Model and Name prompted * ModelPath prompted when model not found * New functions moved in bottom * Placeholders added * spacing fix * pint formatting fix * Docs updated in start/commands
1 parent a3e6dcb commit 2068b86

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
55
## [v3.2.5] - UNRELEASED
66
### New Features
77
- Add setConfigurableArea by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1706
8+
- Add User prompt for missing inputs by @achyutkneupane in https://github.com/rappasoft/laravel-livewire-tables/pull/1681
89

910
### Bug Fixes
1011
- UI patch: toolbar fix for reordering by @itsLeonB in https://github.com/rappasoft/laravel-livewire-tables/pull/1690

docs/start/commands.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ Create a new datatable component called `TestTable` in `App\Livewire` that uses
2222
```bash
2323
php artisan make:datatable TestTable example app/Domains/Test/Models/
2424
```
25+
26+
### Prompt for missing inputs
27+
28+
If you only use the `make:datatable` command without any arguments, it will prompt you for the missing inputs.
29+
30+
```bash
31+
php artisan make:datatable
32+
```

src/Commands/MakeCommand.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
namespace Rappasoft\LaravelLivewireTables\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Contracts\Console\PromptsForMissingInput;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Support\Facades\File;
89
use Illuminate\Support\Str;
910
use Livewire\Features\SupportConsoleCommands\Commands\ComponentParser;
1011
use Livewire\Features\SupportConsoleCommands\Commands\MakeCommand as LivewireMakeCommand;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
use Symfony\Component\Finder\Finder;
15+
16+
use function Laravel\Prompts\suggest;
17+
use function Laravel\Prompts\text;
1118

1219
/**
1320
* Class MakeCommand
1421
*/
15-
class MakeCommand extends Command
22+
class MakeCommand extends Command implements PromptsForMissingInput
1623
{
1724
protected ComponentParser $parser;
1825

@@ -164,4 +171,52 @@ private function generateColumns(string $modelName): string
164171

165172
return $columns;
166173
}
174+
175+
protected function possibleModels()
176+
{
177+
$modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path();
178+
179+
return collect(Finder::create()->files()->depth(0)->in($modelPath))
180+
->map(fn ($file) => $file->getBasename('.php'))
181+
->sort()
182+
->values()
183+
->all();
184+
}
185+
186+
protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
187+
{
188+
189+
if ($this->didReceiveOptions($input)) {
190+
return;
191+
}
192+
193+
if (trim($this->argument('name')) === '') {
194+
$name = text('What is the name of your Livewire class?', 'TestTable');
195+
196+
if ($name) {
197+
$input->setArgument('name', $name);
198+
}
199+
}
200+
201+
if (trim($this->argument('model')) === '') {
202+
$model = suggest(
203+
'What is the name of the model you want to use in this table?',
204+
$this->possibleModels(),
205+
'Test'
206+
);
207+
208+
if ($model) {
209+
$input->setArgument('model', $model);
210+
}
211+
}
212+
213+
if (trim($this->argument('modelpath')) === '' && ! in_array($this->argument('model'), $this->possibleModels())) {
214+
215+
$modelPath = text('What is the path to the model you want to use in this table?', 'app/TestModels/');
216+
217+
if ($modelPath) {
218+
$input->setArgument('modelpath', $modelPath);
219+
}
220+
}
221+
}
167222
}

0 commit comments

Comments
 (0)