-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (https://nette.org) | ||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\Forms\Controls; | ||
|
||
use Nette; | ||
|
||
|
||
/** | ||
* Color picker. | ||
*/ | ||
class ColorPicker extends BaseControl | ||
{ | ||
public function __construct($label = null) | ||
{ | ||
parent::__construct($label); | ||
$this->setOption('type', 'color'); | ||
} | ||
|
||
|
||
/** | ||
* @param ?string $value | ||
* @return static | ||
*/ | ||
public function setValue($value) | ||
{ | ||
if ($value === null) { | ||
$this->value = '#000000'; | ||
} elseif (is_string($value) && preg_match('~#[0-9a-f]{6}~DAi', $value)) { | ||
$this->value = strtolower($value); | ||
} else { | ||
throw new Nette\InvalidArgumentException('Color must have #rrggbb format.'); | ||
} | ||
return $this; | ||
} | ||
|
||
|
||
public function loadHttpData(): void | ||
{ | ||
try { | ||
parent::loadHttpData(); | ||
} catch (Nette\InvalidArgumentException $e) { | ||
$this->setValue(null); | ||
} | ||
} | ||
|
||
|
||
public function getControl(): Nette\Utils\Html | ||
{ | ||
return parent::getControl()->addAttributes([ | ||
'type' => 'color', | ||
'value' => $this->value, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Forms\Controls\ColorPicker. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Forms\Form; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
before(function () { | ||
$_SERVER['REQUEST_METHOD'] = 'POST'; | ||
$_POST = $_FILES = []; | ||
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1'; | ||
Form::initialize(true); | ||
}); | ||
|
||
|
||
test('loadData empty string', function () { | ||
$_POST = ['color' => '']; | ||
|
||
$form = new Form; | ||
$input = $form->addColor('color'); | ||
|
||
Assert::same('#000000', $input->getValue()); | ||
Assert::true($input->isFilled()); | ||
}); | ||
|
||
|
||
test('loadData invalid string', function () { | ||
$_POST = ['color' => '#abc']; | ||
|
||
$form = new Form; | ||
$input = $form->addColor('color'); | ||
|
||
Assert::same('#000000', $input->getValue()); | ||
Assert::true($input->isFilled()); | ||
}); | ||
|
||
|
||
test('loadData valid string', function () { | ||
$_POST = ['color' => '#1020aa']; | ||
|
||
$form = new Form; | ||
$input = $form->addColor('color'); | ||
|
||
Assert::same('#1020aa', $input->getValue()); | ||
Assert::true($input->isFilled()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Forms\Controls\ColorPicker. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Forms\Form; | ||
use Nette\Utils\Html; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
test('', function () { | ||
$form = new Form; | ||
$input = $form->addColor('color') | ||
->setValue('#1020AB'); | ||
|
||
Assert::type(Html::class, $input->getControl()); | ||
Assert::same('<input type="color" name="color" id="frm-color" value="#1020ab">', (string) $input->getControl()); | ||
}); |