Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update NumberInput properties #45

Merged
merged 5 commits into from
Dec 19, 2023
Merged
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
47 changes: 24 additions & 23 deletions src/Elements/NumberInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,64 @@
use SlackPhp\BlockKit\Elements\Traits\HasPlaceholder;
use SlackPhp\BlockKit\Parts\{DispatchActionConfig, PlainText};
use SlackPhp\BlockKit\Property;
use SlackPhp\BlockKit\Validation\ValidInt;
use SlackPhp\BlockKit\Validation\{RequiresAllOf, ValidString};

#[RequiresAllOf('is_decimal_allowed')]
class NumberInput extends Input
{
use HasPlaceholder;

#[Property('is_decimal_allowed')]
public bool $decimalAllowed = false;
public bool $allowDecimal;

#[Property('initial_value')]
#[Property('initial_value'), ValidString]
public ?string $initialValue;

#[Property('min_value')]
public ?int $minValue;
#[Property('min_value'), ValidString]
public ?string $minValue;

#[Property('max_value')]
public ?int $maxValue;
#[Property('max_value'), ValidString]
public ?string $maxValue;

#[Property('dispatch_action_config')]
public ?DispatchActionConfig $dispatchActionConfig;

public function __construct(
?string $actionId = null,
?bool $allowDecimal = null,
int|float|string|null $maxValue = null,
int|float|string|null $minValue = null,
int|float|string|null $initialValue = null,
PlainText|string|null $placeholder = null,
?int $maxValue = null,
?int $minValue = null,
?DispatchActionConfig $dispatchActionConfig = null,
?string $initialValue = null,
bool $decimalAllowed = null,
?bool $focusOnLoad = null,
?DispatchActionConfig $dispatchActionConfig = null,
) {
parent::__construct($actionId, $focusOnLoad);
$this->placeholder($placeholder);
$this->allowDecimal($allowDecimal);
$this->maxValue($maxValue);
$this->minValue($minValue);
$this->dispatchActionConfig($dispatchActionConfig);
$this->initialValue($initialValue);
$this->decimalAllowed($decimalAllowed);
$this->placeholder($placeholder);
$this->dispatchActionConfig($dispatchActionConfig);
}

public function initialValue(?string $text): self
public function initialValue(int|float|string|null $initialValue): self
{
$this->initialValue = $text;
$this->initialValue = $initialValue === null ? null : (string) $initialValue;

return $this;
}

public function minValue(?int $value): self
public function minValue(int|float|string|null $minValue): self
{
$this->minValue = $value;
$this->minValue = $minValue === null ? null : (string) $minValue;

return $this;
}

public function maxValue(?int $value): self
public function maxValue(int|float|string|null $maxValue): self
{
$this->maxValue = $value;
$this->maxValue = $maxValue === null ? null : (string) $maxValue;

return $this;
}
Expand All @@ -75,9 +76,9 @@ public function dispatchActionConfig(?DispatchActionConfig $config): self
return $this;
}

public function decimalAllowed(?bool $allowedDecimal): self
public function allowDecimal(?bool $allowDecimal): self
{
$this->decimalAllowed = (bool) $allowedDecimal;
$this->allowDecimal = (bool) $allowDecimal;

return $this;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Kit.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,19 @@ public static function plainTextInput(
return new Elements\PlainTextInput($actionId, $placeholder, $maxLength, $minLength, $multiline, $dispatchActionConfig, $initialValue, $focusOnLoad);
}

public static function numberInput(
?string $actionId = null,
?bool $allowDecimal = null,
int|float|string|null $maxValue = null,
int|float|string|null $minValue = null,
int|float|string|null $initialValue = null,
Parts\PlainText|string|null $placeholder = null,
?bool $focusOnLoad = null,
?Parts\DispatchActionConfig $dispatchActionConfig = null,
): Elements\NumberInput {
return new Elements\NumberInput($actionId, $allowDecimal, $maxValue, $minValue, $initialValue, $placeholder, $focusOnLoad, $dispatchActionConfig);
}

public static function timePicker(
?string $actionId = null,
\DateTime|string|null $initialTime = null,
Expand Down
7 changes: 4 additions & 3 deletions tests/Functional/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,22 @@ public function testThatAllKitComponentMethodsReturnComponents(): void
}
}

// @TODO This should be moved to a unit test eventually.
public function testNumberInput(): void
{
$modal = Modal::new()
->title('Modal test')
->submit('Click me')
->blocks(
Input::new()->label('Input')->element(
NumberInput::new()->minValue(1)->maxValue(50)->decimalAllowed(true)
NumberInput::new()->minValue(1)->maxValue(50)->allowDecimal(true)
)
);
$modal->validate();
$numberInput = $modal->toArray()['blocks'][0]['element'];
self::assertSame('number_input', $numberInput['type']);
self::assertTrue($numberInput['is_decimal_allowed']);
self::assertSame(1, $numberInput['min_value']);
self::assertSame(50, $numberInput['max_value']);
self::assertSame('1', $numberInput['min_value']);
self::assertSame('50', $numberInput['max_value']);
}
}
Loading