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

Commit dac240b

Browse files
committed
Livewire fix
1 parent 4102b06 commit dac240b

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ Additionally, you can pass an optional modifier to the `@wire` directive. This f
291291
</x-form>
292292
```
293293

294-
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.
294+
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 using the `@wire` directive, or by manually adding the `wire:model` attribute to a form element.
295295

296296
### Select elements
297297

src/FormDataBinder.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class FormDataBinder
1919
/**
2020
* Whether the default wire has been verified once.
2121
*/
22-
private $verifiedDefaultWire = false;
22+
private $loadDefaultWire = true;
2323

2424
/**
2525
* Bind a target to the current instance
@@ -59,8 +59,8 @@ public function pop(): void
5959
*/
6060
public function isWired(): bool
6161
{
62-
if (!$this->verifiedDefaultWire) {
63-
$this->verifiedDefaultWire = true;
62+
if ($this->loadDefaultWire) {
63+
$this->loadDefaultWire = false;
6464

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

@@ -85,12 +85,16 @@ public function getWireModifier(): ?string
8585
/**
8686
* Enable Livewire binding with an optional modifier.
8787
*
88-
* @param string $modifier
88+
* @param bool|string $modifier
8989
* @return void
9090
*/
9191
public function wire($modifier = null): void
9292
{
93-
$this->wire = $modifier ?: null;
93+
$this->wire = $modifier !== false
94+
? ($modifier ?: null)
95+
: false;
96+
97+
$this->loadDefaultWire = false;
9498
}
9599

96100
/**
@@ -101,5 +105,7 @@ public function wire($modifier = null): void
101105
public function endWire(): void
102106
{
103107
$this->wire = false;
108+
109+
$this->loadDefaultWire = true;
104110
}
105111
}

tests/Feature/LivewireTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ public function render()
6565
}
6666
}
6767

68+
class FormComponentDefaultWiredWithDirectiveOverride extends FormComponent
69+
{
70+
public function render()
71+
{
72+
return view('livewire-form-default-wired-with-directive-override');
73+
}
74+
}
75+
6876
class LivewireTest extends TestCase
6977
{
7078
/** @test */
@@ -165,4 +173,19 @@ public function it_can_wire_by_default_but_still_override_the_value()
165173
->assertSeeHtml('wire:model="checkbox"')
166174
->assertSeeHtml('wire:model="radio"');
167175
}
176+
177+
/** @test */
178+
public function it_can_wire_by_default_but_still_override_with_a_directive()
179+
{
180+
config(['form-components.default_wire' => true]);
181+
182+
$component = Livewire::test(FormComponentDefaultWiredWithDirectiveOverride::class);
183+
184+
$component->assertSeeHtml('wire:model="input"')
185+
->assertDontSeeHtml('wire:model="textarea"')
186+
->assertSeeHtml('wire:model="select"')
187+
->assertSeeHtml('wire:model.defer="multi_select"')
188+
->assertSeeHtml('wire:model.debounce.500ms="checkbox"')
189+
->assertSeeHtml('wire:model="radio"');
190+
}
168191
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<x-form wire:submit.prevent="submit">
2+
<x-form-input name="input" />
3+
4+
@wire(false)
5+
<x-form-textarea name="textarea" />
6+
@endwire
7+
8+
<x-form-select name="select" :options="['' => '', 'c' => 'c']" />
9+
10+
@wire('defer')
11+
<x-form-select name="multi_select" multiple :options="['d' => 'd', 'e' => 'e', 'f' => 'f']" />
12+
@endwire
13+
14+
<x-form-checkbox name="checkbox" wire:model.debounce.500ms="checkbox" />
15+
16+
<x-form-group name="radio">
17+
<x-form-radio name="radio" />
18+
</x-form-group>
19+
20+
<x-form-submit />
21+
</x-form>

0 commit comments

Comments
 (0)