Skip to content

Commit

Permalink
implement overflow menu (#11)
Browse files Browse the repository at this point in the history
By: Andreas Hofmann <andreas.hofmann@check24.de>
  • Loading branch information
Andi1986 authored Sep 29, 2020
1 parent 74cdb48 commit 6828ae7
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ The following are supported elements from the Block Kit documentation:
| Surface | Message ||
| Surface | Model ||
| Block | Actions ||
| Block | Checkboxes | |
| Block | Checkboxes | |
| Block | Context ||
| Block | Divider ||
| Block | File ||
Expand All @@ -201,9 +201,9 @@ The following are supported elements from the Block Kit documentation:
| Input | Button | ✅️ |
| Input | Date Picker ||
| Input | Multi-select Menus | ✅✅✅✅✅ |
| Input | Overflow Menu | |
| Input | Overflow Menu | |
| Input | Plain Text Input ||
| Input | Radio Buttons | |
| Input | Radio Buttons | |
| Input | Select Menus | ✅✅✅✅✅ |
| Partial | Confirm Dialog ||
| Partial | Mrkdwn Text ||
Expand Down
8 changes: 8 additions & 0 deletions src/Blocks/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public function newCheckboxes(?string $actionId = null): Inputs\Checkboxes
return $action;
}

public function newOverflowMenu(?string $actionId = null): Inputs\OverflowMenu
{
$action = new Inputs\OverflowMenu($actionId);
$this->add($action);

return $action;
}

public function validate(): void
{
if (empty($this->elements)) {
Expand Down
8 changes: 8 additions & 0 deletions src/Blocks/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ public function newCheckboxesAccessory(?string $actionId = null): Inputs\Checkbo
return $action;
}

public function newOverflowMenuAccessory(?string $actionId = null): Inputs\OverflowMenu
{
$action = new Inputs\OverflowMenu($actionId);
$this->setAccessory($action);

return $action;
}

public function validate(): void
{
if (empty($this->text) && empty($this->fields)) {
Expand Down
51 changes: 51 additions & 0 deletions src/Inputs/OverflowMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Jeremeamia\Slack\BlockKit\Inputs;

use Jeremeamia\Slack\BlockKit\Exception;
use Jeremeamia\Slack\BlockKit\Partials\HasOptions;

class OverflowMenu extends InputElement
{
use HasConfirm;
use HasOptions;

private const MIN_OPTIONS = 2;
private const MAX_OPTIONS = 5;

public function validate(): void
{

$this->validateOptions();

if (!empty($this->confirm)) {
$this->confirm->validate();
}

if (count($this->options) > self::MAX_OPTIONS) {
throw new Exception('Option Size cannot exceed %d', [self::MAX_OPTIONS]);
}

if (count($this->options) < self::MIN_OPTIONS) {
throw new Exception('Option Size must be at least %d', [self::MIN_OPTIONS]);
}
}

/**
* @return array
*/
public function toArray(): array
{

$data = parent::toArray();
$data += $this->getOptionsAsArray();

if (!empty($this->confirm)) {
$data['confirm'] = $this->confirm->toArray();
}

return $data;
}
}
10 changes: 5 additions & 5 deletions src/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class Type
public const CHECKBOXES = 'checkboxes';
public const DATEPICKER = 'datepicker';
public const TEXT_INPUT = 'plain_text_input';
// public const OVERFLOW_MENU = 'overflow'; // Not yet supported.
public const OVERFLOW_MENU = 'overflow';
public const RADIO_BUTTONS = 'radio_buttons';

// Select Menus
Expand Down Expand Up @@ -86,7 +86,7 @@ abstract class Type
self::MULTI_SELECT_MENU_EXTERNAL,
self::MULTI_SELECT_MENU_STATIC,
self::MULTI_SELECT_MENU_USERS,
// self::OVERFLOW_MENU, // Not yet supported.
self::OVERFLOW_MENU,
self::RADIO_BUTTONS,
self::SELECT_MENU_CHANNELS,
self::SELECT_MENU_CONVERSATIONS,
Expand All @@ -100,7 +100,7 @@ abstract class Type
self::BUTTON,
self::CHECKBOXES,
self::DATEPICKER,
// self::OVERFLOW_MENU, // Not yet supported.
self::OVERFLOW_MENU,
self::RADIO_BUTTONS,
self::SELECT_MENU_CHANNELS,
self::SELECT_MENU_CONVERSATIONS,
Expand Down Expand Up @@ -151,9 +151,9 @@ abstract class Type

// Inputs
Inputs\Button::class => self::BUTTON,
Inputs\Checkboxes::class => self::CHECKBOXES, // Not yet supported.
Inputs\Checkboxes::class => self::CHECKBOXES,
Inputs\DatePicker::class => self::DATEPICKER,
// Inputs\OverflowMenu::class => self::OVERFLOW_MENU, // Not yet supported.
Inputs\OverflowMenu::class => self::OVERFLOW_MENU,
Inputs\RadioButtons::class => self::RADIO_BUTTONS,
Inputs\TextInput::class => self::TEXT_INPUT,

Expand Down
93 changes: 93 additions & 0 deletions tests/Inputs/OverflowMenuTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Jeremeamia\Slack\BlockKit\Tests\Inputs;

use Jeremeamia\Slack\BlockKit\Exception;
use Jeremeamia\Slack\BlockKit\Inputs\OverflowMenu;
use Jeremeamia\Slack\BlockKit\Partials\Confirm;
use Jeremeamia\Slack\BlockKit\Tests\TestCase;
use Jeremeamia\Slack\BlockKit\Type;

/**
* @covers \Jeremeamia\Slack\BlockKit\Inputs\OverflowMenu
*/
class OverflowMenuTest extends TestCase
{
public function testOverflowMenuWithConfirm()
{
$input = (new OverflowMenu('overflow-identifier'))
->option('foo', 'foo')
->option('bar', 'bar')
->option('foobar', 'foobar')
->setConfirm(new Confirm('Choose', 'Do you really want to choose this?', 'Yes choose'));

$this->assertJsonData([
'type' => Type::OVERFLOW_MENU,
'action_id' => 'overflow-identifier',
'options' => [
[
'text' => [
'type' => 'plain_text',
'text' => 'foo',
],
'value' => 'foo',
],
[
'text' => [
'type' => 'plain_text',
'text' => 'bar',
],
'value' => 'bar',
],
[
'text' => [
'type' => 'plain_text',
'text' => 'foobar',
],
'value' => 'foobar',
],
],
'confirm' => [
'title' => [
'type' => 'plain_text',
'text' => 'Choose',
],
'text' => [
'type' => 'mrkdwn',
'text' => 'Do you really want to choose this?',
],
'confirm' => [
'type' => 'plain_text',
'text' => 'Yes choose',
],
'deny' => [
'type' => 'plain_text',
'text' => 'Cancel',
],
]
], $input);
}

public function testTooManyOptions()
{
$this->expectException(Exception::class);
$input = (new OverflowMenu())
->option('foo', 'foo')
->option('foo', 'foo')
->option('foo', 'foo')
->option('foo', 'foo')
->option('foo', 'foo')
->option('foo', 'foo');
$input->validate();
}

public function testTooFewOptions()
{
$this->expectException(Exception::class);
$input = (new OverflowMenu())
->option('foo', 'foo');
$input->validate();
}
}
9 changes: 9 additions & 0 deletions tests/Inputs/RadioButtonsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,13 @@ public function testNoOptions()
$input = new RadioButtons();
$input->validate();
}

public function testTooManyInitialOptions()
{
$this->expectException(Exception::class);
$input = (new RadioButtons())
->option('foo', 'foo', true)
->option('foo', 'foo', true);
$input->validate();
}
}
9 changes: 8 additions & 1 deletion tests/manual/menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Jeremeamia\Slack\BlockKit\Partials\Confirm;
use Jeremeamia\Slack\BlockKit\Slack;

require __DIR__ . '/../../vendor/autoload.php';
Expand Down Expand Up @@ -63,7 +64,13 @@
]
])
->initialOptions(['b' => 'l2', 'c' => 'l3']);

$msg->newSection('b5')
->mrkdwnText('Select from Overflow Menu')
->newOverflowMenuAccessory('m6')
->option('foo', 'foo')
->option('bar', 'bar')
->option('foobar', 'foobar')
->setConfirm(new Confirm('Choose', 'Do you really want to choose this?', 'Yes choose'));
//echo Slack::newRenderer()->forJson()->render($msg) . "\n";
echo Slack::newRenderer()->forKitBuilder()->render($msg) . "\n";
// echo Slack::newRenderer()->forCli()->render($msg) . "\n";

0 comments on commit 6828ae7

Please sign in to comment.