From 7b94df65ca2015675c980539eb7a07cb6b8edbfd Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 18 Nov 2022 12:19:16 +0000 Subject: [PATCH 1/3] feature: add support for readonly inputs/textarea --- .../views/components/text-input.blade.php | 1 + .../views/components/textarea.blade.php | 1 + .../src/Components/Concerns/CanBeReadonly.php | 40 +++++++++++++++++++ packages/forms/src/Components/TextInput.php | 1 + packages/forms/src/Components/Textarea.php | 1 + packages/forms/src/Testing/TestsForms.php | 23 +++++++++++ 6 files changed, 67 insertions(+) create mode 100644 packages/forms/src/Components/Concerns/CanBeReadonly.php diff --git a/packages/forms/resources/views/components/text-input.blade.php b/packages/forms/resources/views/components/text-input.blade.php index 318c7beb86..290f83536a 100644 --- a/packages/forms/resources/views/components/text-input.blade.php +++ b/packages/forms/resources/views/components/text-input.blade.php @@ -62,6 +62,7 @@ 'min' => (! $isConcealed) ? $getMinValue() : null, 'minlength' => (! $isConcealed) ? $getMinLength() : null, 'placeholder' => $getPlaceholder(), + 'readonly' => $isReadonly(), 'required' => $isRequired() && (! $isConcealed), 'step' => $getStep(), 'type' => $hasMask ? 'text' : $getType(), diff --git a/packages/forms/resources/views/components/textarea.blade.php b/packages/forms/resources/views/components/textarea.blade.php index 4572b81cb5..2f962cdd8f 100644 --- a/packages/forms/resources/views/components/textarea.blade.php +++ b/packages/forms/resources/views/components/textarea.blade.php @@ -29,6 +29,7 @@ 'maxlength' => (! $isConcealed) ? $getMaxLength() : null, 'minlength' => (! $isConcealed) ? $getMinLength() : null, 'placeholder' => $getPlaceholder(), + 'readonly' => $isReadonly(), 'required' => $isRequired() && (! $isConcealed), 'rows' => $getRows(), $applyStateBindingModifiers('wire:model') => $getStatePath(), diff --git a/packages/forms/src/Components/Concerns/CanBeReadonly.php b/packages/forms/src/Components/Concerns/CanBeReadonly.php new file mode 100644 index 0000000000..e6c021151d --- /dev/null +++ b/packages/forms/src/Components/Concerns/CanBeReadonly.php @@ -0,0 +1,40 @@ +isReadonly = $condition; + + return $this; + } + + public function readonlyOn(string | array $contexts): static + { + $this->readonly(static function (string $context, HasForms $livewire) use ($contexts): bool { + foreach (Arr::wrap($contexts) as $readonlyContext) { + if ($readonlyContext === $context || $livewire instanceof $readonlyContext) { + return true; + } + } + + return false; + }); + + return $this; + } + + public function isReadonly(): bool + { + return $this->evaluate($this->isReadonly); + } +} diff --git a/packages/forms/src/Components/TextInput.php b/packages/forms/src/Components/TextInput.php index 0ed5b9f37c..592fa9cce1 100644 --- a/packages/forms/src/Components/TextInput.php +++ b/packages/forms/src/Components/TextInput.php @@ -13,6 +13,7 @@ class TextInput extends Field implements Contracts\CanBeLengthConstrained, CanHa use Concerns\CanBeAutocapitalized; use Concerns\CanBeAutocompleted; use Concerns\CanBeLengthConstrained; + use Concerns\CanBeReadonly; use Concerns\HasAffixes; use Concerns\HasExtraInputAttributes; use Concerns\HasInputMode; diff --git a/packages/forms/src/Components/Textarea.php b/packages/forms/src/Components/Textarea.php index c15f78100f..a58615f716 100644 --- a/packages/forms/src/Components/Textarea.php +++ b/packages/forms/src/Components/Textarea.php @@ -10,6 +10,7 @@ class Textarea extends Field implements Contracts\CanBeLengthConstrained use Concerns\CanBeAutocapitalized; use Concerns\CanBeAutocompleted; use Concerns\CanBeLengthConstrained; + use Concerns\CanBeReadonly; use Concerns\HasExtraInputAttributes; use Concerns\HasPlaceholder; use HasExtraAlpineAttributes; diff --git a/packages/forms/src/Testing/TestsForms.php b/packages/forms/src/Testing/TestsForms.php index 54e2afdf3c..2b6237eebf 100644 --- a/packages/forms/src/Testing/TestsForms.php +++ b/packages/forms/src/Testing/TestsForms.php @@ -204,6 +204,29 @@ class TestsForms }; } + public function assertFormFieldIsReadonly(): Closure + { + return function (string $fieldName, string $formName = 'form'): static { + /** @phpstan-ignore-next-line */ + $this->assertFormFieldExists($fieldName, $formName); + + /** @var ComponentContainer $form */ + $form = $this->instance()->{$formName}; + + /** @var Field $field */ + $field = $form->getFlatFields(withHidden: true)[$fieldName]; + + $livewireClass = $this->instance()::class; + + Assert::assertTrue( + $field->isReadonly(), + "Failed asserting that a field with the name [{$fieldName}] is readonly on the form named [{$formName}] on the [{$livewireClass}] component." + ); + + return $this; + }; + } + public function assertFormFieldIsHidden(): Closure { return function (string $fieldName, string $formName = 'form'): static { From 784a722bc703cb16c58878bd98d7e2443c287993 Mon Sep 17 00:00:00 2001 From: ryangjchandler Date: Fri, 18 Nov 2022 12:21:41 +0000 Subject: [PATCH 2/3] chore: fix code style --- packages/forms/src/Components/Concerns/CanBeReadonly.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/forms/src/Components/Concerns/CanBeReadonly.php b/packages/forms/src/Components/Concerns/CanBeReadonly.php index e6c021151d..cb84461892 100644 --- a/packages/forms/src/Components/Concerns/CanBeReadonly.php +++ b/packages/forms/src/Components/Concerns/CanBeReadonly.php @@ -3,7 +3,6 @@ namespace Filament\Forms\Components\Concerns; use Closure; -use Filament\Forms\Components\Component; use Filament\Forms\Contracts\HasForms; use Illuminate\Support\Arr; From c7af96ecf51bbf1bf57b172cf82deac4c9b733ee Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sun, 20 Nov 2022 11:17:41 +0000 Subject: [PATCH 3/3] capitalise Only --- .../views/components/text-input.blade.php | 2 +- .../views/components/textarea.blade.php | 2 +- .../src/Components/Concerns/CanBeReadonly.php | 20 +++++++++---------- packages/forms/src/Components/TextInput.php | 2 +- packages/forms/src/Components/Textarea.php | 2 +- packages/forms/src/Testing/TestsForms.php | 6 +++--- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/forms/resources/views/components/text-input.blade.php b/packages/forms/resources/views/components/text-input.blade.php index 290f83536a..66654f24df 100644 --- a/packages/forms/resources/views/components/text-input.blade.php +++ b/packages/forms/resources/views/components/text-input.blade.php @@ -62,7 +62,7 @@ 'min' => (! $isConcealed) ? $getMinValue() : null, 'minlength' => (! $isConcealed) ? $getMinLength() : null, 'placeholder' => $getPlaceholder(), - 'readonly' => $isReadonly(), + 'readonly' => $isReadOnly(), 'required' => $isRequired() && (! $isConcealed), 'step' => $getStep(), 'type' => $hasMask ? 'text' : $getType(), diff --git a/packages/forms/resources/views/components/textarea.blade.php b/packages/forms/resources/views/components/textarea.blade.php index 2f962cdd8f..fe27ff4cc0 100644 --- a/packages/forms/resources/views/components/textarea.blade.php +++ b/packages/forms/resources/views/components/textarea.blade.php @@ -29,7 +29,7 @@ 'maxlength' => (! $isConcealed) ? $getMaxLength() : null, 'minlength' => (! $isConcealed) ? $getMinLength() : null, 'placeholder' => $getPlaceholder(), - 'readonly' => $isReadonly(), + 'readonly' => $isReadOnly(), 'required' => $isRequired() && (! $isConcealed), 'rows' => $getRows(), $applyStateBindingModifiers('wire:model') => $getStatePath(), diff --git a/packages/forms/src/Components/Concerns/CanBeReadonly.php b/packages/forms/src/Components/Concerns/CanBeReadonly.php index cb84461892..85545a6342 100644 --- a/packages/forms/src/Components/Concerns/CanBeReadonly.php +++ b/packages/forms/src/Components/Concerns/CanBeReadonly.php @@ -6,22 +6,22 @@ use Closure; use Filament\Forms\Contracts\HasForms; use Illuminate\Support\Arr; -trait CanBeReadonly +trait CanBeReadOnly { - protected bool | Closure $isReadonly = false; + protected bool | Closure $isReadOnly = false; - public function readonly(bool | Closure $condition = true): static + public function readOnly(bool | Closure $condition = true): static { - $this->isReadonly = $condition; + $this->isReadOnly = $condition; return $this; } - public function readonlyOn(string | array $contexts): static + public function readOnlyOn(string | array $contexts): static { - $this->readonly(static function (string $context, HasForms $livewire) use ($contexts): bool { - foreach (Arr::wrap($contexts) as $readonlyContext) { - if ($readonlyContext === $context || $livewire instanceof $readonlyContext) { + $this->readOnly(static function (string $context, HasForms $livewire) use ($contexts): bool { + foreach (Arr::wrap($contexts) as $readOnlyContext) { + if ($readOnlyContext === $context || $livewire instanceof $readOnlyContext) { return true; } } @@ -32,8 +32,8 @@ trait CanBeReadonly return $this; } - public function isReadonly(): bool + public function isReadOnly(): bool { - return $this->evaluate($this->isReadonly); + return $this->evaluate($this->isReadOnly); } } diff --git a/packages/forms/src/Components/TextInput.php b/packages/forms/src/Components/TextInput.php index 592fa9cce1..d78d7eb3e8 100644 --- a/packages/forms/src/Components/TextInput.php +++ b/packages/forms/src/Components/TextInput.php @@ -13,7 +13,7 @@ class TextInput extends Field implements Contracts\CanBeLengthConstrained, CanHa use Concerns\CanBeAutocapitalized; use Concerns\CanBeAutocompleted; use Concerns\CanBeLengthConstrained; - use Concerns\CanBeReadonly; + use Concerns\CanBeReadOnly; use Concerns\HasAffixes; use Concerns\HasExtraInputAttributes; use Concerns\HasInputMode; diff --git a/packages/forms/src/Components/Textarea.php b/packages/forms/src/Components/Textarea.php index a58615f716..4f810c35d3 100644 --- a/packages/forms/src/Components/Textarea.php +++ b/packages/forms/src/Components/Textarea.php @@ -10,7 +10,7 @@ class Textarea extends Field implements Contracts\CanBeLengthConstrained use Concerns\CanBeAutocapitalized; use Concerns\CanBeAutocompleted; use Concerns\CanBeLengthConstrained; - use Concerns\CanBeReadonly; + use Concerns\CanBeReadOnly; use Concerns\HasExtraInputAttributes; use Concerns\HasPlaceholder; use HasExtraAlpineAttributes; diff --git a/packages/forms/src/Testing/TestsForms.php b/packages/forms/src/Testing/TestsForms.php index 2b6237eebf..56b5019b00 100644 --- a/packages/forms/src/Testing/TestsForms.php +++ b/packages/forms/src/Testing/TestsForms.php @@ -204,7 +204,7 @@ class TestsForms }; } - public function assertFormFieldIsReadonly(): Closure + public function assertFormFieldIsReadOnly(): Closure { return function (string $fieldName, string $formName = 'form'): static { /** @phpstan-ignore-next-line */ @@ -219,8 +219,8 @@ class TestsForms $livewireClass = $this->instance()::class; Assert::assertTrue( - $field->isReadonly(), - "Failed asserting that a field with the name [{$fieldName}] is readonly on the form named [{$formName}] on the [{$livewireClass}] component." + $field->isReadOnly(), + "Failed asserting that a field with the name [{$fieldName}] is read-only on the form named [{$formName}] on the [{$livewireClass}] component." ); return $this;