summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--includes/view-helpers/select_field.php10
-rw-r--r--tests/SelectFieldTest.php17
2 files changed, 25 insertions, 2 deletions
diff --git a/includes/view-helpers/select_field.php b/includes/view-helpers/select_field.php
index 7cff3ef..816f8ef 100644
--- a/includes/view-helpers/select_field.php
+++ b/includes/view-helpers/select_field.php
@@ -13,11 +13,17 @@ namespace EternalTerror\ViewHelpers;
* @param array $opts The options as arrays of [value, label] pairs
* @param mixed|int $selected The value of the preselected option, or null if no
* option is preselected.
+ * @param string $blank Text to use for "no selection", defaults to "Please
+ * select..."
* @return string
*/
-function select_field(string $name, ?array $opts = [], $selected = null) : string
+function select_field(
+ string $name,
+ ?array $opts = [],
+ $selected = null,
+ string $blank = "Please select...") : string
{
- $body = '';
+ $body = "<option value=\"\">{$blank}</option>";
foreach ($opts as $opt) {
$sel = selected($selected, $opt[0], false);
$body .= "<option value=\"{$opt[0]}\"{$sel}>{$opt[1]}</option>";
diff --git a/tests/SelectFieldTest.php b/tests/SelectFieldTest.php
index 1c1a8c2..fb6c298 100644
--- a/tests/SelectFieldTest.php
+++ b/tests/SelectFieldTest.php
@@ -22,6 +22,7 @@ final class SelectFieldTest extends WP_UnitTestCase
$res = ViewHelpers\select_field("fooselect", [[42, 'An option']]);
$this->assertStringStartsWith('<select name="fooselect">', $res);
$this->assertStringEndsWith('</select>', $res);
+ $this->assertStringContainsString('<option value="">Please select...</option>', $res);
$this->assertStringContainsString('<option value="42">An option</option>', $res);
}
@@ -33,6 +34,7 @@ final class SelectFieldTest extends WP_UnitTestCase
$this->assertStringStartsWith('<select name="fooselect">', $res);
$this->assertStringEndsWith('</select>', $res);
+ $this->assertStringContainsString('<option value="">Please select...</option>', $res);
$this->assertStringContainsString('<option value="42">An option</option>', $res);
$this->assertStringContainsString('<option value="666">Another option</option>', $res);
$this->assertStringContainsString('<option value="foo">A foo option</option>', $res);
@@ -46,6 +48,21 @@ final class SelectFieldTest extends WP_UnitTestCase
$this->assertStringStartsWith('<select name="fooselect">', $res);
$this->assertStringEndsWith('</select>', $res);
+ $this->assertStringContainsString('<option value="">Please select...</option>', $res);
+ $this->assertStringContainsString('<option value="42">An option</option>', $res);
+ $this->assertStringContainsString('<option value="666" selected=\'selected\'>Another option</option>', $res);
+ $this->assertStringContainsString('<option value="foo">A foo option</option>', $res);
+ }
+
+ public function testSelectFieldWithCustomBlankSelectionText()
+ {
+ $opts = [[42, 'An option'], [666, 'Another option'], ["foo", 'A foo option']];
+
+ $res = ViewHelpers\select_field("fooselect", $opts, 666, "None");
+
+ $this->assertStringStartsWith('<select name="fooselect">', $res);
+ $this->assertStringEndsWith('</select>', $res);
+ $this->assertStringContainsString('<option value="">None</option>', $res);
$this->assertStringContainsString('<option value="42">An option</option>', $res);
$this->assertStringContainsString('<option value="666" selected=\'selected\'>Another option</option>', $res);
$this->assertStringContainsString('<option value="foo">A foo option</option>', $res);