mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
clean up and make consistent with other fields
This commit is contained in:
@@ -6,6 +6,7 @@ use Closure;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Schemas\Components\StateCasts\Contracts\StateCast;
|
||||
use Filament\Schemas\Components\StateCasts\EnumArrayStateCast;
|
||||
use Filament\Schemas\Components\StateCasts\StringArrayStateCast;
|
||||
use Filament\Support\Concerns\HasExtraAlpineAttributes;
|
||||
use Filament\Support\Enums\Size;
|
||||
use Filament\Support\Services\RelationshipJoiner;
|
||||
@@ -54,16 +55,6 @@ class CheckboxList extends Field implements Contracts\CanDisableOptions, Contrac
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->default([]);
|
||||
|
||||
$this->afterStateHydrated(static function (CheckboxList $component, $state): void {
|
||||
if (is_array($state)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$component->state([]);
|
||||
});
|
||||
|
||||
$this->searchDebounce(0);
|
||||
|
||||
$this->registerActions([
|
||||
@@ -332,6 +323,18 @@ class CheckboxList extends Field implements Contracts\CanDisableOptions, Contrac
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<StateCast>
|
||||
*/
|
||||
public function getDefaultStateCasts(): array
|
||||
{
|
||||
if ($this->hasCustomStateCasts() || filled($this->getEnum())) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [app(StringArrayStateCast::class)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?array<string>
|
||||
*/
|
||||
|
||||
@@ -4,6 +4,9 @@ namespace Filament\Forms\Components;
|
||||
|
||||
use Closure;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Schemas\Components\StateCasts\Contracts\StateCast;
|
||||
use Filament\Schemas\Components\StateCasts\StringArrayStateCast;
|
||||
use Filament\Schemas\Components\StateCasts\StringStateCast;
|
||||
use Filament\Support\Enums\IconPosition;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Support\Services\RelationshipJoiner;
|
||||
@@ -65,20 +68,6 @@ class ModalTableSelect extends Field
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->default(static fn (ModalTableSelect $component): ?array => $component->isMultiple() ? [] : null);
|
||||
|
||||
$this->afterStateHydrated(static function (ModalTableSelect $component, $state): void {
|
||||
if (! $component->isMultiple()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_array($state)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$component->state([]);
|
||||
});
|
||||
|
||||
$this->registerActions([
|
||||
fn (ModalTableSelect $component): Action => $component->getSelectAction(),
|
||||
]);
|
||||
@@ -679,4 +668,20 @@ class ModalTableSelect extends Field
|
||||
{
|
||||
return $this->evaluate($this->tableArguments) ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<StateCast>
|
||||
*/
|
||||
public function getDefaultStateCasts(): array
|
||||
{
|
||||
if ($this->hasCustomStateCasts()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->isMultiple()) {
|
||||
return [app(StringArrayStateCast::class)];
|
||||
}
|
||||
|
||||
return [app(StringStateCast::class, ['isNullable' => true])];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ namespace Filament\Forms\Components;
|
||||
|
||||
use Closure;
|
||||
use Filament\Schemas\Components\StateCasts\BooleanStateCast;
|
||||
use Filament\Schemas\Components\StateCasts\Contracts\StateCast;
|
||||
use Filament\Schemas\Components\StateCasts\StringStateCast;
|
||||
|
||||
class Radio extends Field implements Contracts\CanDisableOptions
|
||||
{
|
||||
@@ -57,6 +59,18 @@ class Radio extends Field implements Contracts\CanDisableOptions
|
||||
return $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<StateCast>
|
||||
*/
|
||||
public function getDefaultStateCasts(): array
|
||||
{
|
||||
if ($this->hasCustomStateCasts() || filled($this->getEnum())) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [app(StringStateCast::class, ['isNullable' => true])];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?array<string>
|
||||
*/
|
||||
|
||||
@@ -908,7 +908,7 @@ class Repeater extends Field implements CanConcealComponents, HasExtraItemAction
|
||||
$this->relationship = $name ?? $this->getName();
|
||||
$this->modifyRelationshipQueryUsing = $modifyQueryUsing;
|
||||
|
||||
$this->afterStateHydrated(function (Repeater $component): void {
|
||||
$this->afterStateHydrated(static function (Repeater $component): void {
|
||||
if (! is_array($component->hydratedDefaultState)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -133,20 +133,6 @@ class Select extends Field implements Contracts\CanDisableOptions, Contracts\Has
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->default(static fn (Select $component): ?array => $component->isMultiple() ? [] : null);
|
||||
|
||||
$this->afterStateHydrated(static function (Select $component, $state): void {
|
||||
if (! $component->isMultiple()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_array($state)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$component->state([]);
|
||||
});
|
||||
|
||||
$this->transformOptionsForJsUsing(static function (Select $component, array $options): array {
|
||||
return collect($options)
|
||||
->map(fn ($label, $value): array => is_array($label)
|
||||
|
||||
@@ -82,7 +82,7 @@ class Slider extends Field implements Contracts\HasNestedRecursiveValidationRule
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->default(fn (Slider $component): float | int => $component->getMinValue());
|
||||
$this->default(static fn (Slider $component): float | int => $component->getMinValue());
|
||||
|
||||
$this->required();
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
namespace Filament\Forms\Components;
|
||||
|
||||
use Closure;
|
||||
use Filament\Schemas\Components\StateCasts\Contracts\StateCast;
|
||||
use Filament\Schemas\Components\StateCasts\StringArrayStateCast;
|
||||
use Filament\Schemas\Components\StateCasts\StringStateCast;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -303,4 +306,20 @@ class TableSelect extends Field
|
||||
{
|
||||
return (bool) $this->evaluate($this->isMultiple);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<StateCast>
|
||||
*/
|
||||
public function getDefaultStateCasts(): array
|
||||
{
|
||||
if ($this->hasCustomStateCasts()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->isMultiple()) {
|
||||
return [app(StringArrayStateCast::class)];
|
||||
}
|
||||
|
||||
return [app(StringStateCast::class, ['isNullable' => true])];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ use Filament\Schemas\Components\StateCasts\BooleanStateCast;
|
||||
use Filament\Schemas\Components\StateCasts\Contracts\StateCast;
|
||||
use Filament\Schemas\Components\StateCasts\EnumArrayStateCast;
|
||||
use Filament\Schemas\Components\StateCasts\EnumStateCast;
|
||||
use Filament\Schemas\Components\StateCasts\StringArrayStateCast;
|
||||
use Filament\Schemas\Components\StateCasts\StringStateCast;
|
||||
use Filament\Support\Facades\FilamentIcon;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
|
||||
@@ -36,25 +38,6 @@ class ToggleButtons extends Field implements Contracts\CanDisableOptions
|
||||
|
||||
protected bool | Closure $areButtonLabelsHidden = false;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->default(fn (ToggleButtons $component): mixed => $component->isMultiple() ? [] : null);
|
||||
|
||||
$this->afterStateHydrated(static function (ToggleButtons $component, $state): void {
|
||||
if (! $component->isMultiple()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_array($state)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$component->state([]);
|
||||
});
|
||||
}
|
||||
|
||||
public function grouped(): static
|
||||
{
|
||||
return $this->view(static::GROUPED_VIEW);
|
||||
@@ -143,6 +126,22 @@ class ToggleButtons extends Field implements Contracts\CanDisableOptions
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<StateCast>
|
||||
*/
|
||||
public function getDefaultStateCasts(): array
|
||||
{
|
||||
if ($this->hasCustomStateCasts() || filled($this->getEnum())) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->isMultiple()) {
|
||||
return [app(StringArrayStateCast::class)];
|
||||
}
|
||||
|
||||
return [app(StringStateCast::class, ['isNullable' => true])];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ?array<string>
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user