From 7178ae0cbbc649ac63366ff934c80f49d734ab92 Mon Sep 17 00:00:00 2001 From: Dominique Heinelt Date: Thu, 14 Mar 2024 18:07:50 +0100 Subject: [PATCH 1/5] Started implementing support for disabling form inputs --- src/Livewire/FormView.php | 4 ++ src/Services/FormBuilder/Checkbox.php | 2 + src/Services/FormBuilder/Code.php | 2 + src/Services/FormBuilder/Color.php | 2 + src/Services/FormBuilder/Date.php | 2 + src/Services/FormBuilder/DateTime.php | 2 + src/Services/FormBuilder/Decimal.php | 2 + src/Services/FormBuilder/File.php | 2 + src/Services/FormBuilder/FormInput.php | 10 ++++- src/Services/FormBuilder/Number.php | 2 + src/Services/FormBuilder/Password.php | 2 + src/Services/FormBuilder/Radio.php | 2 + src/Services/FormBuilder/RichTextarea.php | 2 + src/Services/FormBuilder/Select.php | 2 + src/Services/FormBuilder/Text.php | 2 + src/Services/FormBuilder/Textarea.php | 2 + src/Services/FormBuilder/Time.php | 2 + .../FormBuilder/Traits/SupportsDisabling.php | 42 +++++++++++++++++++ .../bootstrap/components/input.blade.php | 1 + .../components/types/checkbox.blade.php | 1 + .../components/types/datetime.blade.php | 2 + .../components/types/decimal.blade.php | 1 + .../bootstrap/components/types/file.blade.php | 1 + .../components/types/radio.blade.php | 1 + .../components/types/select.blade.php | 1 + .../components/types/textarea.blade.php | 1 + tests/Unit/InputTests/DecimalInputTest.php | 6 +++ 27 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/Services/FormBuilder/Traits/SupportsDisabling.php diff --git a/src/Livewire/FormView.php b/src/Livewire/FormView.php index 66760a6..2a4aa72 100644 --- a/src/Livewire/FormView.php +++ b/src/Livewire/FormView.php @@ -21,6 +21,7 @@ use Nodus\Packages\LivewireForms\Services\FormBuilder\FormInput; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsArrayValidations; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsValidations; use Throwable; @@ -814,6 +815,9 @@ protected function prepareInputs(): void $inputTraits = class_uses($input); + // TODO + // - extra addRuleForInput($input) method? + // - check if input supports disabling, is disabled and only add rules if not if (in_array(SupportsValidations::class, $inputTraits)) { /** @var SupportsValidations|FormInput $input */ $this->rules[$input->getViewId()] = $input->rewriteValidationRules($model); diff --git a/src/Services/FormBuilder/Checkbox.php b/src/Services/FormBuilder/Checkbox.php index 84c81cc..ebf6f93 100644 --- a/src/Services/FormBuilder/Checkbox.php +++ b/src/Services/FormBuilder/Checkbox.php @@ -3,6 +3,7 @@ namespace Nodus\Packages\LivewireForms\Services\FormBuilder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsLabelPosition; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize; @@ -20,6 +21,7 @@ class Checkbox extends FormInput use SupportsValidations; use SupportsHint; use SupportsLabelPosition; + use SupportsDisabling; /** * Checkbox constructor diff --git a/src/Services/FormBuilder/Code.php b/src/Services/FormBuilder/Code.php index dd653ba..7e42ab2 100644 --- a/src/Services/FormBuilder/Code.php +++ b/src/Services/FormBuilder/Code.php @@ -3,6 +3,7 @@ namespace Nodus\Packages\LivewireForms\Services\FormBuilder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsValidations; @@ -18,6 +19,7 @@ class Code extends FormInput use SupportsValidations; use SupportsSize; use SupportsHint; + use SupportsDisabling; /** * Syntax Highlight mode diff --git a/src/Services/FormBuilder/Color.php b/src/Services/FormBuilder/Color.php index f4badb2..7f5a040 100644 --- a/src/Services/FormBuilder/Color.php +++ b/src/Services/FormBuilder/Color.php @@ -3,6 +3,7 @@ namespace Nodus\Packages\LivewireForms\Services\FormBuilder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsValidations; @@ -18,4 +19,5 @@ class Color extends FormInput use SupportsValidations; use SupportsSize; use SupportsHint; + use SupportsDisabling; } diff --git a/src/Services/FormBuilder/Date.php b/src/Services/FormBuilder/Date.php index cc93be9..a4bc9b4 100644 --- a/src/Services/FormBuilder/Date.php +++ b/src/Services/FormBuilder/Date.php @@ -4,6 +4,7 @@ use Illuminate\Support\Carbon; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsMinMax; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder; @@ -24,6 +25,7 @@ class Date extends FormInput use SupportsHint; use SupportsPlaceholder; use SupportsMinMax; + use SupportsDisabling; /** * Date constructor. diff --git a/src/Services/FormBuilder/DateTime.php b/src/Services/FormBuilder/DateTime.php index c395c5e..0949791 100644 --- a/src/Services/FormBuilder/DateTime.php +++ b/src/Services/FormBuilder/DateTime.php @@ -4,6 +4,7 @@ use Illuminate\Support\Carbon; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsValidations; @@ -19,6 +20,7 @@ class DateTime extends FormInput use SupportsValidations; use SupportsSize; use SupportsHint; + use SupportsDisabling; /** * Pre validation mutator handler diff --git a/src/Services/FormBuilder/Decimal.php b/src/Services/FormBuilder/Decimal.php index e6cfc3e..57e770e 100644 --- a/src/Services/FormBuilder/Decimal.php +++ b/src/Services/FormBuilder/Decimal.php @@ -5,6 +5,7 @@ use Nodus\Packages\LivewireForms\Services\FormBuilder\Support\Currency; use Nodus\Packages\LivewireForms\Services\FormBuilder\Support\InputMode; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsInputMode; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder; @@ -25,6 +26,7 @@ class Decimal extends FormInput use SupportsHint; use SupportsPlaceholder; use SupportsInputMode; + use SupportsDisabling; /** * Number of decimals to be shown of the decimal value diff --git a/src/Services/FormBuilder/File.php b/src/Services/FormBuilder/File.php index 720c207..713bc42 100644 --- a/src/Services/FormBuilder/File.php +++ b/src/Services/FormBuilder/File.php @@ -4,6 +4,7 @@ use Livewire\TemporaryUploadedFile; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsArrayValidations; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsMultiple; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize; @@ -21,6 +22,7 @@ class File extends FormInput use SupportsArrayValidations; use SupportsSize; use SupportsHint; + use SupportsDisabling; /** * Accepted file formats diff --git a/src/Services/FormBuilder/FormInput.php b/src/Services/FormBuilder/FormInput.php index ff97660..16bf5b4 100644 --- a/src/Services/FormBuilder/FormInput.php +++ b/src/Services/FormBuilder/FormInput.php @@ -204,7 +204,15 @@ public function getErrorKeys(): array */ public static function supports(string $feature): bool { - $traits = collect(class_uses(static::class)) + $checkClasses = array_values(class_parents(static::class)); + $checkClasses[] = static::class; + + $traits = []; + foreach ($checkClasses as $class) { + $traits = array_merge($traits, class_uses($class)); + } + + $traits = collect($traits) ->map(fn ($value) => Str::of($value) ->classBasename() ->lower() diff --git a/src/Services/FormBuilder/Number.php b/src/Services/FormBuilder/Number.php index 0b237b8..7f3916c 100644 --- a/src/Services/FormBuilder/Number.php +++ b/src/Services/FormBuilder/Number.php @@ -3,6 +3,7 @@ namespace Nodus\Packages\LivewireForms\Services\FormBuilder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsMinMax; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder; @@ -22,4 +23,5 @@ class Number extends FormInput use SupportsHint; use SupportsPlaceholder; use SupportsMinMax; + use SupportsDisabling; } diff --git a/src/Services/FormBuilder/Password.php b/src/Services/FormBuilder/Password.php index 667e226..23c9417 100644 --- a/src/Services/FormBuilder/Password.php +++ b/src/Services/FormBuilder/Password.php @@ -3,6 +3,7 @@ namespace Nodus\Packages\LivewireForms\Services\FormBuilder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize; @@ -20,6 +21,7 @@ class Password extends FormInput use SupportsSize; use SupportsHint; use SupportsPlaceholder; + use SupportsDisabling; /** * Secure mode flag diff --git a/src/Services/FormBuilder/Radio.php b/src/Services/FormBuilder/Radio.php index 6c03f50..d2cb0ad 100644 --- a/src/Services/FormBuilder/Radio.php +++ b/src/Services/FormBuilder/Radio.php @@ -3,6 +3,7 @@ namespace Nodus\Packages\LivewireForms\Services\FormBuilder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsOptions; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize; @@ -15,4 +16,5 @@ class Radio extends FormInput use SupportsSize; use SupportsHint; use SupportsOptions; + use SupportsDisabling; } diff --git a/src/Services/FormBuilder/RichTextarea.php b/src/Services/FormBuilder/RichTextarea.php index ea41756..4d0901f 100644 --- a/src/Services/FormBuilder/RichTextarea.php +++ b/src/Services/FormBuilder/RichTextarea.php @@ -3,6 +3,7 @@ namespace Nodus\Packages\LivewireForms\Services\FormBuilder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsValidations; @@ -18,4 +19,5 @@ class RichTextarea extends FormInput use SupportsValidations; use SupportsSize; use SupportsHint; + use SupportsDisabling; } diff --git a/src/Services/FormBuilder/Select.php b/src/Services/FormBuilder/Select.php index 9f2f10a..47b0d83 100644 --- a/src/Services/FormBuilder/Select.php +++ b/src/Services/FormBuilder/Select.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsMultiple; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsOptions; @@ -38,6 +39,7 @@ class Select extends FormInput } use SupportsHint; use SupportsTranslations; + use SupportsDisabling; use SupportsOptions { getOptions as parentGetOptions; setOptions as parentSetOptions; diff --git a/src/Services/FormBuilder/Text.php b/src/Services/FormBuilder/Text.php index 75c8643..d0ad3e1 100644 --- a/src/Services/FormBuilder/Text.php +++ b/src/Services/FormBuilder/Text.php @@ -3,6 +3,7 @@ namespace Nodus\Packages\LivewireForms\Services\FormBuilder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsInputMode; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder; @@ -22,4 +23,5 @@ class Text extends FormInput use SupportsHint; use SupportsPlaceholder; use SupportsInputMode; + use SupportsDisabling; } diff --git a/src/Services/FormBuilder/Textarea.php b/src/Services/FormBuilder/Textarea.php index 7b27c8c..754cbdf 100644 --- a/src/Services/FormBuilder/Textarea.php +++ b/src/Services/FormBuilder/Textarea.php @@ -3,6 +3,7 @@ namespace Nodus\Packages\LivewireForms\Services\FormBuilder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsPlaceholder; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize; @@ -20,6 +21,7 @@ class Textarea extends FormInput use SupportsSize; use SupportsHint; use SupportsPlaceholder; + use SupportsDisabling; /** * Rows count diff --git a/src/Services/FormBuilder/Time.php b/src/Services/FormBuilder/Time.php index 6aa9e83..f765d5e 100644 --- a/src/Services/FormBuilder/Time.php +++ b/src/Services/FormBuilder/Time.php @@ -4,6 +4,7 @@ use Illuminate\Support\Carbon; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDefaultValue; +use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsDisabling; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsHint; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsMinMax; use Nodus\Packages\LivewireForms\Services\FormBuilder\Traits\SupportsSize; @@ -21,6 +22,7 @@ class Time extends FormInput use SupportsSize; use SupportsHint; use SupportsMinMax; + use SupportsDisabling; /** * Post validation mutator handler diff --git a/src/Services/FormBuilder/Traits/SupportsDisabling.php b/src/Services/FormBuilder/Traits/SupportsDisabling.php new file mode 100644 index 0000000..983acc6 --- /dev/null +++ b/src/Services/FormBuilder/Traits/SupportsDisabling.php @@ -0,0 +1,42 @@ +disabled; + } + + /** + * Sets the inputs disabled state + * + * @param bool $disabled + * + * @return $this + */ + public function setDisabled(bool $disabled = true): static + { + $this->disabled = $disabled; + + return $this; + } +} diff --git a/src/resources/views/livewire/bootstrap/components/input.blade.php b/src/resources/views/livewire/bootstrap/components/input.blade.php index 4385405..a290c5e 100644 --- a/src/resources/views/livewire/bootstrap/components/input.blade.php +++ b/src/resources/views/livewire/bootstrap/components/input.blade.php @@ -7,6 +7,7 @@ @if($input->getStep() !== null) step="{{ $input->getStep() }}" @endif @endif @if($input::supports('placeholder') && $input->hasPlaceholder()) placeholder="{{ $input->getPlaceholder() }}" @endif + @if($input::supports('disabling') && $input->isDisabled()) disabled @endif @if($input::supports('inputMode') && $input->getInputMode() !== null) inputmode="{{ $input->getInputMode() }}" @endif class="form-control @if(isset($errors) && $errors->hasAny($input->getErrorKeys())) is-invalid @endif" wire:model.{{config('livewire-forms.update_mode')}}="{{ $input->getViewId() }}"> diff --git a/src/resources/views/livewire/bootstrap/components/types/checkbox.blade.php b/src/resources/views/livewire/bootstrap/components/types/checkbox.blade.php index a8c2e62..0ee56c6 100644 --- a/src/resources/views/livewire/bootstrap/components/types/checkbox.blade.php +++ b/src/resources/views/livewire/bootstrap/components/types/checkbox.blade.php @@ -4,6 +4,7 @@ name="{{ $input->getName() }}" class="custom-control-input" id="{{ $input->getId(true) }}" + @if($input::supports('disabling') && $input->isDisabled()) disabled @endif wire:model.{{config('livewire-forms.update_mode')}}="{{ $input->getViewId() }}">