1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xetaravel\View\Components; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Contracts\View\View; |
8
|
|
|
use Illuminate\View\Component; |
9
|
|
|
|
10
|
|
|
class Password extends Component |
11
|
|
|
{ |
12
|
|
|
public string $uuid; |
13
|
|
|
|
14
|
|
|
public function __construct( |
15
|
|
|
public ?string $label = null, |
16
|
|
|
public ?string $icon = null, |
17
|
|
|
public ?string $iconRight = null, |
18
|
|
|
public ?string $hint = null, |
19
|
|
|
public ?string $hintClass = 'fieldset-label', |
20
|
|
|
public ?string $prefix = null, |
21
|
|
|
public ?string $suffix = null, |
22
|
|
|
public ?bool $inline = false, |
23
|
|
|
public ?bool $clearable = false, |
24
|
|
|
|
25
|
|
|
// Password |
26
|
|
|
public ?string $passwordIcon = 'heroicon-o-eye-slash', |
27
|
|
|
public ?string $passwordVisibleIcon = 'heroicon-o-eye', |
28
|
|
|
public ?bool $right = false, |
29
|
|
|
public ?bool $onlyPassword = false, |
30
|
|
|
|
31
|
|
|
// Slots |
32
|
|
|
public mixed $prepend = null, |
33
|
|
|
public mixed $append = null, |
34
|
|
|
|
35
|
|
|
// Validations |
36
|
|
|
public ?string $errorField = null, |
37
|
|
|
public ?string $errorClass = 'text-error', |
38
|
|
|
public ?bool $omitError = false, |
39
|
|
|
public ?bool $firstErrorOnly = false, |
40
|
|
|
) { |
41
|
|
|
$this->uuid = md5(serialize($this)); |
42
|
|
|
|
43
|
|
|
// Cannot use a left icon when password toggle should be shown on the left side. |
44
|
|
|
if (($this->icon && ! $this->right) && ! $this->onlyPassword) { |
|
|
|
|
45
|
|
|
throw new Exception("Cannot use `icon` without providing `right` or `onlyPassword`."); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Cannot use a right icon when password toggle should be shown on the right side. |
49
|
|
|
if (($this->iconRight && $this->right) && ! $this->onlyPassword) { |
|
|
|
|
50
|
|
|
throw new Exception("Cannot use `iconRight` when providing `right` and not providing `onlyPassword`."); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function modelName(): ?string |
55
|
|
|
{ |
56
|
|
|
return $this->attributes->whereStartsWith('wire:model')->first(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function errorFieldName(): ?string |
60
|
|
|
{ |
61
|
|
|
return $this->errorField ?? $this->modelName(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function placeToggleLeft(): bool |
65
|
|
|
{ |
66
|
|
|
return (! $this->icon && ! $this->right) && ! $this->onlyPassword; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function placeToggleRight(): bool |
70
|
|
|
{ |
71
|
|
|
return (! $this->iconRight && $this->right) && ! $this->onlyPassword; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the view / contents that represent the component. |
76
|
|
|
*/ |
77
|
|
|
public function render(): View|Closure|string |
78
|
|
|
{ |
79
|
|
|
return view('components.password'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.