Skip to content

Commit

Permalink
SelectBox: prompt <option> is hidden
Browse files Browse the repository at this point in the history
- only makes sense for the required element
- workaround for Safari: disabled & selected attributes
  • Loading branch information
dg committed Apr 1, 2024
1 parent e2041dc commit e908def
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Forms/Controls/SelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,20 @@ public function getControl(): Nette\Utils\Html
}

$attrs = $this->optionAttributes;
$attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : null;
$attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : [];

$selected = $this->value;
if ($this->prompt !== false) {
$promptKey = '';
while (isset($items[$promptKey])) $promptKey .= "\x1";
$items = [$promptKey => $this->translate($this->prompt)] + $items;
if ($this->isRequired()) {
$attrs['hidden:'][$promptKey] = $attrs['disabled:'][$promptKey] = true;
$selected ??= $promptKey; // disabled & selected for Safari, hidden for other browsers
}
}

return Nette\Forms\Helpers::createSelectBox($items, $attrs, $this->value)
return Nette\Forms\Helpers::createSelectBox($items, $attrs, $selected)
->addAttributes(parent::getControl()->attrs);
}

Expand Down
30 changes: 30 additions & 0 deletions tests/Forms/Controls.SelectBox.render.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@ test('selected 2x', function () {
});


test('prompt', function () {
$form = new Form;
$input = $form->addSelect('list', 'Label', [
'a' => 'First',
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());

$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());
});


test('prompt + required', function () {
$form = new Form;
$input = $form->addSelect('list', 'Label', [
'a' => 'First',
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());

$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());
});


test('unique prompt', function () {
$form = new Form;
$input = $form->addSelect('list', 'Label', [
Expand Down

0 comments on commit e908def

Please sign in to comment.