Skip to content

Commit

Permalink
Fix #104: Add parameter $attributes to methods Html::input(), `Ht…
Browse files Browse the repository at this point in the history
…ml::buttonInput()`, `Html::submitInput()` and `Html::resetInput()`
  • Loading branch information
vjik committed Apr 8, 2022
1 parent 065cfdd commit df65ea8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,8 @@
- Enh #106: Add option groups support to method `Select::optionsData()` (vjik)
- Enh #108: Add individual attributes of options and option groups support to method `Select::optionsData()` (vjik)
- Enh #102: Remove psalm type `HtmlAttributes`, too obsessive for package users (vjik)
- Enh #104: Add parameter `$attributes` to methods `Html::input()`, `Html::buttonInput()`, `Html::submitInput()`
and `Html::resetInput()` (vjik)

## 2.3.0 March 25, 2022

Expand Down
33 changes: 26 additions & 7 deletions src/Html.php
Expand Up @@ -632,8 +632,9 @@ public static function resetButton(string $content = 'Reset', array $attributes
* @param string|null $name The name attribute. If it is `null`, the name attribute will not be generated.
* @param bool|float|int|string|Stringable|null $value The value attribute. If it is `null`, the value
* attribute will not be generated.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function input(string $type, ?string $name = null, $value = null): Input
public static function input(string $type, ?string $name = null, $value = null, array $attributes = []): Input
{
$tag = Input::tag()->type($type);
if ($name !== null) {
Expand All @@ -642,6 +643,9 @@ public static function input(string $type, ?string $name = null, $value = null):
if ($value !== null) {
$tag = $tag->value($value);
}
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}

Expand All @@ -651,10 +655,15 @@ public static function input(string $type, ?string $name = null, $value = null):
* @see Input::button()
*
* @param string|null $label The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function buttonInput(?string $label = 'Button'): Input
public static function buttonInput(?string $label = 'Button', array $attributes = []): Input
{
return Input::button($label);
$tag = Input::button($label);
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}

/**
Expand All @@ -663,10 +672,15 @@ public static function buttonInput(?string $label = 'Button'): Input
* @see Input::submitButton()
*
* @param string|null $label The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function submitInput(?string $label = 'Submit'): Input
public static function submitInput(?string $label = 'Submit', array $attributes = []): Input
{
return Input::submitButton($label);
$tag = Input::submitButton($label);
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}

/**
Expand All @@ -675,10 +689,15 @@ public static function submitInput(?string $label = 'Submit'): Input
* @see Input::resetButton()
*
* @param string|null $label The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*/
public static function resetInput(?string $label = 'Reset'): Input
public static function resetInput(?string $label = 'Reset', array $attributes = []): Input
{
return Input::resetButton($label);
$tag = Input::resetButton($label);
if (!empty($attributes)) {
$tag = $tag->attributes($attributes);
}
return $tag;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/common/HtmlTest.php
Expand Up @@ -297,6 +297,10 @@ public function testInput(): void
'<input type="text" name="test" value="43">',
Html::input('text', 'test', '43')->render(),
);
$this->assertSame(
'<input type="text" name="test" value="43" data-key="x101">',
Html::input('text', 'test', '43', ['data-key' => 'x101'])->render(),
);
}

public function testButtonInput(): void
Expand All @@ -305,6 +309,10 @@ public function testButtonInput(): void
$this->assertSame('<input type="button">', Html::buttonInput(null)->render());
$this->assertSame('<input type="button" value>', Html::buttonInput('')->render());
$this->assertSame('<input type="button" value="Go">', Html::buttonInput('Go')->render());
$this->assertSame(
'<input type="button" value="Go" data-key="x101">',
Html::buttonInput('Go', ['data-key' => 'x101'])->render(),
);
}

public function testSubmitInput(): void
Expand All @@ -313,6 +321,10 @@ public function testSubmitInput(): void
$this->assertSame('<input type="submit">', Html::submitInput(null)->render());
$this->assertSame('<input type="submit" value>', Html::submitInput('')->render());
$this->assertSame('<input type="submit" value="Go">', Html::submitInput('Go')->render());
$this->assertSame(
'<input type="submit" value="Go" data-key="x101">',
Html::submitInput('Go', ['data-key' => 'x101'])->render(),
);
}

public function testResetInput(): void
Expand All @@ -321,6 +333,10 @@ public function testResetInput(): void
$this->assertSame('<input type="reset">', Html::resetInput(null)->render());
$this->assertSame('<input type="reset" value>', Html::resetInput('')->render());
$this->assertSame('<input type="reset" value="Go">', Html::resetInput('Go')->render());
$this->assertSame(
'<input type="reset" value="Go" data-key="x101">',
Html::resetInput('Go', ['data-key' => 'x101'])->render(),
);
}

public function testTextInput(): void
Expand Down

0 comments on commit df65ea8

Please sign in to comment.