mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Merge branch 'feature/rc/readonly-input-fields' into 3.x
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
'maxlength' => (! $isConcealed) ? $getMaxLength() : null,
|
||||
'minlength' => (! $isConcealed) ? $getMinLength() : null,
|
||||
'placeholder' => $getPlaceholder(),
|
||||
'readonly' => $isReadOnly(),
|
||||
'required' => $isRequired() && (! $isConcealed),
|
||||
'rows' => $getRows(),
|
||||
$applyStateBindingModifiers('wire:model') => $statePath,
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Forms\Components\Concerns;
|
||||
|
||||
use Closure;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
trait CanBeReadOnly
|
||||
{
|
||||
protected bool | Closure $isReadOnly = false;
|
||||
|
||||
public function readOnly(bool | Closure $condition = true): static
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 read-only on the form named [{$formName}] on the [{$livewireClass}] component."
|
||||
);
|
||||
|
||||
return $this;
|
||||
};
|
||||
}
|
||||
|
||||
public function assertFormFieldIsHidden(): Closure
|
||||
{
|
||||
return function (string $fieldName, string $formName = 'form'): static {
|
||||
|
||||
Reference in New Issue
Block a user