|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Html\Elements; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Spatie\Html\Selectable; |
|
7
|
|
|
use Spatie\Html\BaseElement; |
|
8
|
|
|
|
|
9
|
|
|
class Select extends BaseElement |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var string */ |
|
12
|
|
|
protected $tag = 'select'; |
|
13
|
|
|
|
|
14
|
|
|
/** @var array */ |
|
15
|
|
|
protected $options = []; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string|iterable */ |
|
18
|
|
|
protected $value = ''; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @return static |
|
22
|
|
|
*/ |
|
23
|
|
|
public function multiple() |
|
24
|
|
|
{ |
|
25
|
|
|
$element = $this->attribute('multiple'); |
|
26
|
|
|
|
|
27
|
|
|
$element->applyValueToOptions(); |
|
28
|
|
|
|
|
29
|
|
|
return $element; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $name |
|
34
|
|
|
* |
|
35
|
|
|
* @return static |
|
36
|
|
|
*/ |
|
37
|
|
|
public function name(?string $name) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->attribute('name', $name); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param iterable $options |
|
44
|
|
|
* |
|
45
|
|
|
* @return static |
|
46
|
|
|
*/ |
|
47
|
|
|
public function options(iterable $options) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->addChildren($options, function ($text, $value) { |
|
50
|
|
|
return Option::create() |
|
51
|
|
|
->value($value) |
|
52
|
|
|
->text($text) |
|
53
|
|
|
->selectedIf($value === $this->value); |
|
54
|
|
|
}); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function placeholder(string $text) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->prependChild( |
|
60
|
|
|
Option::create() |
|
61
|
|
|
->value('') |
|
62
|
|
|
->text($text) |
|
63
|
|
|
->selectedIf(! $this->hasSelection()) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string|iterable $value |
|
69
|
|
|
* |
|
70
|
|
|
* @return static |
|
71
|
|
|
*/ |
|
72
|
|
|
public function value($value) |
|
73
|
|
|
{ |
|
74
|
|
|
$element = clone $this; |
|
75
|
|
|
|
|
76
|
|
|
$element->value = $value; |
|
77
|
|
|
|
|
78
|
|
|
$element->applyValueToOptions(); |
|
79
|
|
|
|
|
80
|
|
|
return $element; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function hasSelection(): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->children->contains->hasAttribute('selected'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function applyValueToOptions() |
|
89
|
|
|
{ |
|
90
|
|
|
$value = Collection::make($this->value); |
|
91
|
|
|
|
|
92
|
|
|
if (! $this->hasAttribute('multiple')) { |
|
93
|
|
|
$value = $value->take(1); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->children = $this->children->map(function ($child) use ($value) { |
|
97
|
|
|
if ($child instanceof Selectable) { |
|
98
|
|
|
return $child->selectedIf($value->contains($child->getAttribute('value'))); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $child; |
|
102
|
|
|
}); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|