Skip to content

Commit

Permalink
SelectBox: distinctive prompt key
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 27, 2024
1 parent 3f0635d commit bedb0ea
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/Forms/Controls/SelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SelectBox extends ChoiceControl

/** @deprecated use SelectBox::Valid */
public const VALID = self::Valid;
private const PromptKey = "!\x01prompt\x01%";

/** of option / optgroup */
private array $options = [];
Expand Down Expand Up @@ -95,10 +96,10 @@ public function getControl(): Nette\Utils\Html
$attrs = $this->optionAttributes;
$attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : [];
if ($this->prompt !== false) {
$items[''] = $this->translate($this->prompt);
$items[self::PromptKey] = $this->translate($this->prompt);
if ($this->isRequired()) {
$attrs['disabled:'][''] = $attrs['hidden:'][''] = true;
$selected ??= ''; // disabled & selected for Safari, hidden for other browsers
$attrs['disabled:'][self::PromptKey] = $attrs['hidden:'][self::PromptKey] = true;
$selected ??= self::PromptKey; // disabled & selected for Safari, hidden for other browsers
}
}
foreach ($this->options as $key => $value) {
Expand Down
12 changes: 6 additions & 6 deletions tests/Forms/Controls.SelectBox.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ test('prompt', function () {
0 => 'Second',
])->setPrompt('prompt');

Assert::same('<select name="list" id="frm-list"><option value="">prompt</option><option value="a">First</option><option value="0">Second</option></select>', (string) $input->getControl());
Assert::same('<select name="list" id="frm-list"><option value="' . "!\x01prompt\x01%" . '">prompt</option><option value="a">First</option><option value="0">Second</option></select>', (string) $input->getControl());

$input->setValue(0);

Assert::same('<select name="list" id="frm-list"><option value="">prompt</option><option value="a">First</option><option value="0" selected>Second</option></select>', (string) $input->getControl());
Assert::same('<select name="list" id="frm-list"><option value="' . "!\x01prompt\x01%" . '">prompt</option><option value="a">First</option><option value="0" selected>Second</option></select>', (string) $input->getControl());
});


Expand All @@ -83,11 +83,11 @@ test('prompt + required', function () {
0 => 'Second',
])->setPrompt('prompt')->setRequired();

Assert::same('<select name="list" id="frm-list" required data-nette-rules=\'[{"op":":filled","msg":"This field is required."}]\'><option value="" disabled hidden selected>prompt</option><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":"This field is required."}]\'><option value="' . "!\x01prompt\x01%" . '" disabled hidden selected>prompt</option><option value="a">First</option><option value="0">Second</option></select>', (string) $input->getControl());

$input->setValue(0);

Assert::same('<select name="list" id="frm-list" required data-nette-rules=\'[{"op":":filled","msg":"This field is required."}]\'><option value="" disabled hidden>prompt</option><option value="a">First</option><option value="0" selected>Second</option></select>', (string) $input->getControl());
Assert::same('<select name="list" id="frm-list" required data-nette-rules=\'[{"op":":filled","msg":"This field is required."}]\'><option value="' . "!\x01prompt\x01%" . '" disabled hidden>prompt</option><option value="a">First</option><option value="0" selected>Second</option></select>', (string) $input->getControl());
});


Expand All @@ -101,7 +101,7 @@ test('translator & groups', function () {

Assert::same('<label for="frm-list">Label</label>', (string) $input->getLabel());
Assert::same('<label for="frm-list">Another label</label>', (string) $input->getLabel('Another label'));
Assert::same('<select name="list" id="frm-list"><option value="">PROMPT</option><option value="a">FIRST</option><optgroup label="GROUP"><option value="0">SECOND</option><option value="1">THIRD</option></optgroup></select>', (string) $input->getControl());
Assert::same('<select name="list" id="frm-list"><option value="' . "!\x01prompt\x01%" . '">PROMPT</option><option value="a">FIRST</option><optgroup label="GROUP"><option value="0">SECOND</option><option value="1">THIRD</option></optgroup></select>', (string) $input->getControl());
});


Expand All @@ -115,7 +115,7 @@ test('Html with translator & groups', function () {

Assert::same('<label for="frm-list"><b>Label</b></label>', (string) $input->getLabel());
Assert::same('<label for="frm-list"><b>Another label</b></label>', (string) $input->getLabel(Html::el('b', 'Another label')));
Assert::same('<select name="list" id="frm-list"><option class="class" value="">Prompt</option><option class="class" value="a">First</option><optgroup label="GROUP"><option value="0">Second</option></optgroup></select>', (string) $input->getControl());
Assert::same('<select name="list" id="frm-list"><option class="class" value="' . "!\x01prompt\x01%" . '">Prompt</option><option class="class" value="a">First</option><optgroup label="GROUP"><option value="0">Second</option></optgroup></select>', (string) $input->getControl());
});


Expand Down
4 changes: 2 additions & 2 deletions tests/Forms/expected/Forms.renderer.1.expect
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@
<tr>
<th><label for="frm-country">Country:</label></th>

<td><select name="country" id="frm-country" data-nette-rules='[{"op":":equal","rules":[{"op":":filled","msg":"Select your country"}],"control":"send","arg":true}]'><option value="">Select your country</option><optgroup label="Europe"><option value="CZ">Czech Republic</option><option value="SK">Slovakia</option><option value="GB">United Kingdom</option></optgroup><option value="CA">Canada</option><option value="US">United States</option><option value="?">other</option></select>
<td><select name="country" id="frm-country" data-nette-rules='[{"op":":equal","rules":[{"op":":filled","msg":"Select your country"}],"control":"send","arg":true}]'><option value="!prompt%">Select your country</option><optgroup label="Europe"><option value="CZ">Czech Republic</option><option value="SK">Slovakia</option><option value="GB">United Kingdom</option></optgroup><option value="CA">Canada</option><option value="US">United States</option><option value="?">other</option></select>
<span class="error">Select your country</span></td>
</tr>

<tr>
<th><label for="frm-countrySetItems">Country:</label></th>

<td><select name="countrySetItems" id="frm-countrySetItems"><option value="">Select your country</option><optgroup label="Europe"><option value="CZ">Czech Republic</option><option value="SK">Slovakia</option><option value="GB">United Kingdom</option></optgroup><option value="CA">Canada</option><option value="US">United States</option><option value="?">other</option></select></td>
<td><select name="countrySetItems" id="frm-countrySetItems"><option value="!prompt%">Select your country</option><optgroup label="Europe"><option value="CZ">Czech Republic</option><option value="SK">Slovakia</option><option value="GB">United Kingdom</option></optgroup><option value="CA">Canada</option><option value="US">United States</option><option value="?">other</option></select></td>
</tr>
</table>
</div>
Expand Down
2 changes: 1 addition & 1 deletion tests/Forms/expected/Forms.renderer.2.expect
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

<dt><label for="frm-country">Country:</label></dt>

<dd class="odd"><select name="country" id="frm-country" data-nette-rules='[{"op":":equal","rules":[{"op":":filled","msg":"Select your country"}],"control":"send","arg":true}]'><option value="">Select your country</option><optgroup label="Europe"><option value="CZ">Czech Republic</option><option value="SK">Slovakia</option><option value="GB">United Kingdom</option></optgroup><option value="CA">Canada</option><option value="US">United States</option><option value="?">other</option></select></dd>
<dd class="odd"><select name="country" id="frm-country" data-nette-rules='[{"op":":equal","rules":[{"op":":filled","msg":"Select your country"}],"control":"send","arg":true}]'><option value="!prompt%">Select your country</option><optgroup label="Europe"><option value="CZ">Czech Republic</option><option value="SK">Slovakia</option><option value="GB">United Kingdom</option></optgroup><option value="CA">Canada</option><option value="US">United States</option><option value="?">other</option></select></dd>

</dl>
</div>
Expand Down

0 comments on commit bedb0ea

Please sign in to comment.