|
1 | 1 | <?php |
2 | | - |
3 | 2 | namespace Ajax\semantic\html\collections\form; |
4 | 3 |
|
5 | 4 | use Ajax\semantic\html\collections\form\traits\TextFieldsTrait; |
|
8 | 7 | class HtmlFormInput extends HtmlFormField { |
9 | 8 | use TextFieldsTrait; |
10 | 9 |
|
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; |
16 | 15 | } |
17 | 16 |
|
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(); |
22 | 21 | return $field; |
23 | 22 | } |
| 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 | + } |
24 | 51 | } |
0 commit comments