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

Commit bf689eb

Browse files
authored
Default wire setting (#76)
1 parent b5de93e commit bf689eb

10 files changed

+142
-2
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

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

5+
## 3.5.0 - 2022-01-05
6+
7+
- Added `default_wire` configuration
8+
- Fix for overriding `wire:model` attribute
9+
510
## 3.4.0 - 2022-01-04
611

712
- Added `tailwind-forms-simple` views

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class ContactForm extends Component
266266
}
267267
```
268268

269-
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.
269+
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.
270270

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

292+
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.
293+
292294
### Select elements
293295

294296
Besides the `name` attribute, the `select` element has a required `options` attribute, which should be a simple *key-value* array.

config/config.php

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
'use_eloquent_date_casting' => false,
1212

13+
/** bool | string */
14+
'default_wire' => false,
15+
1316
'components' => [
1417
'form' => [
1518
'view' => 'form-components::{framework}.form',

src/Components/Component.php

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function render()
3636
*/
3737
public function isWired(): bool
3838
{
39+
if ($this->attributes && count($this->attributes->whereStartsWith('wire:model')->getIterator())) {
40+
return false;
41+
}
42+
3943
return app(FormDataBinder::class)->isWired();
4044
}
4145

src/FormDataBinder.php

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class FormDataBinder
1616
*/
1717
private $wire = false;
1818

19+
/**
20+
* Whether the default wire has been verified once.
21+
*/
22+
private $verifiedDefaultWire = false;
23+
1924
/**
2025
* Bind a target to the current instance
2126
*
@@ -54,6 +59,16 @@ public function pop(): void
5459
*/
5560
public function isWired(): bool
5661
{
62+
if (!$this->verifiedDefaultWire) {
63+
$this->verifiedDefaultWire = true;
64+
65+
$defaultWire = config('form-components.default_wire');
66+
67+
if ($defaultWire !== false) {
68+
$this->wire = $defaultWire;
69+
}
70+
}
71+
5772
return $this->wire !== false;
5873
}
5974

tests/Feature/LivewireTest.php

+69
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ public function render()
4141
}
4242
}
4343

44+
class FormComponentDefaultWired extends FormComponent
45+
{
46+
public function render()
47+
{
48+
return view('livewire-form-default-wired');
49+
}
50+
}
51+
52+
class FormComponentDefaultWiredModifier extends FormComponent
53+
{
54+
public function render()
55+
{
56+
return view('livewire-form-default-wired-lazy');
57+
}
58+
}
59+
60+
class FormComponentDefaultWiredWithOverride extends FormComponent
61+
{
62+
public function render()
63+
{
64+
return view('livewire-form-default-wired-with-override');
65+
}
66+
}
67+
4468
class LivewireTest extends TestCase
4569
{
4670
/** @test */
@@ -96,4 +120,49 @@ public function it_can_add_a_modifier_to_the_wire_directive()
96120
->assertSeeHtml('The checkbox must be accepted')
97121
->assertSeeHtml('The radio must be accepted');
98122
}
123+
124+
/** @test */
125+
public function it_can_wire_by_default()
126+
{
127+
config(['form-components.default_wire' => true]);
128+
129+
$component = Livewire::test(FormComponentDefaultWired::class);
130+
131+
$component->assertSeeHtml('wire:model="input"')
132+
->assertSeeHtml('wire:model="textarea"')
133+
->assertSeeHtml('wire:model="select"')
134+
->assertSeeHtml('wire:model="multi_select"')
135+
->assertSeeHtml('wire:model="checkbox"')
136+
->assertSeeHtml('wire:model="radio"');
137+
}
138+
139+
/** @test */
140+
public function it_can_wire_by_default_with_a_modifier()
141+
{
142+
config(['form-components.default_wire' => 'debounce.1000ms']);
143+
144+
$component = Livewire::test(FormComponentDefaultWiredModifier::class);
145+
146+
$component->assertSeeHtml('wire:model.debounce.1000ms="input"')
147+
->assertSeeHtml('wire:model.debounce.1000ms="textarea"')
148+
->assertSeeHtml('wire:model.debounce.1000ms="select"')
149+
->assertSeeHtml('wire:model.debounce.1000ms="multi_select"')
150+
->assertSeeHtml('wire:model.debounce.1000ms="checkbox"')
151+
->assertSeeHtml('wire:model.debounce.1000ms="radio"');
152+
}
153+
154+
/** @test */
155+
public function it_can_wire_by_default_but_still_override_the_value()
156+
{
157+
config(['form-components.default_wire' => true]);
158+
159+
$component = Livewire::test(FormComponentDefaultWiredWithOverride::class);
160+
161+
$component->assertSeeHtml('wire:model="input"')
162+
->assertSeeHtml('wire:model="textarea"')
163+
->assertDontSeeHtml('wire:model="select"')
164+
->assertSeeHtml('wire:model.debounce.1000ms="multi_select"')
165+
->assertSeeHtml('wire:model="checkbox"')
166+
->assertSeeHtml('wire:model="radio"');
167+
}
99168
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<x-form wire:submit.prevent="submit">
2+
<x-form-input name="input" />
3+
<x-form-textarea name="textarea" />
4+
<x-form-select name="select" :options="['' => '', 'c' => 'c']" />
5+
<x-form-select name="multi_select" multiple :options="['d' => 'd', 'e' => 'e', 'f' => 'f']" />
6+
7+
<x-form-checkbox name="checkbox" />
8+
9+
<x-form-group name="radio">
10+
<x-form-radio name="radio" />
11+
</x-form-group>
12+
13+
<x-form-submit />
14+
</x-form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<x-form wire:submit.prevent="submit">
2+
<x-form-input name="input" />
3+
<x-form-textarea name="textarea" />
4+
<x-form-select name="select" :options="['' => '', 'c' => 'c']" wire:model="" />
5+
<x-form-select name="multi_select" multiple :options="['d' => 'd', 'e' => 'e', 'f' => 'f']" wire:model.debounce.1000ms="multi_select" />
6+
7+
<x-form-checkbox name="checkbox" />
8+
9+
<x-form-group name="radio">
10+
<x-form-radio name="radio" />
11+
</x-form-group>
12+
13+
<x-form-submit />
14+
</x-form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<x-form wire:submit.prevent="submit">
2+
<x-form-input name="input" />
3+
<x-form-textarea name="textarea" />
4+
<x-form-select name="select" :options="['' => '', 'c' => 'c']" />
5+
<x-form-select name="multi_select" multiple :options="['d' => 'd', 'e' => 'e', 'f' => 'f']" />
6+
7+
<x-form-checkbox name="checkbox" />
8+
9+
<x-form-group name="radio">
10+
<x-form-radio name="radio" />
11+
</x-form-group>
12+
13+
<x-form-submit />
14+
</x-form>

tests/Unit/FormDataBinderTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace ProtoneMedia\LaravelFormComponents\Tests\Unit;
44

5-
use PHPUnit\Framework\TestCase;
65
use ProtoneMedia\LaravelFormComponents\FormDataBinder;
6+
use ProtoneMedia\LaravelFormComponents\Tests\TestCase;
77

88
class FormDataBinderTest extends TestCase
99
{

0 commit comments

Comments
 (0)