Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Selectbox auto default value #183

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Bridges/FormsLatte/FormMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public function macroInput(MacroNode $node, PhpWriter $writer)
$node->replaced = true;
$name = array_shift($words);
return $writer->write(
($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; echo $_input' : 'echo end($this->global->formsStack)[%0.word]')
. '->%1.raw'
. ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : '')
($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; ' : 'echo end($this->global->formsStack)[%0.word]; ')
. ($node->tokenizer->isNext() ? 'foreach(%node.array as $_k => $_v) {$_input->setHtmlAttribute($_k, $_v);} ' : '')
. 'echo $_input->%1.raw'
. " /* line $node->startLine */",
$name,
$words ? 'getControlPart(' . implode(', ', array_map([$writer, 'formatWord'], $words)) . ')' : 'getControl()'
Expand Down
38 changes: 37 additions & 1 deletion src/Forms/Controls/SelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class SelectBox extends ChoiceControl
/** @var mixed */
private $prompt = false;

/** @var bool */
private $autoDefault = false;

/** @var array */
private $optionAttributes = [];

Expand All @@ -47,6 +50,7 @@ public function __construct($label = null, array $items = null)
public function setPrompt($prompt)
{
$this->prompt = $prompt;
$this->setDefaultValueAuto(null, true);
return $this;
}

Expand All @@ -61,6 +65,13 @@ public function getPrompt()
}


public function setDefaultValue($value)
{
$this->autoDefault = true;
return parent::setDefaultValue($value);
}


/**
* Sets options and option groups from which to choose.
* @return static
Expand All @@ -82,7 +93,13 @@ public function setItems(array $items, bool $useKeys = true)
$items = $res;
}
$this->options = $items;
return parent::setItems(Nette\Utils\Arrays::flatten($items, true));
parent::setItems(Nette\Utils\Arrays::flatten($items, true));

if ($this->prompt === false && $this->items) {
reset($this->items);
$this->setDefaultValueAuto(key($this->items));
}
return $this;
}


Expand All @@ -106,6 +123,15 @@ public function getControl(): Nette\Utils\Html
}


public function setHtmlAttribute(string $name, $value = true)
{
if ($name === 'size' && $value > 1) {
$this->setDefaultValueAuto(null, true);
}
return parent::setHtmlAttribute($name, $value);
}


/**
* @return static
*/
Expand All @@ -124,4 +150,14 @@ public function isOk(): bool
|| !$this->options
|| $this->control->size > 1;
}


private function setDefaultValueAuto($value, bool $autoDefault = false)
{
if ($this->autoDefault === false) {
$this->setDefaultValue($value);
$this->autoDefault = $autoDefault;
}
return $this;
}
}
4 changes: 2 additions & 2 deletions tests/Forms.Latte/expected/FormMacros.forms.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@
</FORM>


<select name="select" id="frm-select"><option value="m">male</option><option value="f">female</option></select>
<select name="select" id="frm-select"><option value="m" selected>male</option><option value="f">female</option></select>


<textarea title="10" name="area" id="frm-area">one&lt;two</textarea>


<select name="select" id="frm-select"><option value="m">male</option><option value="f">female</option></select>
<select name="select" id="frm-select"><option value="m" selected>male</option><option value="f">female</option></select>
<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->
</form>
47 changes: 47 additions & 0 deletions tests/Forms/Controls.SelectBox.defaultValue.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Test: Nette\Forms\Controls\SelectBox::setDefaultValueAuto()
*/

declare(strict_types=1);

use Nette\Forms\Form;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$form = new Form;
$select = $form->addSelect('foo', null, ['bar' => 'Bar', 'foo' => 'Foo']);
Assert::same('bar', $select->getValue());

$select->setPrompt('Baz');
Assert::null($select->getValue());

$select->setDefaultValue('foo');
Assert::same('foo', $select->getValue());


$form = new Form;
$select = $form->addSelect('foo')
->setPrompt('Baz')
->setItems(['bar' => 'Bar', 'foo' => 'Foo']);
Assert::null($select->getValue());


$form = new Form;
$select = $form->addSelect('foo')
->setItems(['bar' => 'Bar', 'foo' => 'Foo'])
->setDefaultValue('foo')
->setPrompt('Baz');
Assert::same('foo', $select->getValue());


$form = new Form;
$select = $form->addSelect('foo')
->setPrompt('Baz')
->setItems(['bar' => 'Bar', 'foo' => 'Foo'])
->setDefaultValue('foo');
Assert::same('foo', $select->getValue());
2 changes: 1 addition & 1 deletion tests/Forms/Controls.SelectBox.isOk.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require __DIR__ . '/../bootstrap.php';
$form = new Form;
$select = $form->addSelect('foo', null, ['bar' => 'Bar']);

Assert::false($select->isOk());
Assert::true($select->isOk());

$select->setDisabled(true);
Assert::true($select->isOk());
Expand Down
6 changes: 3 additions & 3 deletions tests/Forms/Controls.SelectBox.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test(function () {
Assert::same('<label for="frm-list">Another label</label>', (string) $input->getLabel('Another label'));

Assert::type(Html::class, $input->getControl());
Assert::same('<select name="list" id="frm-list"><option value="a">First</option><option value="0">Second</option></select>', (string) $input->getControl());
Assert::same('<select name="list" id="frm-list"><option value="a" selected>First</option><option value="0">Second</option></select>', (string) $input->getControl());
});


Expand Down Expand Up @@ -96,7 +96,7 @@ test(function () { // validation rules
0 => 'Second',
])->setRequired('required');

Assert::same('<select name="list" id="frm-list" required data-nette-rules=\'[{"op":":filled","msg":"required"}]\'><option value="a">First</option><option value="0">Second</option></select>', (string) $input->getControl());
Assert::same('<select name="list" id="frm-list" required data-nette-rules=\'[{"op":":filled","msg":"required"}]\'><option value="a" selected>First</option><option value="0">Second</option></select>', (string) $input->getControl());
});


Expand All @@ -108,7 +108,7 @@ test(function () { // container
0 => 'Second',
]);

Assert::same('<select name="container[list]" id="frm-container-list"><option value="a">First</option><option value="0">Second</option></select>', (string) $input->getControl());
Assert::same('<select name="container[list]" id="frm-container-list"><option value="a" selected>First</option><option value="0">Second</option></select>', (string) $input->getControl());
});


Expand Down