This repository was archived by the owner on Jan 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathFormSelect.php
89 lines (73 loc) · 2.29 KB
/
FormSelect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace ProtoneMedia\LaravelFormComponents\Components;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class FormSelect extends Component
{
use HandlesValidationErrors;
use HandlesBoundValues;
public string $name;
public string $label;
public $options;
public $selectedKey;
public bool $multiple;
public bool $floating;
public string $placeholder;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct(
string $name,
string $label = '',
$options = [],
$bind = null,
$default = null,
bool $multiple = false,
bool $showErrors = true,
bool $manyRelation = false,
bool $floating = false,
string $placeholder = ''
) {
$this->name = $name;
$this->label = $label;
$this->options = $options;
$this->manyRelation = $manyRelation;
$this->placeholder = $placeholder;
if ($this->isNotWired()) {
$inputName = static::convertBracketsToDots(Str::before($name, '[]'));
if (is_null($default)) {
$default = $this->getBoundValue($bind, $inputName);
}
$this->selectedKey = old($inputName, $default);
if ($this->selectedKey instanceof Arrayable) {
$this->selectedKey = $this->selectedKey->toArray();
}
if ($this->selectedKey instanceof \BackedEnum) {
$this->selectedKey = $this->selectedKey->value;
}
if ($this->selectedKey instanceof \UnitEnum) {
$this->selectedKey = $this->selectedKey->name;
}
}
$this->multiple = $multiple;
$this->showErrors = $showErrors;
$this->floating = $floating && !$multiple;
}
public function isSelected($key): bool
{
if ($this->isWired()) {
return false;
}
return in_array($key, Arr::wrap($this->selectedKey));
}
public function nothingSelected(): bool
{
if ($this->isWired()) {
return false;
}
return is_array($this->selectedKey) ? empty($this->selectedKey) : is_null($this->selectedKey);
}
}