From bedb0ea235284251037789eea6ea766ea4801a77 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 27 Mar 2024 20:53:20 +0100 Subject: [PATCH] SelectBox: distinctive prompt key --- src/Forms/Controls/SelectBox.php | 7 ++++--- tests/Forms/Controls.SelectBox.render.phpt | 12 ++++++------ tests/Forms/expected/Forms.renderer.1.expect | 4 ++-- tests/Forms/expected/Forms.renderer.2.expect | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Forms/Controls/SelectBox.php b/src/Forms/Controls/SelectBox.php index bf134c9ac..b6cfdfc74 100644 --- a/src/Forms/Controls/SelectBox.php +++ b/src/Forms/Controls/SelectBox.php @@ -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 = []; @@ -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) { diff --git a/tests/Forms/Controls.SelectBox.render.phpt b/tests/Forms/Controls.SelectBox.render.phpt index eb09ed126..ae657d17f 100644 --- a/tests/Forms/Controls.SelectBox.render.phpt +++ b/tests/Forms/Controls.SelectBox.render.phpt @@ -68,11 +68,11 @@ test('prompt', function () { 0 => 'Second', ])->setPrompt('prompt'); - Assert::same('', (string) $input->getControl()); + Assert::same('', (string) $input->getControl()); $input->setValue(0); - Assert::same('', (string) $input->getControl()); + Assert::same('', (string) $input->getControl()); }); @@ -83,11 +83,11 @@ test('prompt + required', function () { 0 => 'Second', ])->setPrompt('prompt')->setRequired(); - Assert::same('', (string) $input->getControl()); + Assert::same('', (string) $input->getControl()); $input->setValue(0); - Assert::same('', (string) $input->getControl()); + Assert::same('', (string) $input->getControl()); }); @@ -101,7 +101,7 @@ test('translator & groups', function () { Assert::same('', (string) $input->getLabel()); Assert::same('', (string) $input->getLabel('Another label')); - Assert::same('', (string) $input->getControl()); + Assert::same('', (string) $input->getControl()); }); @@ -115,7 +115,7 @@ test('Html with translator & groups', function () { Assert::same('', (string) $input->getLabel()); Assert::same('', (string) $input->getLabel(Html::el('b', 'Another label'))); - Assert::same('', (string) $input->getControl()); + Assert::same('', (string) $input->getControl()); }); diff --git a/tests/Forms/expected/Forms.renderer.1.expect b/tests/Forms/expected/Forms.renderer.1.expect index 8b0082a2e..4ea307496 100644 --- a/tests/Forms/expected/Forms.renderer.1.expect +++ b/tests/Forms/expected/Forms.renderer.1.expect @@ -61,14 +61,14 @@ - + Select your country - + diff --git a/tests/Forms/expected/Forms.renderer.2.expect b/tests/Forms/expected/Forms.renderer.2.expect index 4fbec753b..187997c70 100644 --- a/tests/Forms/expected/Forms.renderer.2.expect +++ b/tests/Forms/expected/Forms.renderer.2.expect @@ -60,7 +60,7 @@
-
+