From 1472bfb69cbeaf52b5c1cd504a216d4850dd04d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Wed, 13 Jun 2018 12:15:23 +0200 Subject: [PATCH 1/5] upgrade SelectBox --- src/Forms/Controls/SelectBox.php | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Forms/Controls/SelectBox.php b/src/Forms/Controls/SelectBox.php index ebcdfb1c0..5aaa03d54 100644 --- a/src/Forms/Controls/SelectBox.php +++ b/src/Forms/Controls/SelectBox.php @@ -26,6 +26,9 @@ class SelectBox extends ChoiceControl /** @var mixed */ private $prompt = false; + /** @var bool */ + private $lockAutoDefault = false; + /** @var array */ private $optionAttributes = []; @@ -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; } @@ -61,6 +65,13 @@ public function getPrompt() } + public function setDefaultValue($value) + { + $this->lockAutoDefault = true; + return parent::setDefaultValue($value); + } + + /** * Sets options and option groups from which to choose. * @return static @@ -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; } @@ -124,4 +141,14 @@ public function isOk(): bool || !$this->options || $this->control->size > 1; } + + + private function setDefaultValueAuto($value, $lock = false) + { + if ($this->lockAutoDefault === false) { + $this->setDefaultValue($value); + $this->lockAutoDefault = $lock; + } + return $this; + } } From 369659d6b3ca1f9f36d8fb88f03c21f3664726a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Wed, 13 Jun 2018 12:15:39 +0200 Subject: [PATCH 2/5] fix exists tests --- tests/Forms.Latte/expected/FormMacros.forms.html | 8 ++++---- tests/Forms/Controls.SelectBox.isOk.phpt | 2 +- tests/Forms/Controls.SelectBox.render.phpt | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Forms.Latte/expected/FormMacros.forms.html b/tests/Forms.Latte/expected/FormMacros.forms.html index 0557fa5ed..2041730dd 100644 --- a/tests/Forms.Latte/expected/FormMacros.forms.html +++ b/tests/Forms.Latte/expected/FormMacros.forms.html @@ -18,13 +18,13 @@ error - +
- + @@ -108,12 +108,12 @@ - + - + diff --git a/tests/Forms/Controls.SelectBox.isOk.phpt b/tests/Forms/Controls.SelectBox.isOk.phpt index 094c69368..ebdc274b1 100644 --- a/tests/Forms/Controls.SelectBox.isOk.phpt +++ b/tests/Forms/Controls.SelectBox.isOk.phpt @@ -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()); diff --git a/tests/Forms/Controls.SelectBox.render.phpt b/tests/Forms/Controls.SelectBox.render.phpt index 6c8a9e11e..86b542e32 100644 --- a/tests/Forms/Controls.SelectBox.render.phpt +++ b/tests/Forms/Controls.SelectBox.render.phpt @@ -35,7 +35,7 @@ test(function () { Assert::same('', (string) $input->getLabel('Another label')); Assert::type(Html::class, $input->getControl()); - Assert::same('', (string) $input->getControl()); + Assert::same('', (string) $input->getControl()); }); @@ -96,7 +96,7 @@ test(function () { // validation rules 0 => 'Second', ])->setRequired('required'); - Assert::same('', (string) $input->getControl()); + Assert::same('', (string) $input->getControl()); }); @@ -108,7 +108,7 @@ test(function () { // container 0 => 'Second', ]); - Assert::same('', (string) $input->getControl()); + Assert::same('', (string) $input->getControl()); }); From 7308c8931eb88ea4f05c9e138f75345f9b26bde7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Wed, 13 Jun 2018 12:32:50 +0200 Subject: [PATCH 3/5] test --- .../Controls.SelectBox.defaultValue.phpt | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/Forms/Controls.SelectBox.defaultValue.phpt diff --git a/tests/Forms/Controls.SelectBox.defaultValue.phpt b/tests/Forms/Controls.SelectBox.defaultValue.phpt new file mode 100644 index 000000000..1832ebbbc --- /dev/null +++ b/tests/Forms/Controls.SelectBox.defaultValue.phpt @@ -0,0 +1,47 @@ +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()); From b076a51307557d15d09168691581b9402299ed78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Thu, 14 Jun 2018 06:01:30 +0200 Subject: [PATCH 4/5] rename property lockAutoDefault --- src/Forms/Controls/SelectBox.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Forms/Controls/SelectBox.php b/src/Forms/Controls/SelectBox.php index 5aaa03d54..49bd39ea0 100644 --- a/src/Forms/Controls/SelectBox.php +++ b/src/Forms/Controls/SelectBox.php @@ -27,7 +27,7 @@ class SelectBox extends ChoiceControl private $prompt = false; /** @var bool */ - private $lockAutoDefault = false; + private $autoDefault = false; /** @var array */ private $optionAttributes = []; @@ -67,7 +67,7 @@ public function getPrompt() public function setDefaultValue($value) { - $this->lockAutoDefault = true; + $this->autoDefault = true; return parent::setDefaultValue($value); } @@ -143,11 +143,11 @@ public function isOk(): bool } - private function setDefaultValueAuto($value, $lock = false) + private function setDefaultValueAuto($value, bool $autoDefault = false) { - if ($this->lockAutoDefault === false) { + if ($this->autoDefault === false) { $this->setDefaultValue($value); - $this->lockAutoDefault = $lock; + $this->autoDefault = $autoDefault; } return $this; } From 0a3c5e322453e736827b22adf8ebbde688444aed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Mat=C4=9Bj=C4=8Dek?= Date: Fri, 15 Jun 2018 07:20:34 +0200 Subject: [PATCH 5/5] change render type --- src/Bridges/FormsLatte/FormMacros.php | 6 +++--- src/Forms/Controls/SelectBox.php | 9 +++++++++ tests/Forms.Latte/expected/FormMacros.forms.html | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Bridges/FormsLatte/FormMacros.php b/src/Bridges/FormsLatte/FormMacros.php index 07bba735c..173e06760 100644 --- a/src/Bridges/FormsLatte/FormMacros.php +++ b/src/Bridges/FormsLatte/FormMacros.php @@ -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()' diff --git a/src/Forms/Controls/SelectBox.php b/src/Forms/Controls/SelectBox.php index 49bd39ea0..4b80a3fdd 100644 --- a/src/Forms/Controls/SelectBox.php +++ b/src/Forms/Controls/SelectBox.php @@ -123,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 */ diff --git a/tests/Forms.Latte/expected/FormMacros.forms.html b/tests/Forms.Latte/expected/FormMacros.forms.html index 2041730dd..746d2e3bd 100644 --- a/tests/Forms.Latte/expected/FormMacros.forms.html +++ b/tests/Forms.Latte/expected/FormMacros.forms.html @@ -18,13 +18,13 @@ error - +
- +