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 3 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
29 changes: 28 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 $lockAutoDefault = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LockAutoDefault looks weird to me.


/** @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->lockAutoDefault = 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 Down Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions tests/Forms.Latte/expected/FormMacros.forms.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
<label for="frm-username" title="hello"> <input type="text" name="username" size="10" class="control-class" id="frm-username" title="Hello"> </label>
error
<label for="frm-select"></label>
<select name="select" size="10" id="frm-select" title="Hello"><option value="m">male</option><option value="f">female</option></select>
<select name="select" size="10" id="frm-select" title="Hello"><option value="m" selected>male</option><option value="f">female</option></select>



<br>

<label for="frm-select" title="hello"> <select name="select" size="10" id="frm-select" title="Hello"><option value="m">male</option><option value="f">female</option></select> </label>
<label for="frm-select" title="hello"> <select name="select" size="10" id="frm-select" title="Hello"><option value="m" selected>male</option><option value="f">female</option></select> </label>

<label for="frm-area"></label>
<textarea name="area" id="frm-area" title="Hello" size="10">one&lt;two</textarea>
Expand Down 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