Skip to content

Commit

Permalink
Add options example (#28)
Browse files Browse the repository at this point in the history
* Add options example
Fix date formatting

* loosely check as the option could be a string or an int

* Prefer strict typing

* Cast to string

* Update enum data handler

* Move expensive assoc out

* revert date datahandler
  • Loading branch information
MrEssex authored Apr 17, 2023
1 parent 46a3f33 commit d31f4f8
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 14 deletions.
6 changes: 6 additions & 0 deletions demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class DemoForm extends Form
/** @var \Packaged\Form\DataHandlers\MultiLineTextDataHandler */
public $comments;

/** @var EnumDataHandler */
public $opions;

protected function _initDataHandlers()
{
$this->name = TextDataHandler::i()->addValidator(new StringValidator(2, 20));
Expand Down Expand Up @@ -104,12 +107,15 @@ protected function _initDataHandlers()
->setGuidance("Required")
->addValidator(new RequiredValidator());
//$this->setHandlerDecorator(new InputOnlyDataHandlerDecorator(), 'agree');

$this->opions = EnumDataHandler::i();
}

protected function _configureDataHandlers()
{
parent::_configureDataHandlers();
$this->confirmPassword->addValidator(new ConfirmationValidator($this->password->getName()));
$this->opions->setOptions(['test1' => 'Test 1', 'test2' => 'Test 2', 'test3' => 'Test 3']);
}
}

Expand Down
49 changes: 35 additions & 14 deletions php/src/DataHandlers/EnumDataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use Packaged\Glimpse\Tags\Form\Label;
use Packaged\Glimpse\Tags\Form\Option;
use Packaged\Glimpse\Tags\Form\Select;
use Packaged\Helpers\Arrays;
use Packaged\Helpers\Strings;
use Packaged\Helpers\ValueAs;
use Packaged\Ui\Html\HtmlElement;

class EnumDataHandler extends AbstractDataHandler
{
const INPUT_STYLE_COMBINED = 'single';
const INPUT_STYLE_SPLIT = 'split';
const INPUT_STYLE_SPLIT = 'split';

protected $_options = [];
protected $_inputStyle = self::INPUT_STYLE_COMBINED;
Expand Down Expand Up @@ -91,33 +93,52 @@ protected function _generateSplitInput($key, $value, $selected = false): HtmlEle

protected function _isSelectedOption($option): bool
{
return $option === $this->getValue();
return ValueAs::string($option) === $this->getValue();
}

protected function _generateInput(): HtmlElement
{
$isAssocArray = Arrays::isAssoc($this->getOptions());

if($this->_inputStyle === self::INPUT_STYLE_SPLIT)
{
$ele = new MultiInputContainer();
foreach($this->getOptions() as $optK => $optV)
if($isAssocArray)
{
$ele->addInput($this->_generateSplitInput($optK, $optV, $this->_isSelectedOption($optK)));
foreach($this->getOptions() as $optK => $optV)
{
$ele->addInput($this->_generateSplitInput($optK, $optV, $this->_isSelectedOption($optK)));
}
}
else
{
foreach($this->getOptions() as $optV)
{
$ele->addInput($this->_generateSplitInput($optV, $optV, $this->_isSelectedOption($optV)));
}
}

return $ele;
}

$options = [];
foreach($this->getOptions() as $optK => $optV)
$options = Option::collection($this->getOptions());

/** @var Option $option */
foreach($options as $option)
{
$options[] = Option::create($optV)->addAttributes(
array_filter(
[
'value' => $optK,
'selected' => $this->_isSelectedOption($optK),
]
)
);
if($isAssocArray)
{
if($this->_isSelectedOption($option->getAttribute('value')))
{
$option->setAttribute('selected', true);
}
}
else if($this->_isSelectedOption($option->getContent(false)))
{
$option->setAttribute('selected', true);
}
}

$ele = Select::create($options);
$ele->addAttributes(
[
Expand Down
75 changes: 75 additions & 0 deletions php/tests/DataHandlers/EnumDataHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,79 @@ public function testMultiEnum()
$h->getInput()->render()
);
}

public function testNonAssocArray()
{
$h = new EnumDataHandler();
$h->setOptions(['1', '2', '3', '4', '5']);
$h->setValue('1');

self::assertEquals(
'<select><option selected>1</option><option>2</option><option>3</option><option>4</option><option>5</option></select>',
$h->getInput()->render()
);
}

public function testAssocArray()
{
$h = new EnumDataHandler();
$h->setOptions(['1' => 'Banana', '2' => 'Apple']);
$h->setValue('2');

self::assertEquals(
'<select><option value="1">Banana</option><option value="2" selected>Apple</option></select>',
$h->getInput()->render()
);
}

public function testAssocArray2()
{
$h = new EnumDataHandler();
$h->setOptions(['Banana' => 'Banana', 'Apple' => 'Apple']);
$h->setValue('Apple');

self::assertEquals(
'<select><option value="Banana">Banana</option><option value="Apple" selected>Apple</option></select>',
$h->getInput()->render()
);
}

public function testNonAssocArraySplit()
{
$h = new EnumDataHandler();
$h->setOptions(['1', '2', '3']);
$h->setValue('1');
$h->styleSplit();

self::assertMatchesRegularExpression(
'~<div class="p-form__labeled-input"><input type="radio" id="(.+)" value="1" checked /><label for="\1">1</label></div><div class="p-form__labeled-input"><input type="radio" id="(.+)" value="2" /><label for="\2">2</label></div><div class="p-form__labeled-input"><input type="radio" id="(.+)" value="3" /><label for="\3">3</label></div>~',
$h->getInput()->render()
);
}

public function testAssocArraySplit()
{
$h = new EnumDataHandler();
$h->setOptions(['1' => 'Banana', '2' => 'Apple']);
$h->setValue('2');
$h->styleSplit();

self::assertMatchesRegularExpression(
'~<div class="p-form__labeled-input"><input type="radio" id="(.+)" value="1" /><label for="\1">Banana</label></div><div class="p-form__labeled-input"><input type="radio" id="(.+)" value="2" checked /><label for="\2">Apple</label></div>~',
$h->getInput()->render()
);
}

public function testAssocArraySplit2()
{
$h = new EnumDataHandler();
$h->setOptions(['Banana' => 'Banana', 'Apple' => 'Apple']);
$h->setValue('Apple');
$h->styleSplit();

self::assertMatchesRegularExpression(
'~<div class="p-form__labeled-input"><input type="radio" id="(.+)" value="Banana" /><label for="\1">Banana</label></div><div class="p-form__labeled-input"><input type="radio" id="(.+)" value="Apple" checked /><label for="\2">Apple</label></div>~',
$h->getInput()->render()
);
}
}

0 comments on commit d31f4f8

Please sign in to comment.