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); + } +}