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