From 1d9577bc93820049823d2008a0b3ef191421fba3 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 | 14 ++++++ tests/Forms/Controls.TextInput.color.phpt | 54 +++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 tests/Forms/Controls.TextInput.color.phpt diff --git a/src/Forms/Container.php b/src/Forms/Container.php index db6f5871d..c07a09255 100644 --- a/src/Forms/Container.php +++ b/src/Forms/Container.php @@ -522,6 +522,20 @@ public function addMultiSelect( } + /** + * Adds color picker. + * @param string|object|null $label + */ + public function addColor(string $name, $label = null): Controls\TextInput + { + return $this[$name] = (new Controls\TextInput($label)) + ->setNullable() + ->setHtmlType('color') + ->setOption('type', 'color') + ->addRule(Form::Pattern, 'Invalid color', '#[0-9a-fA-F]{6}'); + } + + /** * Adds button used to submit form. * @param string|object|null $caption diff --git a/tests/Forms/Controls.TextInput.color.phpt b/tests/Forms/Controls.TextInput.color.phpt new file mode 100644 index 000000000..4770d09cd --- /dev/null +++ b/tests/Forms/Controls.TextInput.color.phpt @@ -0,0 +1,54 @@ + '']; + + $form = new Form; + $input = $form->addColor('color'); + + Assert::null($input->getValue()); + Assert::false($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()); +}); + + + +test('', function () { + $form = new Form; + $input = $form->addColor('color') + ->setValue('#1020AB'); + + Assert::type(Html::class, $input->getControl()); + Assert::same('', (string) $input->getControl()); +});