Skip to content

Commit

Permalink
Fix #90: Add method itemsFromValues() to widgets RadioList and `C…
Browse files Browse the repository at this point in the history
…heckboxList` that set items with labels equal to values
  • Loading branch information
vjik committed Oct 20, 2021
1 parent 95729b0 commit 8347945
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 7 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
@@ -1,8 +1,9 @@
# Yii HTML Change Log

## 2.2.0 under development

## 2.1.1 under development

- New #90: Add method `itemsFromValues()` to widgets `RadioList` and `CheckboxList` that set items with labels equal
to values (vjik)
- New #92: A third optional argument `$attributes` containing tag attributes in terms of name-value pairs has been
added to methods `Html::textInput()`, `Html::hiddenInput()`, `Html::passwordInput()`, `Html::fileInput()`,
`Html::radio()`, `Html::checkbox()`, `Html::textarea()` (vjik)
Expand Down
19 changes: 18 additions & 1 deletion src/Widget/CheckboxList/CheckboxList.php
Expand Up @@ -130,6 +130,22 @@ public function items(array $items, bool $encodeLabels = true): self
return $new;
}

/**
* Fills items from an array provided. Array values are used for both input labels and input values.
*
* @param bool[]|float[]|int[]|string[]|\Stringable[] $values
* @param bool $encodeLabels Whether labels should be encoded.
*/
public function itemsFromValues(array $values, bool $encodeLabels = true): self
{
$values = array_map('\strval', $values);

return $this->items(
array_combine($values, $values),
$encodeLabels
);
}

/**
* @param scalar|\Stringable ...$value
*/
Expand Down Expand Up @@ -192,7 +208,7 @@ public function disabled(bool $disabled = true): self
public function uncheckValue($value): self
{
$new = clone $this;
$new->uncheckValue = $value === null ? null : (string)$value;
$new->uncheckValue = $value === null ? null : (string) $value;
return $new;
}

Expand All @@ -205,6 +221,7 @@ public function separator(string $separator): self

/**
* @param Closure|null $formatter
*
* @psalm-param Closure(CheckboxItem):string|null $formatter
*/
public function itemFormatter(?Closure $formatter): self
Expand Down
22 changes: 20 additions & 2 deletions src/Widget/RadioList/RadioList.php
Expand Up @@ -111,6 +111,7 @@ public function replaceIndividualInputAttributes(array $attributes): self

/**
* @param string[] $items
* @param bool $encodeLabels Whether labels should be encoded.
*/
public function items(array $items, bool $encodeLabels = true): self
{
Expand All @@ -120,13 +121,29 @@ public function items(array $items, bool $encodeLabels = true): self
return $new;
}

/**
* Fills items from an array provided. Array values are used for both input labels and input values.
*
* @param bool[]|float[]|int[]|string[]|\Stringable[] $values
* @param bool $encodeLabels Whether labels should be encoded.
*/
public function itemsFromValues(array $values, bool $encodeLabels = true): self
{
$values = array_map('\strval', $values);

return $this->items(
array_combine($values, $values),
$encodeLabels
);
}

/**
* @param bool|float|int|string|\Stringable|null $value
*/
public function value($value): self
{
$new = clone $this;
$new->value = $value === null ? null : (string)$value;
$new->value = $value === null ? null : (string) $value;
return $new;
}

Expand Down Expand Up @@ -166,7 +183,7 @@ public function disabled(bool $disabled = true): self
public function uncheckValue($value): self
{
$new = clone $this;
$new->uncheckValue = $value === null ? null : (string)$value;
$new->uncheckValue = $value === null ? null : (string) $value;
return $new;
}

Expand All @@ -179,6 +196,7 @@ public function separator(string $separator): self

/**
* @param Closure|null $formatter
*
* @psalm-param Closure(RadioItem):string|null $formatter
*/
public function itemFormatter(?Closure $formatter): self
Expand Down
53 changes: 52 additions & 1 deletion tests/common/Widget/CheckboxListTest.php
Expand Up @@ -255,6 +255,56 @@ public function testItemsWithoutEncodeLabel(): void
);
}

public function dataItemsFromValues(): array
{
return [
[
'<label><input type="checkbox" name="test[]" value="1"> 1</label>' . "\n" .
'<label><input type="checkbox" name="test[]" value="2"> 2</label>',
[1, 2],
],
[
'<label><input type="checkbox" name="test[]" value="One"> One</label>' . "\n" .
'<label><input type="checkbox" name="test[]" value="&lt;b&gt;Two&lt;/b&gt;"> &lt;b&gt;Two&lt;/b&gt;</label>',
['One', '<b>Two</b>'],
],
[
'<label><input type="checkbox" name="test[]" value="1"> 1</label>' . "\n" .
'<label><input type="checkbox" name="test[]" value></label>',
[true, false],
],
];
}

/**
* @dataProvider dataItemsFromValues
*/
public function testItemsFromValues(string $expected, array $values): void
{
$this->assertSame(
$expected,
CheckboxList::create('test')
->itemsFromValues($values)
->withoutContainer()
->render(),
);
}

public function testItemsFromValuesWithoutEncodeLabel(): void
{
$this->assertSame(
'<label><input type="checkbox" name="test[]" value="One"> One</label>' . "\n" .
'<label><input type="checkbox" name="test[]" value="&lt;b&gt;Two&lt;/b&gt;"> <b>Two</b></label>',
CheckboxList::create('test')
->itemsFromValues([
'One',
'<b>Two</b>',
], false)
->withoutContainer()
->render(),
);
}

public function dataValue(): array
{
return [
Expand Down Expand Up @@ -577,7 +627,7 @@ public function testStringable(): void
{
$this->assertSame(
"<div>\n</div>",
(string)CheckboxList::create('test'),
(string) CheckboxList::create('test'),
);
}

Expand All @@ -592,6 +642,7 @@ public function testImmutability(): void
$this->assertNotSame($widget, $widget->individualInputAttributes([]));
$this->assertNotSame($widget, $widget->replaceIndividualInputAttributes([]));
$this->assertNotSame($widget, $widget->items([]));
$this->assertNotSame($widget, $widget->itemsFromValues([]));
$this->assertNotSame($widget, $widget->value());
$this->assertNotSame($widget, $widget->values([]));
$this->assertNotSame($widget, $widget->form(''));
Expand Down
53 changes: 52 additions & 1 deletion tests/common/Widget/RadioListTest.php
Expand Up @@ -252,6 +252,56 @@ public function testItemsWithoutEncodeLabel(): void
);
}

public function dataItemsFromValues(): array
{
return [
[
'<label><input type="radio" name="test" value="1"> 1</label>' . "\n" .
'<label><input type="radio" name="test" value="2"> 2</label>',
[1, 2],
],
[
'<label><input type="radio" name="test" value="One"> One</label>' . "\n" .
'<label><input type="radio" name="test" value="&lt;b&gt;Two&lt;/b&gt;"> &lt;b&gt;Two&lt;/b&gt;</label>',
['One', '<b>Two</b>'],
],
[
'<label><input type="radio" name="test" value="1"> 1</label>' . "\n" .
'<label><input type="radio" name="test" value></label>',
[true, false],
],
];
}

/**
* @dataProvider dataItemsFromValues
*/
public function testItemsFromValues(string $expected, array $values): void
{
$this->assertSame(
$expected,
RadioList::create('test')
->itemsFromValues($values)
->withoutContainer()
->render(),
);
}

public function testItemsFromValuesWithoutEncodeLabel(): void
{
$this->assertSame(
'<label><input type="radio" name="test" value="One"> One</label>' . "\n" .
'<label><input type="radio" name="test" value="&lt;b&gt;Two&lt;/b&gt;"> <b>Two</b></label>',
RadioList::create('test')
->itemsFromValues([
'One',
'<b>Two</b>',
], false)
->withoutContainer()
->render(),
);
}

public function dataValue(): array
{
return [
Expand Down Expand Up @@ -547,7 +597,7 @@ public function testStringable(): void
{
$this->assertSame(
"<div>\n</div>",
(string)RadioList::create('test'),
(string) RadioList::create('test'),
);
}

Expand All @@ -562,6 +612,7 @@ public function testImmutability(): void
$this->assertNotSame($widget, $widget->individualInputAttributes([]));
$this->assertNotSame($widget, $widget->replaceIndividualInputAttributes([]));
$this->assertNotSame($widget, $widget->items([]));
$this->assertNotSame($widget, $widget->itemsFromValues([]));
$this->assertNotSame($widget, $widget->value(null));
$this->assertNotSame($widget, $widget->form(''));
$this->assertNotSame($widget, $widget->readonly());
Expand Down

0 comments on commit 8347945

Please sign in to comment.