From e38d90588242a3e28d66a435a7baa5a15b1b33e3 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 31 Oct 2023 15:09:12 +0100 Subject: [PATCH] added addColor() --- src/Forms/Container.php | 10 +++ src/Forms/Controls/ColorPicker.php | 61 +++++++++++++++++++ .../Forms/Controls.ColorPicker.loadData.phpt | 53 ++++++++++++++++ tests/Forms/Controls.ColorPicker.render.phpt | 23 +++++++ 4 files changed, 147 insertions(+) create mode 100644 src/Forms/Controls/ColorPicker.php create mode 100644 tests/Forms/Controls.ColorPicker.loadData.phpt create mode 100644 tests/Forms/Controls.ColorPicker.render.phpt diff --git a/src/Forms/Container.php b/src/Forms/Container.php index db6f5871d..022795047 100644 --- a/src/Forms/Container.php +++ b/src/Forms/Container.php @@ -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 diff --git a/src/Forms/Controls/ColorPicker.php b/src/Forms/Controls/ColorPicker.php new file mode 100644 index 000000000..5e9ccfb53 --- /dev/null +++ b/src/Forms/Controls/ColorPicker.php @@ -0,0 +1,61 @@ +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, + ]); + } +} diff --git a/tests/Forms/Controls.ColorPicker.loadData.phpt b/tests/Forms/Controls.ColorPicker.loadData.phpt new file mode 100644 index 000000000..a8824c6d3 --- /dev/null +++ b/tests/Forms/Controls.ColorPicker.loadData.phpt @@ -0,0 +1,53 @@ + '']; + + $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()); +}); diff --git a/tests/Forms/Controls.ColorPicker.render.phpt b/tests/Forms/Controls.ColorPicker.render.phpt new file mode 100644 index 000000000..17ad5a57f --- /dev/null +++ b/tests/Forms/Controls.ColorPicker.render.phpt @@ -0,0 +1,23 @@ +addColor('color') + ->setValue('#1020AB'); + + Assert::type(Html::class, $input->getControl()); + Assert::same('', (string) $input->getControl()); +});