Skip to content

Commit

Permalink
New Block Types (#75)
Browse files Browse the repository at this point in the history
* Add Timepicker element

* Restrict plain_text_input to input block types

* Support plain_text_input dispatch config

* Additional select blocks and improved block hierarchy

* Add unit tests for new block types

* Rename existing Select classes to StaticSelect
  • Loading branch information
cmbuckley committed Oct 17, 2021
1 parent eec14a3 commit 4028c58
Show file tree
Hide file tree
Showing 45 changed files with 3,297 additions and 670 deletions.
31 changes: 20 additions & 11 deletions src/BlockElement.php
Expand Up @@ -18,19 +18,28 @@ abstract class BlockElement extends Payload
* @var array
*/
protected static $validFor = [
'button' => ['Button', ['section', 'actions']],
'checkboxes' => ['Checkboxes', ['section', 'actions', 'input']],
'datepicker' => ['DatePicker', ['section', 'actions', 'input']],
'image' => ['Image', ['section', 'context']],
'multi_static_select' => ['MultiSelect', ['section', 'input']],
'overflow' => ['Overflow', ['section', 'actions']],
'plain_text_input' => ['TextInput', ['section', 'actions', 'input']],
'radio_buttons' => ['RadioButtons', ['section', 'actions', 'input']],
'static_select' => ['Select', ['section', 'actions', 'input']],
'button' => ['Button', ['section', 'actions']],
'checkboxes' => ['Checkboxes', ['section', 'actions', 'input']],
'datepicker' => ['DatePicker', ['section', 'actions', 'input']],
'timepicker' => ['Timepicker', ['section', 'actions', 'input']],
'image' => ['Image', ['section', 'context']],
'multi_static_select' => ['MultiStaticSelect', ['section', 'input']],
'multi_external_select' => ['MultiExternalSelect', ['section', 'input']],
'multi_users_select' => ['MultiUsersSelect', ['section', 'input']],
'multi_conversations_select' => ['MultiConversationsSelect', ['section', 'input']],
'multi_channels_select' => ['MultiChannelsSelect', ['section', 'input']],
'overflow' => ['Overflow', ['section', 'actions']],
'plain_text_input' => ['TextInput', ['input']],
'radio_buttons' => ['RadioButtons', ['section', 'actions', 'input']],
'static_select' => ['StaticSelect', ['section', 'actions', 'input']],
'external_select' => ['ExternalSelect', ['section', 'actions', 'input']],
'users_select' => ['UsersSelect', ['section', 'actions', 'input']],
'conversations_select' => ['ConversationsSelect', ['section', 'actions', 'input']],
'channels_select' => ['ChannelsSelect', ['section', 'actions', 'input']],

// Context Block allows a Text object to be used directly, so need to map types here
'plain_text' => ['Text', ['context']],
'mrkdwn' => ['Text', ['context']],
'plain_text' => ['Text', ['context']],
'mrkdwn' => ['Text', ['context']],
];

/**
Expand Down
9 changes: 9 additions & 0 deletions src/BlockElement/AbstractDynamicSelect.php
@@ -0,0 +1,9 @@
<?php
namespace Maknz\Slack\BlockElement;

use Maknz\Slack\PlaceholderTrait;

abstract class AbstractDynamicSelect extends Confirmable
{
use PlaceholderTrait;
}
177 changes: 4 additions & 173 deletions src/BlockElement/AbstractSelect.php
@@ -1,178 +1,9 @@
<?php
namespace Maknz\Slack\BlockElement;

use InvalidArgumentException;
use Maknz\Slack\Object\OptionGroup;

abstract class AbstractSelect extends Options
/**
* @deprecated This class is deprecated in favour of AbstractStaticSelect since 2.2.0 and will be removed in a future release.
*/
abstract class AbstractSelect extends AbstractStaticSelect
{
/**
* Select placeholder.
*
* @var \Maknz\Slack\BlockElement\Text
*/
protected $placeholder;

/**
* Select option groups.
*
* @var \Maknz\Slack\Object\OptionGroup[]
*/
protected $option_groups = [];

/**
* Get the placeholder.
*
* @return \Maknz\Slack\BlockElement\Text
*/
public function getPlaceholder()
{
return $this->placeholder;
}

/**
* Set the placeholder.
*
* @param mixed $placeholder
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function setPlaceholder($placeholder)
{
$this->placeholder = Text::create($placeholder, Text::TYPE_PLAIN);

return $this;
}

/**
* Set options available within the block.
*
* @param array $options
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function setOptions(array $options)
{
$this->clearOptionGroups();

return parent::setOptions($options);
}

/**
* Add an option to the block.
*
* @param mixed $option
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function addOption($option)
{
parent::addOption($option);
$this->clearOptionGroups();

return $this;
}

/**
* Get the option groups.
*
* @return \Maknz\Slack\Object\OptionGroup[]
*/
public function getOptionGroups()
{
return $this->option_groups;
}

/**
* Get the option groups in array format.
*
* @return array
*/
public function getOptionGroupsAsArrays()
{
$groups = [];

foreach ($this->getOptionGroups() as $group) {
$groups[] = $group->toArray();
}

return $groups;
}

/**
* Set the option groups.
*
* @param array $groups
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function setOptionGroups(array $groups)
{
$this->clearOptions();
$this->clearOptionGroups();

foreach ($groups as $group) {
$this->addOptionGroup($group);
}

return $this;
}

/**
* Clear option groups in the block.
*
* @return $this
*/
public function clearOptionGroups()
{
$this->option_groups = [];

return $this;
}

/**
* Clear options and option groups.
*
* @return $this
*/
public function clearAllOptions()
{
$this->clearOptions();
$this->clearOptionGroups();

return $this;
}

/**
* Add an option group to the block.
*
* @param mixed $group
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function addOptionGroup($group)
{
if (is_array($group)) {
$group = new OptionGroup($group);
}

if ($group instanceof OptionGroup) {
$this->clearOptions();
$this->option_groups[] = $group;

return $this;
}

throw new InvalidArgumentException('The option group must be an instance of '.OptionGroup::class.' or a keyed array');
}
}
148 changes: 148 additions & 0 deletions src/BlockElement/AbstractStaticSelect.php
@@ -0,0 +1,148 @@
<?php
namespace Maknz\Slack\BlockElement;

use InvalidArgumentException;
use Maknz\Slack\Object\OptionGroup;
use Maknz\Slack\PlaceholderTrait;

abstract class AbstractStaticSelect extends Options
{
use PlaceholderTrait;

/**
* Select option groups.
*
* @var \Maknz\Slack\Object\OptionGroup[]
*/
protected $option_groups = [];

/**
* Set options available within the block.
*
* @param array $options
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function setOptions(array $options)
{
$this->clearOptionGroups();

return parent::setOptions($options);
}

/**
* Add an option to the block.
*
* @param mixed $option
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function addOption($option)
{
parent::addOption($option);
$this->clearOptionGroups();

return $this;
}

/**
* Get the option groups.
*
* @return \Maknz\Slack\Object\OptionGroup[]
*/
public function getOptionGroups()
{
return $this->option_groups;
}

/**
* Get the option groups in array format.
*
* @return array
*/
public function getOptionGroupsAsArrays()
{
$groups = [];

foreach ($this->getOptionGroups() as $group) {
$groups[] = $group->toArray();
}

return $groups;
}

/**
* Set the option groups.
*
* @param array $groups
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function setOptionGroups(array $groups)
{
$this->clearOptions();
$this->clearOptionGroups();

foreach ($groups as $group) {
$this->addOptionGroup($group);
}

return $this;
}

/**
* Clear option groups in the block.
*
* @return $this
*/
public function clearOptionGroups()
{
$this->option_groups = [];

return $this;
}

/**
* Clear options and option groups.
*
* @return $this
*/
public function clearAllOptions()
{
$this->clearOptions();
$this->clearOptionGroups();

return $this;
}

/**
* Add an option group to the block.
*
* @param mixed $group
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function addOptionGroup($group)
{
if (is_array($group)) {
$group = new OptionGroup($group);
}

if ($group instanceof OptionGroup) {
$this->clearOptions();
$this->option_groups[] = $group;

return $this;
}

throw new InvalidArgumentException('The option group must be an instance of '.OptionGroup::class.' or a keyed array');
}
}

0 comments on commit 4028c58

Please sign in to comment.