Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit e6a94d2

Browse files
committed
Add x-model support to inputs
1 parent d1f0317 commit e6a94d2

File tree

13 files changed

+81
-17
lines changed

13 files changed

+81
-17
lines changed

resources/views/components/choice/checkbox-or-radio.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@if ($id) id="{{ $id }}" @endif
66
type="{{ $type }}"
77
@if ($value) value="{{ $value }}" @endif
8-
@if ($checked && ! $attributes->hasStartsWith('wire:model')) checked @endif
8+
@if ($checked && ! $hasBoundModel()) checked @endif
99
{{ $extraAttributes }}
1010
{!! $ariaDescribedBy() !!}
1111
@if ($hasErrorsAndShow($name))

resources/views/components/choice/switch-toggle.blade.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<div x-data="{
22
onValue: {{ json_encode($onValue) }},
33
offValue: {{ json_encode($offValue) }},
4-
@if ($attributes->hasStartsWith('wire:model'))
4+
@if ($hasWireModel())
55
value: @entangle($attributes->wire('model')),
6+
@elseif ($hasXModel())
7+
value: {{ $attributes->first('x-model') }},
68
@else
79
value: {{ json_encode($value) }},
810
@endif
@@ -15,6 +17,9 @@
1517
}"
1618
wire:ignore.self
1719
class="{{ $getContainerClass() }}"
20+
@if ($hasXModel())
21+
x-init="$watch(value, newValue => { {{ $attributes->first('x-model') }} = newValue })"
22+
@endif
1823
{{ $extraAttributes }}
1924
>
2025
@if ($label && $labelPosition === 'left')
@@ -33,7 +38,7 @@ class="flex-grow switch-toggle-label form-label block text-sm font-medium leadin
3338
type="button"
3439
@if ($id) id="{{ $id }}" @endif
3540
@if ($label) aria-labelledby="{{ $labelId() }}" @endif
36-
{{ $attributes->except(['type', 'wire:model', 'wire:model.defer', 'wire:model.lazy'])->merge(['class' => $buttonClass()]) }}
41+
{{ $attributes->except(['type', 'wire:model', 'wire:model.defer', 'wire:model.lazy', 'x-model'])->merge(['class' => $buttonClass()]) }}
3742
x-bind:class="{ 'pressed': isPressed }"
3843
@if ($disabled) disabled @endif
3944
>

resources/views/components/inputs/custom-select.blade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<div x-data="customSelect({
22
{{ $configToJson() }}
3-
@if ($attributes->hasStartsWith('wire:model'))
3+
@if ($hasWireModel())
44
value: @entangle($attributes->wire('model')),
55
_wire: $wire,
6+
@elseif ($hasXModel())
7+
value: {{ $attributes->first('x-model') }},
68
@else
79
value: {{ $selectedKeyToJS() }},
810
@endif

resources/views/components/inputs/date-picker.blade.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
11
<div x-data="{
22
fp: null,
3-
@if ($value && ! $attributes->hasStartsWith('wire:model'))
4-
value: '{{ $value }}',
5-
@elseif ($attributes->hasStartsWith('wire:model'))
3+
@if ($hasWireModel())
64
value: @entangle($attributes->wire('model')),
5+
@elseif ($hasXModel())
6+
value: {{ $attributes->first('x-model') }},
77
@else
8-
value: null,
8+
value: {{ $value ? "'{$value}'" : 'null' }},
99
@endif
10+
updateValue(newValue) {
11+
this.value = newValue;
12+
$dispatch('input', newValue);
13+
14+
try {
15+
this.fp.setDate(newValue);
16+
} catch {}
17+
18+
@if ($hasXModel())
19+
{{ $attributes->first('x-model') }} = newValue;
20+
@endif
21+
},
1022
}"
1123
x-init="fp = flatpickr($refs.input, {
1224
defaultDate: value,
1325
onOpen: [
1426
function (selectedDates, dateStr, instance) {
1527
instance.setDate(value);
1628
},
29+
{{ $onOpen ?? '' }}
1730
],
1831
{{ $jsonOptions() }}
1932
{{ $optionsSlot ?? '' }}
2033
})"
21-
x-on:change="value = $event.target.value; fp.setDate(value)"
34+
x-on:change="updateValue($event.target.value)"
2235
class="{{ $getContainerClass() }}"
2336
{{ $extraAttributes }}
2437
>
@@ -43,10 +56,14 @@ class="leading-addon cursor-pointer inline-flex items-center px-3 rounded-l-md b
4356
@if ($name) name="{{ $name }}" @endif
4457
@if ($id) id="{{ $id }}" @endif
4558
x-ref="input"
46-
x-bind:value="value"
59+
60+
@unless ($hasXModel())
61+
x-bind:value="value"
62+
@endunless
63+
4764
placeholder="{{ $placeholder }}"
4865

49-
@if ($value && ! $attributes->hasStartsWith('wire:model')) value="{{ $value }}" @endif
66+
@if ($value && ! $hasBoundModel()) value="{{ $value }}" @endif
5067

5168
{!! $ariaDescribedBy() !!}
5269
@if ($hasErrorsAndShow($name))
@@ -59,7 +76,7 @@ class="leading-addon cursor-pointer inline-flex items-center px-3 rounded-l-md b
5976
<button x-show="Boolean(value)"
6077
x-transition
6178
x-cloak
62-
x-on:click="value = null; fp.setDate(value)"
79+
x-on:click="updateValue(null)"
6380
class="form-input-clear h-6 w-6 group rounded-full p-1 hover:bg-blue-gray-200 focus:outline-blue-gray transition-colors"
6481
type="button"
6582
>

resources/views/components/inputs/input.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@if ($id) id="{{ $id }}" @endif
1111
type="{{ $type }}"
1212

13-
@if (! is_null($value) && ! $attributes->hasStartsWith('wire:model')) value="{{ $value }}" @endif
13+
@if (! is_null($value) && ! $hasBoundModel()) value="{{ $value }}" @endif
1414

1515
@if ($hasErrorsAndShow($name))
1616
aria-invalid="true"

resources/views/components/inputs/password.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class="{{ $containerClass() }}"
1919
type="password"
2020
@endif
2121

22-
@if ($value && ! $attributes->hasStartsWith('wire:model')) value="{{ $value }}" @endif
22+
@if ($value && ! $hasBoundModel()) value="{{ $value }}" @endif
2323

2424
@if ($hasErrorsAndShow($name))
2525
aria-invalid="true"

resources/views/components/inputs/textarea.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@endif
1111

1212
{!! $attributes->merge(['class' => $inputClass(), 'rows' => 3]) !!}
13-
>@if (! is_null($value) && ! $attributes->hasStartsWith('wire:model')){!! $value !!}@elseif ($slot->isNotEmpty()){!! $slot !!}@endif</textarea>
13+
>@if (! is_null($value) && ! $hasBoundModel()){!! $value !!}@elseif ($slot->isNotEmpty()){!! $slot !!}@endif</textarea>
1414

1515
@include('form-components::partials.trailing-addons')
1616
</div>

src/Components/Choice/Checkbox.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Rawilk\FormComponents\Components\BladeComponent;
66
use Rawilk\FormComponents\Concerns\HandlesValidationErrors;
7+
use Rawilk\FormComponents\Concerns\HasModels;
78

89
class Checkbox extends BladeComponent
910
{
1011
use HandlesValidationErrors;
12+
use HasModels;
1113

1214
public string $type = 'checkbox';
1315

src/Components/Choice/SwitchToggle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
use Illuminate\Support\Str;
99
use Rawilk\FormComponents\Components\BladeComponent;
1010
use Rawilk\FormComponents\Concerns\HandlesValidationErrors;
11+
use Rawilk\FormComponents\Concerns\HasModels;
1112

1213
class SwitchToggle extends BladeComponent
1314
{
1415
use HandlesValidationErrors;
16+
use HasModels;
1517

1618
protected static array $assets = ['alpine'];
1719

src/Components/Files/FileUpload.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use Rawilk\FormComponents\Components\BladeComponent;
88
use Rawilk\FormComponents\Concerns\AcceptsFiles;
99
use Rawilk\FormComponents\Concerns\HandlesValidationErrors;
10+
use Rawilk\FormComponents\Concerns\HasModels;
1011

1112
class FileUpload extends BladeComponent
1213
{
1314
use HandlesValidationErrors;
1415
use AcceptsFiles;
16+
use HasModels;
1517

1618
protected static array $assets = ['alpine'];
1719

@@ -47,7 +49,7 @@ public function canShowUploadProgress($attributes): bool
4749
return $this->canShowUploadProgress = false;
4850
}
4951

50-
if (! $attributes->hasStartsWith('wire:model')) {
52+
if (! $this->hasWireModel()) {
5153
return $this->canShowUploadProgress = false;
5254
}
5355

src/Components/Inputs/Input.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
use Rawilk\FormComponents\Components\BladeComponent;
99
use Rawilk\FormComponents\Concerns\HandlesValidationErrors;
1010
use Rawilk\FormComponents\Concerns\HasAddons;
11+
use Rawilk\FormComponents\Concerns\HasModels;
1112

1213
class Input extends BladeComponent
1314
{
1415
use HandlesValidationErrors;
1516
use HasAddons;
17+
use HasModels;
1618

1719
/*
1820
* Normally we want arrays to be encoded, but some components don't need that, like CustomSelect.

src/Concerns/HasModels.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rawilk\FormComponents\Concerns;
4+
5+
trait HasModels
6+
{
7+
protected $hasWireModel;
8+
protected $hasXModel;
9+
10+
public function hasBoundModel(): bool
11+
{
12+
return $this->hasWireModel() || $this->hasXModel();
13+
}
14+
15+
public function hasWireModel(): bool
16+
{
17+
if ($this->hasWireModel !== null) {
18+
return $this->hasWireModel;
19+
}
20+
21+
return $this->hasWireModel = $this->attributes->hasStartsWith('wire:model');
22+
}
23+
24+
public function hasXModel(): bool
25+
{
26+
if ($this->hasXModel !== null) {
27+
return $this->hasXModel;
28+
}
29+
30+
return $this->hasXModel = $this->attributes->hasStartsWith('x-model');
31+
}
32+
}

tests/Components/Choice/__snapshots__/SwitchToggleTest__custom_attributes_are_applied_to_the_button__1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}"
1212
wire:ignore.self
1313
class="flex items-center "
14-
14+
1515
>
1616

1717
<button x-bind:aria-pressed="JSON.stringify(isPressed)"

0 commit comments

Comments
 (0)