diff --git a/src/Elements/NumberInput.php b/src/Elements/NumberInput.php index ea8ad5b..5bde365 100644 --- a/src/Elements/NumberInput.php +++ b/src/Elements/NumberInput.php @@ -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; } @@ -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; } diff --git a/src/Kit.php b/src/Kit.php index e471c88..080d6f1 100644 --- a/src/Kit.php +++ b/src/Kit.php @@ -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, diff --git a/tests/Functional/CreateTest.php b/tests/Functional/CreateTest.php index 585751f..6c7f650 100644 --- a/tests/Functional/CreateTest.php +++ b/tests/Functional/CreateTest.php @@ -191,6 +191,7 @@ public function testThatAllKitComponentMethodsReturnComponents(): void } } + // @TODO This should be moved to a unit test eventually. public function testNumberInput(): void { $modal = Modal::new() @@ -198,14 +199,14 @@ public function testNumberInput(): void ->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']); } }