Skip to content

Commit

Permalink
Fix #92: A third optional argument $attributes containing tag attri…
Browse files Browse the repository at this point in the history
…butes 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()`
  • Loading branch information
vjik committed Oct 20, 2021
1 parent 71118bd commit 95729b0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,9 @@

## 2.1.1 under development

- 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)
- New #89: Add method `nofollow()` to the `A` tag (soodssr)

## 2.1.0 September 23, 2021
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -32,7 +32,7 @@
"require-dev": {
"maglnet/composer-require-checker": "^3.3",
"phpunit/phpunit": "^9.5",
"roave/infection-static-analysis-plugin": "^1.9",
"roave/infection-static-analysis-plugin": "^1.10",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.10"
},
Expand Down
55 changes: 41 additions & 14 deletions src/Html.php
Expand Up @@ -666,10 +666,14 @@ public static function resetInput(?string $label = 'Reset'): Input
*
* @param string|null $name The name attribute.
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*
* @psalm-param HtmlAttributes $attributes
*/
public static function textInput(?string $name = null, $value = null): Input
public static function textInput(?string $name = null, $value = null, array $attributes = []): Input
{
return Input::text($name, $value);
$tag = Input::text($name, $value);
return $attributes === [] ? $tag : $tag->attributes($attributes);
}

/**
Expand All @@ -679,10 +683,14 @@ public static function textInput(?string $name = null, $value = null): Input
*
* @param string|null $name The name attribute.
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*
* @psalm-param HtmlAttributes $attributes
*/
public static function hiddenInput(?string $name = null, $value = null): Input
public static function hiddenInput(?string $name = null, $value = null, array $attributes = []): Input
{
return Input::hidden($name, $value);
$tag = Input::hidden($name, $value);
return $attributes === [] ? $tag : $tag->attributes($attributes);
}

/**
Expand All @@ -692,10 +700,14 @@ public static function hiddenInput(?string $name = null, $value = null): Input
*
* @param string|null $name The name attribute.
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*
* @psalm-param HtmlAttributes $attributes
*/
public static function passwordInput(?string $name = null, $value = null): Input
public static function passwordInput(?string $name = null, $value = null, array $attributes = []): Input
{
return Input::password($name, $value);
$tag = Input::password($name, $value);
return $attributes === [] ? $tag : $tag->attributes($attributes);
}

/**
Expand All @@ -709,10 +721,14 @@ public static function passwordInput(?string $name = null, $value = null): Input
*
* @param string|null $name The name attribute.
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*
* @psalm-param HtmlAttributes $attributes
*/
public static function fileInput(?string $name = null, $value = null): Input
public static function fileInput(?string $name = null, $value = null, array $attributes = []): Input
{
return Input::file($name, $value);
$tag = Input::file($name, $value);
return $attributes === [] ? $tag : $tag->attributes($attributes);
}

/**
Expand All @@ -722,10 +738,14 @@ public static function fileInput(?string $name = null, $value = null): Input
*
* @param string|null $name The name attribute.
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*
* @psalm-param HtmlAttributes $attributes
*/
public static function radio(?string $name = null, $value = null): Radio
public static function radio(?string $name = null, $value = null, array $attributes = []): Radio
{
return Input::radio($name, $value);
$tag = Input::radio($name, $value);
return $attributes === [] ? $tag : $tag->attributes($attributes);
}

/**
Expand All @@ -735,19 +755,26 @@ public static function radio(?string $name = null, $value = null): Radio
*
* @param string|null $name The name attribute.
* @param bool|float|int|string|Stringable|null $value The value attribute.
* @param array $attributes The tag attributes in terms of name-value pairs.
*
* @psalm-param HtmlAttributes $attributes
*/
public static function checkbox(?string $name = null, $value = null): Checkbox
public static function checkbox(?string $name = null, $value = null, array $attributes = []): Checkbox
{
return Input::checkbox($name, $value);
$tag = Input::checkbox($name, $value);
return $attributes === [] ? $tag : $tag->attributes($attributes);
}

/**
* Generates a {@see Textarea} input.
*
* @param string|null $name The input name.
* @param string|null $value The input value.
* @param array $attributes The tag attributes in terms of name-value pairs.
*
* @psalm-param HtmlAttributes $attributes
*/
public static function textarea(?string $name = null, ?string $value = null): Textarea
public static function textarea(?string $name = null, ?string $value = null, array $attributes = []): Textarea
{
$tag = Textarea::tag();
if ($name !== null) {
Expand All @@ -756,7 +783,7 @@ public static function textarea(?string $name = null, ?string $value = null): Te
if (!empty($value)) {
$tag = $tag->value($value);
}
return $tag;
return $attributes === [] ? $tag : $tag->attributes($attributes);
}

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

public function testHiddenInput(): void
Expand All @@ -316,6 +320,10 @@ public function testHiddenInput(): void
'<input type="hidden" name="test" value="43">',
Html::hiddenInput('test', '43')->render(),
);
$this->assertSame(
'<input type="hidden" id="ABC" name="test" value="43">',
Html::hiddenInput('test', '43', ['id' => 'ABC'])->render(),
);
}

public function testPasswordInput(): void
Expand All @@ -328,6 +336,10 @@ public function testPasswordInput(): void
'<input type="password" name="test" value="43">',
Html::passwordInput('test', '43')->render(),
);
$this->assertSame(
'<input type="password" name="test" value="43" data-key="7">',
Html::passwordInput('test', '43', ['data-key' => '7'])->render(),
);
}

public function testFileInput(): void
Expand All @@ -340,6 +352,10 @@ public function testFileInput(): void
'<input type="file" name="test" value="43">',
Html::fileInput('test', '43')->render(),
);
$this->assertSame(
'<input type="file" class="photo" name="test" value="43">',
Html::fileInput('test', '43', ['class' => 'photo'])->render(),
);
}

public function testRadio(): void
Expand All @@ -352,6 +368,10 @@ public function testRadio(): void
'<input type="radio" name="test" value="43">',
Html::radio('test', '43')->render(),
);
$this->assertSame(
'<input type="radio" name="test" value="43" readonly>',
Html::radio('test', '43', ['readonly' => true])->render(),
);
}

public function testCheckbox(): void
Expand All @@ -364,6 +384,10 @@ public function testCheckbox(): void
'<input type="checkbox" name="test" value="43">',
Html::checkbox('test', '43')->render(),
);
$this->assertSame(
'<input type="checkbox" name="test" value="43" readonly>',
Html::checkbox('test', '43', ['readonly' => true])->render(),
);
}

public function testSelect(): void
Expand Down Expand Up @@ -393,6 +417,10 @@ public function testTextarea(): void
$this->assertSame('<textarea name></textarea>', Html::textarea('')->render());
$this->assertSame('<textarea name="test"></textarea>', Html::textarea('test')->render());
$this->assertSame('<textarea name="test">body</textarea>', Html::textarea('test', 'body')->render());
$this->assertSame(
'<textarea name="test" readonly>body</textarea>',
Html::textarea('test', 'body', ['readonly' => true])->render()
);
}

public function testCheckboxList(): void
Expand Down

0 comments on commit 95729b0

Please sign in to comment.