Skip to content

Commit

Permalink
Adding - NumberInput (#43)
Browse files Browse the repository at this point in the history
* added number input
* allow dateinterface instead of datetime
  • Loading branch information
lsv authored Dec 19, 2023
1 parent 9e84f55 commit 9d1f472
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Elements/DatePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SlackPhp\BlockKit\Elements;

use DateTime;
use DateTimeInterface;
use SlackPhp\BlockKit\Elements\Traits\{HasConfirm, HasPlaceholder};
use SlackPhp\BlockKit\Parts\Confirm;
use SlackPhp\BlockKit\Property;
Expand All @@ -20,7 +20,7 @@ class DatePicker extends Input

public function __construct(
?string $actionId = null,
DateTime|string|null $initialDate = null,
DateTimeInterface|string|null $initialDate = null,
?string $placeholder = null,
?Confirm $confirm = null,
?bool $focusOnLoad = null,
Expand All @@ -31,9 +31,9 @@ public function __construct(
$this->initialDate($initialDate);
}

public function initialDate(DateTime|string|null $initialDate): self
public function initialDate(DateTimeInterface|string|null $initialDate): self
{
$this->initialDate = ($initialDate instanceof DateTime) ? $initialDate->format('Y-m-d') : $initialDate;
$this->initialDate = ($initialDate instanceof DateTimeInterface) ? $initialDate->format('Y-m-d') : $initialDate;

return $this;
}
Expand Down
84 changes: 84 additions & 0 deletions src/Elements/NumberInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace SlackPhp\BlockKit\Elements;

use SlackPhp\BlockKit\Elements\Traits\HasPlaceholder;
use SlackPhp\BlockKit\Parts\{DispatchActionConfig, PlainText};
use SlackPhp\BlockKit\Property;
use SlackPhp\BlockKit\Validation\ValidInt;

class NumberInput extends Input
{
use HasPlaceholder;

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

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

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

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

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

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

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

return $this;
}

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

return $this;
}

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

return $this;
}

public function dispatchActionConfig(?DispatchActionConfig $config): self
{
$this->dispatchActionConfig = $config;

return $this;
}

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

return $this;
}
}
8 changes: 4 additions & 4 deletions src/Elements/TimePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SlackPhp\BlockKit\Elements;

use DateTime;
use DateTimeInterface;
use SlackPhp\BlockKit\Elements\Traits\{HasConfirm, HasPlaceholder};
use SlackPhp\BlockKit\Parts\Confirm;
use SlackPhp\BlockKit\Property;
Expand All @@ -20,7 +20,7 @@ class TimePicker extends Input

public function __construct(
?string $actionId = null,
DateTime|string|null $initialTime = null,
DateTimeInterface|string|null $initialTime = null,
?string $placeholder = null,
?Confirm $confirm = null,
?bool $focusOnLoad = null,
Expand All @@ -31,9 +31,9 @@ public function __construct(
$this->initialTime($initialTime);
}

public function initialTime(DateTime|string|null $initialTime): self
public function initialTime(DateTimeInterface|string|null $initialTime): self
{
$this->initialTime = ($initialTime instanceof DateTime) ? $initialTime->format('H:i') : $initialTime;
$this->initialTime = ($initialTime instanceof DateTimeInterface) ? $initialTime->format('H:i') : $initialTime;

return $this;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum Type: string
case SELECT_USERS = 'users_select';
case PLAIN_TEXT_INPUT = 'plain_text_input';
case TIMEPICKER = 'timepicker';
case NUMBER_INPUT = 'number_input';

// Parts (aka Composition Objects)
case CONFIRM = 'confirm';
Expand Down Expand Up @@ -90,6 +91,7 @@ enum Type: string
Elements\RadioButtons::class => self::RADIO_BUTTONS,
Elements\PlainTextInput::class => self::PLAIN_TEXT_INPUT,
Elements\TimePicker::class => self::TIMEPICKER,
Elements\NumberInput::class => self::NUMBER_INPUT,

// Menus
Elements\OverflowMenu::class => self::OVERFLOW_MENU,
Expand Down
21 changes: 21 additions & 0 deletions tests/Functional/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace SlackPhp\BlockKit\Tests\Functional;

use SlackPhp\BlockKit\Blocks\Input;
use SlackPhp\BlockKit\Blocks\Virtual\VirtualBlock;
use SlackPhp\BlockKit\Collections\ComponentCollection;
use SlackPhp\BlockKit\Component;
use SlackPhp\BlockKit\Elements\NumberInput;
use SlackPhp\BlockKit\Surfaces\Modal;
use SlackPhp\BlockKit\Type;
use SlackPhp\BlockKit\Kit;
use SlackPhp\BlockKit\PrivateMetadata;
Expand Down Expand Up @@ -187,4 +190,22 @@ public function testThatAllKitComponentMethodsReturnComponents(): void
}
}
}

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)
)
);
$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']);
}
}

0 comments on commit 9d1f472

Please sign in to comment.