Skip to content

Commit 6995969

Browse files
authored
Add option "wrapInput" for checkbox and radio + tests (#21)
* More tests for Html::generateId(). * Add option "wrapInput" for checkbox and radio. * Fix typo.
1 parent 48a196c commit 6995969

2 files changed

Lines changed: 90 additions & 10 deletions

File tree

src/Html.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ public static function generateId(string $prefix = 'i'): string
103103
$counter = ++static::$generateIdCounter[$prefix];
104104
} else {
105105
$counter = 1;
106-
static::$generateIdCounter = [
107-
$prefix => $counter,
108-
];
106+
static::$generateIdCounter = [$prefix => $counter];
109107
}
110108
return $prefix . $counter;
111109
}
@@ -792,6 +790,9 @@ public static function checkbox(string $name, bool $checked = false, array $opti
792790
* When this option is specified, the checkbox will be enclosed by a label tag.
793791
* - labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label"
794792
* option.
793+
* - wrapInput: bool, use when has label.
794+
* if `wrapInput` is true result will be `<label><input> Label</label>`,
795+
* else `<input> <label>Label</label>`
795796
*
796797
* The rest of the options will be rendered as the attributes of the resulting checkbox tag. The values will be
797798
* HTML-encoded using {@see encode()}. If a value is null, the corresponding attribute will not be rendered.
@@ -823,15 +824,27 @@ private static function booleanInput(string $type, string $name, bool $checked,
823824
$hidden = '';
824825
}
825826

826-
if (isset($options['label'])) {
827-
$label = $options['label'];
828-
$labelOptions = $options['labelOptions'] ?? [];
829-
unset($options['label'], $options['labelOptions']);
830-
$content = static::label(static::input($type, $name, $value, $options) . ' ' . $label, null, $labelOptions);
831-
return $hidden . $content;
827+
$label = $options['label'] ?? null;
828+
$labelOptions = $options['labelOptions'] ?? [];
829+
$wrapInput = $options['wrapInput'] ?? true;
830+
unset($options['label'], $options['labelOptions'], $options['wrapInput']);
831+
832+
if (empty($label)) {
833+
return $hidden . static::input($type, $name, $value, $options);
834+
}
835+
836+
if ($wrapInput) {
837+
$input = static::input($type, $name, $value, $options);
838+
return $hidden . static::label($input . ' ' . $label, null, $labelOptions);
832839
}
833840

834-
return $hidden . static::input($type, $name, $value, $options);
841+
if (!isset($options['id'])) {
842+
$options['id'] = static::generateId();
843+
}
844+
return $hidden .
845+
static::input($type, $name, $value, $options) .
846+
' ' .
847+
static::label($label, $options['id'], $labelOptions);
835848
}
836849

837850
/**

tests/HtmlTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,28 @@
88

99
final class HtmlTest extends TestCase
1010
{
11+
/**
12+
* Use different values in different tests
13+
* @var mixed
14+
*/
15+
public static $hrtimeResult;
16+
17+
protected function setUp(): void
18+
{
19+
static::$hrtimeResult = null;
20+
parent::setUp();
21+
}
22+
1123
public function testGenerateId(): void
1224
{
1325
$this->assertMatchesRegularExpression('/i\d+/', Html::generateId());
1426
$this->assertMatchesRegularExpression('/test\d+/', Html::generateId('test'));
27+
28+
static::$hrtimeResult = 123;
29+
$this->assertSame('i1231', Html::generateId());
30+
$this->assertSame('i1232', Html::generateId());
31+
static::$hrtimeResult = 124;
32+
$this->assertSame('i1241', Html::generateId());
1533
}
1634

1735
public function testEncode(): void
@@ -350,6 +368,26 @@ public function testRadio(): void
350368
'label' => 'ccc',
351369
'value' => 2,
352370
]));
371+
372+
$this->assertSame(
373+
'<input type="radio" id="UseThis" name="test" checked> <label for="UseThis">Use this</label>',
374+
Html::radio('test', true, [
375+
'id' => 'UseThis',
376+
'label' => 'Use this',
377+
'value' => null,
378+
'wrapInput' => false,
379+
])
380+
);
381+
382+
static::$hrtimeResult = 42;
383+
$this->assertSame(
384+
'<input type="radio" id="i421" name="test" checked> <label for="i421">Use this</label>',
385+
Html::radio('test', true, [
386+
'label' => 'Use this',
387+
'value' => null,
388+
'wrapInput' => false,
389+
])
390+
);
353391
}
354392

355393
public function testCheckbox(): void
@@ -386,6 +424,26 @@ public function testCheckbox(): void
386424
'value' => 2,
387425
'form' => 'test-form',
388426
]));
427+
428+
$this->assertSame(
429+
'<input type="checkbox" id="UseThis" name="test"> <label for="UseThis">Use this</label>',
430+
Html::checkbox('test', false, [
431+
'id' => 'UseThis',
432+
'label' => 'Use this',
433+
'value' => null,
434+
'wrapInput' => false,
435+
])
436+
);
437+
438+
static::$hrtimeResult = 49;
439+
$this->assertSame(
440+
'<input type="checkbox" id="i491" name="test"> <label for="i491">Use this</label>',
441+
Html::checkbox('test', false, [
442+
'label' => 'Use this',
443+
'value' => null,
444+
'wrapInput' => false,
445+
])
446+
);
389447
}
390448

391449
public function testDropDownList(): void
@@ -1196,3 +1254,12 @@ public function testEscapeJsRegularExpression(string $expected, string $regexp):
11961254
);
11971255
}
11981256
}
1257+
1258+
namespace Yiisoft\Html;
1259+
1260+
use Yiisoft\Html\Tests\HtmlTest;
1261+
1262+
function hrtime(bool $getAsNumber = false)
1263+
{
1264+
return HtmlTest::$hrtimeResult ?? \hrtime($getAsNumber);
1265+
}

0 commit comments

Comments
 (0)