Skip to content

Commit

Permalink
added addColor()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 1, 2023
1 parent 87fcb76 commit e38d905
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,16 @@ public function addMultiSelect(
}


/**
* Adds color picker.
* @param string|object|null $label
*/
public function addColor(string $name, $label = null): Controls\ColorPicker
{
return $this[$name] = new Controls\ColorPicker($label);
}


/**
* Adds button used to submit form.
* @param string|object|null $caption
Expand Down
61 changes: 61 additions & 0 deletions src/Forms/Controls/ColorPicker.php
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,
]);
}
}
53 changes: 53 additions & 0 deletions tests/Forms/Controls.ColorPicker.loadData.phpt
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());
});
23 changes: 23 additions & 0 deletions tests/Forms/Controls.ColorPicker.render.phpt
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());
});

0 comments on commit e38d905

Please sign in to comment.