diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bcde24c..b8e06cc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 3.11.1 under development - New #249: Add `Tag::addStyle()` and `Tag::removeStyle()` methods (@vjik) +- New #252: Add `Color` class, `Html::color()` and `Input::color()` methods (@razvbir) ## 3.11.0 June 10, 2025 diff --git a/README.md b/README.md index 2807dc03..2f68445c 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ The package provides various tools to help with dynamic server-side generation o - Tag classes `A`, `Address`, `Article`, `Aside`, `Audio`, `B`, `Body`, `Br`, `Button`, `Caption`, `Col`, `Colgroup`, `Datalist`, `Div`, `Em`, `Fieldset`, `Footer`, `Form`, `H1`, `H2`, `H3`, `H4`, `H5`, `H6`, `Header`, `Hr`, `Hgroup`, - `Html`, `I`, `Img`, `Input` (and specialized `Checkbox`, `Radio`, `Range`, `File`), `Label`, `Legend`, `Li`, `Link`, + `Html`, `I`, `Img`, `Input` (and specialized `Checkbox`, `Radio`, `Range`, `File`, `Color`), `Label`, `Legend`, `Li`, `Link`, `Meta`, `Nav`, `Noscript`, `Ol`, `Optgroup`, `Option`, `P`, `Picture`, `Script`, `Section`, `Select`, `Small`, `Source`, `Span`, `Strong`, `Style`, `Table`, `Tbody`, `Td`, `Textarea`, `Tfoot`, `Th`, `Thead`, `Title`, `Tr`, `Track`, `Ul`, `Video`. @@ -319,9 +319,10 @@ Overall the helper has the following method groups. - button - buttonInput - checkbox -- file +- color - datalist - fieldset +- file - fileInput - form - hiddenInput @@ -332,13 +333,14 @@ Overall the helper has the following method groups. - option - passwordInput - radio +- range - resetButton - resetInput - select - submitButton - submitInput -- textInput - textarea +- textInput #### Table tags diff --git a/src/Html.php b/src/Html.php index e8b502af..6430df41 100644 --- a/src/Html.php +++ b/src/Html.php @@ -35,6 +35,7 @@ use Yiisoft\Html\Tag\Img; use Yiisoft\Html\Tag\Input; use Yiisoft\Html\Tag\Input\Checkbox; +use Yiisoft\Html\Tag\Input\Color; use Yiisoft\Html\Tag\Input\File; use Yiisoft\Html\Tag\Input\Radio; use Yiisoft\Html\Tag\Input\Range; @@ -877,6 +878,24 @@ public static function range( return $attributes === [] ? $tag : $tag->addAttributes($attributes); } + /** + * Generates a color {@see Input} field. + * + * @see Input::color() + * + * @param string|null $name The name attribute. + * @param string|Stringable|null $value The value attribute. + * @param array $attributes The tag attributes in terms of name-value pairs. + */ + public static function color( + ?string $name = null, + string|Stringable|null $value = null, + array $attributes = [] + ): Color { + $tag = Input::color($name, $value); + return $attributes === [] ? $tag : $tag->addAttributes($attributes); + } + /** * Generates a checkbox {@see Input}. * diff --git a/src/Tag/Input.php b/src/Tag/Input.php index d8a1f42a..9e50aeb5 100644 --- a/src/Tag/Input.php +++ b/src/Tag/Input.php @@ -7,6 +7,7 @@ use Stringable; use Yiisoft\Html\Tag\Base\InputTag; use Yiisoft\Html\Tag\Input\Checkbox; +use Yiisoft\Html\Tag\Input\Color; use Yiisoft\Html\Tag\Input\File; use Yiisoft\Html\Tag\Input\Radio; use Yiisoft\Html\Tag\Input\Range; @@ -149,6 +150,26 @@ public static function range(?string $name = null, float|int|string|Stringable|n return $input; } + /** + * Color. + * + * @link https://html.spec.whatwg.org/multipage/input.html#color-state-(type=color) + * + * @param string|null $name Name of the input. + * @param string|Stringable|null $value Value of the input. + */ + public static function color(?string $name = null, string|Stringable|null $value = null): Color + { + $input = Color::tag(); + if ($name !== null) { + $input = $input->name($name); + } + if ($value !== null) { + $input = $input->value($value); + } + return $input; + } + /** * Button. * diff --git a/src/Tag/Input/Color.php b/src/Tag/Input/Color.php new file mode 100644 index 00000000..3d7ca78e --- /dev/null +++ b/src/Tag/Input/Color.php @@ -0,0 +1,18 @@ +attributes['type'] = 'color'; + } +} diff --git a/tests/HtmlTest.php b/tests/HtmlTest.php index 3c7c764a..4f374a81 100644 --- a/tests/HtmlTest.php +++ b/tests/HtmlTest.php @@ -382,6 +382,22 @@ public function testTextInput(): void ); } + public function testColorInput(): void + { + $this->assertSame('', Html::color()->render()); + $this->assertSame('', Html::color('')->render()); + $this->assertSame('', Html::color(null, '')->render()); + $this->assertSame('', Html::color('test')->render()); + $this->assertSame( + '', + Html::color('test', '#ff0000')->render(), + ); + $this->assertSame( + '', + Html::color('test', '#ff0000', ['required' => true])->render(), + ); + } + public function testHiddenInput(): void { $this->assertSame('', Html::hiddenInput()->render()); diff --git a/tests/Tag/Input/ColorTest.php b/tests/Tag/Input/ColorTest.php new file mode 100644 index 00000000..ae5a1d3d --- /dev/null +++ b/tests/Tag/Input/ColorTest.php @@ -0,0 +1,22 @@ +assertSame( + '', + Color::tag() + ->name('color') + ->value('#ff0000') + ->render() + ); + } +} diff --git a/tests/Tag/InputTest.php b/tests/Tag/InputTest.php index a6daebe8..78e29ea9 100644 --- a/tests/Tag/InputTest.php +++ b/tests/Tag/InputTest.php @@ -77,6 +77,14 @@ public function testRange(): void ); } + public function testColor(): void + { + $this->assertSame( + '', + (string) Input::color('color', '#ff0000') + ); + } + public function testButton(): void { $this->assertSame(