Skip to content

Commit

Permalink
- New stubs, create.stub and update.stub
Browse files Browse the repository at this point in the history
- Fix for checkboxes, automatically becomes array data
- **Breaking change:** Deleted mount() from form component
- **Breaking change:** renamed setup() to afterFormProperties()
- New LegacyForm trait to cover for breaking changes
- New Relationship field type support
- New Custom field type support
- New custom() field type support
- Updated make command to support multiple stub files
- New DefaultMount trait for slim components
  • Loading branch information
tanthammar committed Aug 4, 2020
1 parent 05d5d14 commit 75d9a6a
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 28 deletions.
11 changes: 8 additions & 3 deletions resources/stubs/component.stub
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ use Tanthammar\TallForms\FormComponent;

class DummyComponent extends FormComponent
{
public function setup() {
//validation?
public function mount(DummyModel $dummymodel) //or mount() in a create form
{
$this->mount_form($dummymodel); //or mount_form(null) in a create form
}

public function afterFormProperties() {
//Gate::authorize()
$this->fill([
'formTitle' => FormTitle,
'action' => 'Action', //create or update,
Expand All @@ -20,7 +25,7 @@ class DummyComponent extends FormComponent
]);
}

//needed if this is a create form
//remove this method, if this is an update form
public function create($form_data)
{
DummyModel::create($form_data);
Expand Down
37 changes: 37 additions & 0 deletions resources/stubs/create-component.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Livewire\Namespace;

use App\ModelsPathDummyModel;
use Tanthammar\TallForms\ArrayField;
use Tanthammar\TallForms\Field;
use Tanthammar\TallForms\FormComponent;
use Tanthammar\TallForms\Traits\DefaultMount;

class DummyComponent extends FormComponent
{
public function mount()
{
//Gate::authorize()
$this->fill([
'formTitle' => FormTitle,
'action' => 'create',
'spaMode' => true, //wrap with x-pages.default
'showGoBack' => false, //or true
]);
$this->mount_form();
}


public function create($form_data)
{
DummyModel::create($form_data);
}

public function fields()
{
return [
Field::make('Name')->input()->rules('required'),
];
}
}
31 changes: 31 additions & 0 deletions resources/stubs/update-component.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Livewire\Namespace;

use App\ModelsPathDummyModel;
use Tanthammar\TallForms\ArrayField;
use Tanthammar\TallForms\Field;
use Tanthammar\TallForms\FormComponent;

class DummyComponent extends FormComponent
{
public function mount(DummyModel $dummymodel)
{
//Gate::authorize()
$this->fill([
'formTitle' => FormTitle,
'action' => 'update',
'spaMode' => true, //wrap with x-pages.default
'showGoBack' => false, //or true
]);

$this->mount_form($dummymodel);
}

public function fields()
{
return [
Field::make('Name')->input()->rules('required'),
];
}
}
2 changes: 1 addition & 1 deletion resources/views/components/checkbox.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ class="form-checkbox mt-1 h-4 w-4 text-indigo-600 transition duration-150 ease-i
@if($html)<p class="help">{!! $html !!}</p>@endif
@enderror
</div>
</div>
</div>
5 changes: 2 additions & 3 deletions resources/views/fields/checkboxes.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<x-tall-field-wrapper :inline="$field->inline" :colspan="$field->colspan" :field="$field->name"
:label="$field->label" :labelW="$field->labelW" :fieldW="$field->fieldW">
@foreach($field->options as $value => $label)
<x-tall-checkbox :field="$field->key" id="{{$field->name}}.{{$loop->index}}" :label="$label"
:help="$field->help" :errorMsg="$field->errorMsg" :value="$value" />
<x-tall-checkbox field="{{$field->key}}" id="{{$field->name}}.{{$loop->index}}" :label="$label" :help="$field->help" :errorMsg="$field->errorMsg" :value="$value" />
@endforeach
</x-tall-field-wrapper>
</x-tall-field-wrapper>
8 changes: 7 additions & 1 deletion src/BaseField.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BaseField
protected $errorMsg;
protected $inline = true;
protected $is_relation = false;
protected $is_custom = false;

public function __get($property)
{
Expand Down Expand Up @@ -87,6 +88,12 @@ public function relation(): BaseField
return $this;
}

public function custom(): BaseField
{
$this->is_custom = true;
return $this;
}

public function default(string $default): BaseField
{
$this->default = $default;
Expand Down Expand Up @@ -122,7 +129,6 @@ public function help(string $help): BaseField
return $this;
}


/**
* Only applied to fields of type input
* @param string $prefix
Expand Down
9 changes: 5 additions & 4 deletions src/Commands/MakeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@

class MakeForm extends Command
{
protected $signature = 'make:tall-form {name} {--model=Model} {--path=Forms/} {--modelspath=Models\} {--action=create}';
protected $signature = 'make:tall-form {name} {--model=Model} {--path=Forms} {--modelspath=Models\} {--action=create}';
protected $description = 'Make a new Laravel Livewire form component.';

public function handle()
{
$stub = File::get(__DIR__ . '/../../resources/stubs/component.stub');
$stub = $this->option('action') == 'create' ? File::get(__DIR__ . '/../../resources/stubs/create-component.stub') : File::get(__DIR__ . '/../../resources/stubs/update-component.stub');
$stub = str_replace('FormTitle', config('tall-forms.form-title'), $stub);
$stub = str_replace('DummyComponent', $this->argument('name'), $stub);
$stub = str_replace('DummyModel', $this->option('model'), $stub);
if ($this->option('action') != 'create') $stub = str_replace('dummymodel', Str::lower($this->option('model')), $stub);
$stub = str_replace('ModelsPath', $this->option('modelspath'), $stub);
$stub = str_replace('Action', $this->option('action'), $stub);
$stub = str_replace('FormTitle', config('tall-forms.form-title'), $stub);
//$stub = str_replace('Action', $this->option('action'), $stub);
//$stub = str_replace('DummyRoute', Str::slug(Str::plural($this->option('model'))), $stub);
$stub = str_replace('Namespace', Str::of($this->option('path'))->replace('/', "\\"), $stub);
$path = app_path('Http/Livewire/'. $this->option('path') . "/" . $this->argument('name') . '.php');
Expand Down
56 changes: 40 additions & 16 deletions src/FormComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Tanthammar\TallForms;

use Illuminate\Routing\Redirector;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\URL;
use Tanthammar\TallForms\Traits\FollowsRules;
use Tanthammar\TallForms\Traits\HandlesArrays;
use Tanthammar\TallForms\Traits\Notify;
Expand All @@ -16,7 +17,7 @@ class FormComponent extends Component

public $model;
public $form_data;
public $action;
public $action = 'update';
public $formTitle;
public $showDelete = false;
public $showGoBack = true;
Expand All @@ -30,38 +31,42 @@ class FormComponent extends Component

protected $listeners = ['fileUpdate'];

public function mount($model = null, $action = 'update', $showDelete = false)
public function mount_form($model = null)
{
$this->model = $model;
$this->beforeFormProperties();
$this->setFormProperties($model);
$this->action = $action;
$this->showDelete = $showDelete;
$this->setup();
$this->setFormProperties($this->model);
$this->afterFormProperties();
$this->previous = \URL::previous(); //used for saveAndGoBack
}


public function beforeFormProperties()
{
return;
}


public function setFormProperties($model = null)
{
if ($model) $this->form_data = $model->toArray();
foreach ($this->fields() as $field) {
if (!isset($this->form_data[$field->name])) {
$array = in_array($field->type, ['checkbox', 'file']);
$array = in_array($field->type, ['checkboxes', 'file']);
$this->form_data[$field->name] = $field->default ?? ($array ? [] : null);
}
}
}

public function setup() {
$this->fill([
'formTitle' => 'create DummyModel',
'action' => 'update',
]);
public function afterFormProperties()
{
return;
}

//legacy method from v1
public function setup()
{
$this->formTitle = '';
}

public function render()
Expand Down Expand Up @@ -97,12 +102,26 @@ public function submit()

$field_names = [];
$relationship_names = [];
$custom_names = [];

foreach ($this->fields() as $field) {
($field->is_relation) ? $relation_names[] = $field->name : $field_names[] = $field->name;
if ($field->is_relation) {
$relationship_names[] = $field->name;
} elseif ($field->is_custom) {
$custom_names[] = $field->name;
} else {
$field_names[] = $field->name;
}
}

$relationship_data = Arr::only($this->form_data, $relationship_names);
$custom_data = Arr::only($this->form_data, $custom_names);
$this->form_data = Arr::only($this->form_data, $field_names);
$this->success();
filled($relationship_data = Arr::only($this->form_data, $relationship_names)) ? $this->relations($relationship_data) : null;

//make sure to create the model before attaching any relations
$this->success(); //creates or updates the model
if (filled($this->model)) $this->relations($relationship_data);
$this->custom_fields($custom_data);
}

public function errorMessage($message)
Expand All @@ -124,6 +143,11 @@ public function relations(array $relationship_data)
return;
}

public function custom_fields(array $custom_data)
{
return;
}

public function create($form_data)
{
return;
Expand Down
15 changes: 15 additions & 0 deletions src/Traits/DefaultMount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tanthammar\TallForms\Traits;

/**
* Optional default mount method if you want to keep your components slim
* @package Tanthammar\TallForms\Traits
*/
trait DefaultMount
{
public function mount($model = null)
{
$this->mount_form($model);
}
}
21 changes: 21 additions & 0 deletions src/Traits/LegacyMount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tanthammar\TallForms\Traits;

/**
* Mount method from v1
* @package Tanthammar\TallForms\Traits
*/
trait LegacyMount
{
public function mount($model = null, $action = 'update', $showDelete = false)
{
$this->model = $model;
$this->beforeFormProperties();
$this->setFormProperties($this->model);
$this->action = $action;
$this->showDelete = $showDelete;
$this->setup();
$this->previous = \URL::previous(); //used for saveAndGoBack
}
}

0 comments on commit 75d9a6a

Please sign in to comment.