Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
Default wire setting (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet authored Jan 5, 2022
1 parent b5de93e commit bf689eb
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `laravel-form-components` will be documented in this file

## 3.5.0 - 2022-01-05

- Added `default_wire` configuration
- Fix for overriding `wire:model` attribute

## 3.4.0 - 2022-01-04

- Added `tailwind-forms-simple` views
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class ContactForm extends Component
}
```

Normally you would use a `wire:model` attribute to bind a component property with a form element. By using the `@wire` directive, this package will automatically use the `wire:model` attribute instead of the `name` attribute.
Normally you would use a `wire:model` attribute to bind a component property with a form element. By using the `@wire` directive, this package will automatically add the `wire:model` attribute.

```blade
<x-form wire:submit.prevent="submit">
Expand All @@ -289,6 +289,8 @@ Additionally, you can pass an optional modifier to the `@wire` directive. This f
</x-form>
```

It's also possible to use the `wire:model` attribute by default. You may set the `default_wire` configuration setting to `true` or a modifier like `debounce.500ms`. This way, you don't need the `@wire` and `@endwire` directives in your views. You may still override the default setting by manually adding the `wire:model` attribute to a form element.

### Select elements

Besides the `name` attribute, the `select` element has a required `options` attribute, which should be a simple *key-value* array.
Expand Down
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

'use_eloquent_date_casting' => false,

/** bool | string */
'default_wire' => false,

'components' => [
'form' => [
'view' => 'form-components::{framework}.form',
Expand Down
4 changes: 4 additions & 0 deletions src/Components/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function render()
*/
public function isWired(): bool
{
if ($this->attributes && count($this->attributes->whereStartsWith('wire:model')->getIterator())) {
return false;
}

return app(FormDataBinder::class)->isWired();
}

Expand Down
15 changes: 15 additions & 0 deletions src/FormDataBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class FormDataBinder
*/
private $wire = false;

/**
* Whether the default wire has been verified once.
*/
private $verifiedDefaultWire = false;

/**
* Bind a target to the current instance
*
Expand Down Expand Up @@ -54,6 +59,16 @@ public function pop(): void
*/
public function isWired(): bool
{
if (!$this->verifiedDefaultWire) {
$this->verifiedDefaultWire = true;

$defaultWire = config('form-components.default_wire');

if ($defaultWire !== false) {
$this->wire = $defaultWire;
}
}

return $this->wire !== false;
}

Expand Down
69 changes: 69 additions & 0 deletions tests/Feature/LivewireTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public function render()
}
}

class FormComponentDefaultWired extends FormComponent
{
public function render()
{
return view('livewire-form-default-wired');
}
}

class FormComponentDefaultWiredModifier extends FormComponent
{
public function render()
{
return view('livewire-form-default-wired-lazy');
}
}

class FormComponentDefaultWiredWithOverride extends FormComponent
{
public function render()
{
return view('livewire-form-default-wired-with-override');
}
}

class LivewireTest extends TestCase
{
/** @test */
Expand Down Expand Up @@ -96,4 +120,49 @@ public function it_can_add_a_modifier_to_the_wire_directive()
->assertSeeHtml('The checkbox must be accepted')
->assertSeeHtml('The radio must be accepted');
}

/** @test */
public function it_can_wire_by_default()
{
config(['form-components.default_wire' => true]);

$component = Livewire::test(FormComponentDefaultWired::class);

$component->assertSeeHtml('wire:model="input"')
->assertSeeHtml('wire:model="textarea"')
->assertSeeHtml('wire:model="select"')
->assertSeeHtml('wire:model="multi_select"')
->assertSeeHtml('wire:model="checkbox"')
->assertSeeHtml('wire:model="radio"');
}

/** @test */
public function it_can_wire_by_default_with_a_modifier()
{
config(['form-components.default_wire' => 'debounce.1000ms']);

$component = Livewire::test(FormComponentDefaultWiredModifier::class);

$component->assertSeeHtml('wire:model.debounce.1000ms="input"')
->assertSeeHtml('wire:model.debounce.1000ms="textarea"')
->assertSeeHtml('wire:model.debounce.1000ms="select"')
->assertSeeHtml('wire:model.debounce.1000ms="multi_select"')
->assertSeeHtml('wire:model.debounce.1000ms="checkbox"')
->assertSeeHtml('wire:model.debounce.1000ms="radio"');
}

/** @test */
public function it_can_wire_by_default_but_still_override_the_value()
{
config(['form-components.default_wire' => true]);

$component = Livewire::test(FormComponentDefaultWiredWithOverride::class);

$component->assertSeeHtml('wire:model="input"')
->assertSeeHtml('wire:model="textarea"')
->assertDontSeeHtml('wire:model="select"')
->assertSeeHtml('wire:model.debounce.1000ms="multi_select"')
->assertSeeHtml('wire:model="checkbox"')
->assertSeeHtml('wire:model="radio"');
}
}
14 changes: 14 additions & 0 deletions tests/Feature/views/livewire-form-default-wired-lazy.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<x-form wire:submit.prevent="submit">
<x-form-input name="input" />
<x-form-textarea name="textarea" />
<x-form-select name="select" :options="['' => '', 'c' => 'c']" />
<x-form-select name="multi_select" multiple :options="['d' => 'd', 'e' => 'e', 'f' => 'f']" />

<x-form-checkbox name="checkbox" />

<x-form-group name="radio">
<x-form-radio name="radio" />
</x-form-group>

<x-form-submit />
</x-form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<x-form wire:submit.prevent="submit">
<x-form-input name="input" />
<x-form-textarea name="textarea" />
<x-form-select name="select" :options="['' => '', 'c' => 'c']" wire:model="" />
<x-form-select name="multi_select" multiple :options="['d' => 'd', 'e' => 'e', 'f' => 'f']" wire:model.debounce.1000ms="multi_select" />

<x-form-checkbox name="checkbox" />

<x-form-group name="radio">
<x-form-radio name="radio" />
</x-form-group>

<x-form-submit />
</x-form>
14 changes: 14 additions & 0 deletions tests/Feature/views/livewire-form-default-wired.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<x-form wire:submit.prevent="submit">
<x-form-input name="input" />
<x-form-textarea name="textarea" />
<x-form-select name="select" :options="['' => '', 'c' => 'c']" />
<x-form-select name="multi_select" multiple :options="['d' => 'd', 'e' => 'e', 'f' => 'f']" />

<x-form-checkbox name="checkbox" />

<x-form-group name="radio">
<x-form-radio name="radio" />
</x-form-group>

<x-form-submit />
</x-form>
2 changes: 1 addition & 1 deletion tests/Unit/FormDataBinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace ProtoneMedia\LaravelFormComponents\Tests\Unit;

use PHPUnit\Framework\TestCase;
use ProtoneMedia\LaravelFormComponents\FormDataBinder;
use ProtoneMedia\LaravelFormComponents\Tests\TestCase;

class FormDataBinderTest extends TestCase
{
Expand Down

0 comments on commit bf689eb

Please sign in to comment.