add support for number inputs for startup variables (and set default cast)

This commit is contained in:
Boy132 2025-09-02 13:09:38 +02:00
parent b70b8e477d
commit a7df78d211
3 changed files with 70 additions and 0 deletions

View File

@ -5,6 +5,7 @@ namespace App\Enums;
enum StartupVariableType: string
{
case Text = 'text';
case Number = 'number';
case Select = 'select';
case Toggle = 'toggle';
}

View File

@ -9,6 +9,9 @@ use Filament\Forms\Components\Concerns\HasAffixes;
use Filament\Forms\Components\Concerns\HasExtraInputAttributes;
use Filament\Forms\Components\Concerns\HasPlaceholder;
use Filament\Forms\Components\Field;
use Filament\Schemas\Components\StateCasts\BooleanStateCast;
use Filament\Schemas\Components\StateCasts\Contracts\StateCast;
use Filament\Schemas\Components\StateCasts\NumberStateCast;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Support\Concerns\HasExtraAlpineAttributes;
use Illuminate\Support\Arr;
@ -56,6 +59,24 @@ class StartupVariable extends Field
$this->live(onBlur: true);
}
/**
* @return StateCast[]
*/
public function getDefaultStateCasts(): array
{
return match ($this->getType()) {
StartupVariableType::Number => [
...parent::getDefaultStateCasts(),
app(NumberStateCast::class, ['isNullable' => false]),
],
StartupVariableType::Toggle => [
...parent::getDefaultStateCasts(),
app(BooleanStateCast::class, ['isNullable' => false]),
],
default => parent::getDefaultStateCasts()
};
}
public function fromForm(): static
{
$this->variableName(fn (Get $get) => $get('name'));
@ -147,6 +168,36 @@ class StartupVariable extends Field
return in_array('required', $rules);
}
public function getMinValue(): ?int
{
$rules = $this->getVariableRules();
$minRule = Arr::first($rules, fn ($value) => str($value)->startsWith('min:'));
if ($minRule) {
return str($minRule)
->after('min:')
->trim()
->toInteger();
}
return null;
}
public function getMaxValue(): ?int
{
$rules = $this->getVariableRules();
$maxRule = Arr::first($rules, fn ($value) => str($value)->startsWith('max:'));
if ($maxRule) {
return str($maxRule)
->after('max:')
->trim()
->toInteger();
}
return null;
}
public function getType(): StartupVariableType
{
$rules = $this->getVariableRules();
@ -159,6 +210,10 @@ class StartupVariable extends Field
return StartupVariableType::Toggle;
}
if (in_array('numeric', $rules) || in_array('integer', $rules)) {
return StartupVariableType::Number;
}
return StartupVariableType::Text;
}
@ -178,4 +233,14 @@ class StartupVariable extends Field
return [];
}
public function getTextType(): string
{
return $this->getType() === StartupVariableType::Number ? 'number' : 'text';
}
public function getTextInputMode(): ?string
{
return $this->getType() === StartupVariableType::Number ? 'decimal' : null;
}
}

View File

@ -68,6 +68,10 @@
:required="$isRequired"
:disabled="$isDisabled"
:placeholder="$getPlaceholder()"
:type="$getTextType()"
:inputmode="$getTextInputMode()"
:max="$getMaxValue()"
:min="$getMinValue()"
:attributes="
\Filament\Support\prepare_inherited_attributes($getExtraInputAttributeBag())
->merge($getExtraAlpineAttributes(), escape: false)