Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/forms/src/Components/TextInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class TextInput extends Field implements CanHaveNumericState, Contracts\CanBeLen

protected bool | Closure $isEmail = false;

protected bool | Closure $isInteger = false;

protected bool | Closure $isNumeric = false;

protected bool | Closure $isPassword = false;
Expand Down Expand Up @@ -85,6 +87,8 @@ public function email(bool | Closure $condition = true): static

public function integer(bool | Closure $condition = true): static
{
$this->isInteger = $condition;

$this->numeric($condition);
$this->inputMode(static fn (): ?string => $condition ? 'numeric' : null);
$this->step(static fn (): ?int => $condition ? 1 : null);
Expand Down Expand Up @@ -274,6 +278,11 @@ public function isEmail(): bool
return (bool) $this->evaluate($this->isEmail);
}

public function isInteger(): bool
{
return (bool) $this->evaluate($this->isInteger);
}

public function isNumeric(): bool
{
return (bool) $this->evaluate($this->isNumeric);
Expand Down Expand Up @@ -302,7 +311,7 @@ public function getDefaultStateCasts(): array
return [
...parent::getDefaultStateCasts(),
...($this->hasStripCharacters() ? [app(StripCharactersStateCast::class, ['characters' => $this->getStripCharacters()])] : []),
...($this->isNumeric() ? [app(NumberStateCast::class, ['isNullable' => true])] : []),
...($this->isNumeric() ? [app(NumberStateCast::class, ['isNullable' => true, 'isInteger' => $this->isInteger()])] : []),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ class NumberStateCast implements StateCast
{
public function __construct(
protected bool $isNullable = true,
protected bool $isInteger = false,
) {}

public function get(mixed $state): ?float
public function get(mixed $state): int | float | null
{
if ($this->isNullable && blank($state)) {
return null;
}

return floatval($state);
return $this->isInteger ? intval($state) : floatval($state);
}

public function set(mixed $state): ?float
public function set(mixed $state): int | float | null
{
if ($this->isNullable && blank($state)) {
return null;
}

return floatval($state);
return $this->isInteger ? intval($state) : floatval($state);
}
}
28 changes: 28 additions & 0 deletions tests/src/Forms/Components/TextInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
[null, null],
]);

it('casts state to an integer when using `integer()`', function (): void {
$component = livewire(TestComponentWithIntegerTextInput::class)
->fillForm(['quantity' => '1234'])
->call('save');

expect($component->get('data.quantity'))->toBe(1234);
});

it('can set `email()`', function (): void {
$input = TextInput::make('email');

Expand Down Expand Up @@ -290,6 +298,26 @@ public function save(): void
}
}

class TestComponentWithIntegerTextInput extends Livewire
{
public $data = [];

public function form(Schema $form): Schema
{
return $form
->schema([
TextInput::make('quantity')
->integer(),
])
->statePath('data');
}

public function save(): void
{
$this->data = $this->form->getState();
}
}

class TestComponentWithTextInputTrim extends Livewire
{
public $data = [];
Expand Down