File tree Expand file tree Collapse file tree 4 files changed +147
-0
lines changed Expand file tree Collapse file tree 4 files changed +147
-0
lines changed Original file line number Diff line number Diff line change @@ -522,6 +522,16 @@ public function addMultiSelect(
522522 }
523523
524524
525+ /**
526+ * Adds color picker.
527+ * @param string|object|null $label
528+ */
529+ public function addColor (string $ name , $ label = null ): Controls \ColorPicker
530+ {
531+ return $ this [$ name ] = new Controls \ColorPicker ($ label );
532+ }
533+
534+
525535 /**
526536 * Adds button used to submit form.
527537 * @param string|object|null $caption
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /**
4+ * This file is part of the Nette Framework (https://nette.org)
5+ * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+ */
7+
8+ declare (strict_types=1 );
9+
10+ namespace Nette \Forms \Controls ;
11+
12+ use Nette ;
13+
14+
15+ /**
16+ * Color picker.
17+ */
18+ class ColorPicker extends BaseControl
19+ {
20+ public function __construct ($ label = null )
21+ {
22+ parent ::__construct ($ label );
23+ $ this ->setOption ('type ' , 'color ' );
24+ }
25+
26+
27+ /**
28+ * @param ?string $value
29+ * @return static
30+ */
31+ public function setValue ($ value )
32+ {
33+ if ($ value === null ) {
34+ $ this ->value = '#000000 ' ;
35+ } elseif (is_string ($ value ) && preg_match ('~#?[0-9a-f]{6}~DAi ' , $ value )) {
36+ $ this ->value = '# ' . strtolower (ltrim ($ value , '# ' ));
37+ } else {
38+ throw new Nette \InvalidArgumentException ('Color must have #rrggbb format. ' );
39+ }
40+ return $ this ;
41+ }
42+
43+
44+ public function loadHttpData (): void
45+ {
46+ try {
47+ parent ::loadHttpData ();
48+ } catch (Nette \InvalidArgumentException $ e ) {
49+ $ this ->setValue (null );
50+ }
51+ }
52+
53+
54+ public function getControl (): Nette \Utils \Html
55+ {
56+ return parent ::getControl ()->addAttributes ([
57+ 'type ' => 'color ' ,
58+ 'value ' => $ this ->value ,
59+ ]);
60+ }
61+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /**
4+ * Test: Nette\Forms\Controls\ColorPicker.
5+ */
6+
7+ declare (strict_types=1 );
8+
9+ use Nette \Forms \Form ;
10+ use Tester \Assert ;
11+
12+ require __DIR__ . '/../bootstrap.php ' ;
13+
14+
15+ before (function () {
16+ $ _SERVER ['REQUEST_METHOD ' ] = 'POST ' ;
17+ $ _POST = $ _FILES = [];
18+ $ _COOKIE [Nette \Http \Helpers::STRICT_COOKIE_NAME ] = '1 ' ;
19+ Form::initialize (true );
20+ });
21+
22+
23+ test ('loadData empty string ' , function () {
24+ $ _POST = ['color ' => '' ];
25+
26+ $ form = new Form ;
27+ $ input = $ form ->addColor ('color ' );
28+
29+ Assert::same ('#000000 ' , $ input ->getValue ());
30+ Assert::true ($ input ->isFilled ());
31+ });
32+
33+
34+ test ('loadData invalid string ' , function () {
35+ $ _POST = ['color ' => '#abc ' ];
36+
37+ $ form = new Form ;
38+ $ input = $ form ->addColor ('color ' );
39+
40+ Assert::same ('#000000 ' , $ input ->getValue ());
41+ Assert::true ($ input ->isFilled ());
42+ });
43+
44+
45+ test ('loadData valid string ' , function () {
46+ $ _POST = ['color ' => '#1020aa ' ];
47+
48+ $ form = new Form ;
49+ $ input = $ form ->addColor ('color ' );
50+
51+ Assert::same ('#1020aa ' , $ input ->getValue ());
52+ Assert::true ($ input ->isFilled ());
53+ });
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /**
4+ * Test: Nette\Forms\Controls\ColorPicker.
5+ */
6+
7+ declare (strict_types=1 );
8+
9+ use Nette \Forms \Form ;
10+ use Nette \Utils \Html ;
11+ use Tester \Assert ;
12+
13+ require __DIR__ . '/../bootstrap.php ' ;
14+
15+
16+ test ('' , function () {
17+ $ form = new Form ;
18+ $ input = $ form ->addColor ('color ' )
19+ ->setValue ('1020AB ' );
20+
21+ Assert::type (Html::class, $ input ->getControl ());
22+ Assert::same ('<input type="color" name="color" id="frm-color" value="#1020ab"> ' , (string ) $ input ->getControl ());
23+ });
You can’t perform that action at this time.
0 commit comments