diff --git a/app/Enums/StartupVariableType.php b/app/Enums/StartupVariableType.php index 6dd287f7e..479a6cd16 100644 --- a/app/Enums/StartupVariableType.php +++ b/app/Enums/StartupVariableType.php @@ -5,6 +5,7 @@ namespace App\Enums; enum StartupVariableType: string { case Text = 'text'; + case Number = 'number'; case Select = 'select'; case Toggle = 'toggle'; } diff --git a/app/Filament/Components/Forms/Fields/StartupVariable.php b/app/Filament/Components/Forms/Fields/StartupVariable.php index 18fc833ef..0961259c5 100644 --- a/app/Filament/Components/Forms/Fields/StartupVariable.php +++ b/app/Filament/Components/Forms/Fields/StartupVariable.php @@ -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; + } } diff --git a/resources/views/filament/components/startup-variable.blade.php b/resources/views/filament/components/startup-variable.blade.php index 2b0a041a8..6d165be3d 100644 --- a/resources/views/filament/components/startup-variable.blade.php +++ b/resources/views/filament/components/startup-variable.blade.php @@ -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)