Skip to content

Commit a57f3da

Browse files
committed
add password action to input field
1 parent 1d27dad commit a57f3da

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed
Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Ajax\semantic\html\collections\form;
43

54
use Ajax\semantic\html\collections\form\traits\TextFieldsTrait;
@@ -8,17 +7,45 @@
87
class HtmlFormInput extends HtmlFormField {
98
use TextFieldsTrait;
109

11-
public function __construct($identifier, $label=NULL,$type="text",$value=NULL,$placeholder=NULL) {
12-
if(!isset($placeholder) && $type==="text")
13-
$placeholder=$label;
14-
parent::__construct("field-".$identifier, new HtmlInput($identifier,$type,$value,$placeholder), $label);
15-
$this->_identifier=$identifier;
10+
public function __construct($identifier, $label = NULL, $type = "text", $value = NULL, $placeholder = NULL) {
11+
if (! isset($placeholder) && $type === "text")
12+
$placeholder = $label;
13+
parent::__construct("field-" . $identifier, new HtmlInput($identifier, $type, $value, $placeholder), $label);
14+
$this->_identifier = $identifier;
1615
}
1716

18-
public function getDataField(){
19-
$field= $this->getField();
20-
if($field instanceof HtmlInput)
21-
$field=$field->getDataField();
17+
public function getDataField() {
18+
$field = $this->getField();
19+
if ($field instanceof HtmlInput)
20+
$field = $field->getDataField();
2221
return $field;
2322
}
23+
24+
/**
25+
* Changes the input type to password and adds an icon
26+
*
27+
* @param string $keyIcon
28+
*/
29+
public function asPassword($keyIcon = 'key') {
30+
$this->setInputType('password');
31+
if ($keyIcon != '') {
32+
$this->addIcon($keyIcon);
33+
}
34+
}
35+
36+
/**
37+
* Adds an action to show/hide the password
38+
*
39+
* @param string $buttonIcon
40+
* @param string $keyIcon
41+
* @param string $slashIcon
42+
* @return mixed|\Ajax\semantic\html\elements\HtmlButton
43+
*/
44+
public function addTogglePasswordAction($buttonIcon = 'eye', $keyIcon = 'key', $slashIcon = 'slash') {
45+
$this->asPassword($keyIcon);
46+
$action = $this->addAction('see');
47+
$action->asIcon($buttonIcon);
48+
$action->onClick('$(this).find(".icon").toggleClass("' . $slashIcon . '");$(this).closest(".field").find("input").attr("type",(_,attr)=>(attr=="text")?"password":"text")');
49+
return $action;
50+
}
2451
}

0 commit comments

Comments
 (0)