From bce30e9d044729bfa9d9b1156522ced0fa662d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Konvi=C4=8Dka?= Date: Mon, 29 Jun 2026 10:48:32 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20form=20prvek=20addPasswordReveal=20pro?= =?UTF-8?q?=20maskovan=C3=BD=20text=20s=20odkryt=C3=ADm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controls/PasswordRevealInput.php | 118 +++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/Controls/PasswordRevealInput.php diff --git a/src/Controls/PasswordRevealInput.php b/src/Controls/PasswordRevealInput.php new file mode 100644 index 0000000..9e79d36 --- /dev/null +++ b/src/Controls/PasswordRevealInput.php @@ -0,0 +1,118 @@ +setHtmlType('password'); + $this->getControlPrototype()->appendAttribute('class', self::INPUT_CLASS); + } + + public function getControl(): Html + { + $input = parent::getControl(); + + $button = Html::el('button') + ->setAttribute('type', 'button') + ->setAttribute('tabindex', '-1') + ->appendAttribute('class', 'btn btn-outline-secondary ' . self::TOGGLE_CLASS) + ->addHtml(Html::el('i')->setAttribute('class', 'fas fa-eye')); + + $group = Html::el('div') + ->setAttribute('class', 'input-group') + ->addHtml($input) + ->addHtml($button); + + if (!self::$scriptRendered) { + self::$scriptRendered = true; + $group->addHtml(self::getScript()); + } + + return $group; + } + + public static function addPasswordReveal(Container $container, string $name, $label = null): self + { + $component = new self($label); + $container->addComponent($component, $name); + return $component; + } + + public static function register(): void + { + Form::extensionMethod('addPasswordReveal', [self::class, 'addPasswordReveal']); + Container::extensionMethod('addPasswordReveal', [self::class, 'addPasswordReveal']); + } + + private static function getScript(): Html + { + $js = << self::INPUT_CLASS, + '{TOGGLE_CLASS}' => self::TOGGLE_CLASS, + ]); + + return Html::el('script')->setHtml($js); + } +}