-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSelect.php
187 lines (142 loc) · 3.93 KB
/
Select.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace Laravolt\SemanticForm\Elements;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
class Select extends FormControl
{
protected $options = [];
protected $selected;
protected $attributes = [
'class' => 'ui dropdown search',
];
public function __construct($name, $options = [])
{
if ($options instanceof Collection) {
$options = $options->toArray();
}
$this->setName($name);
$this->setOptions($options);
}
public function select($option)
{
$this->selected = $option;
return $this;
}
public function value($value)
{
$this->value = $value;
return $this->select($value);
}
protected function setOptions($options)
{
$this->options = $options;
}
public function options($options)
{
$this->setOptions($options);
return $this;
}
public function render()
{
if ($this->label) {
$element = clone $this;
$element->label = false;
$field = new Field($this->label, $element);
if ($element->getAttribute('readonly')) {
$field->addClass('disabled readonly');
}
return $this->decorateField($field)->render();
}
$this->beforeRender();
$result = '<select';
$result .= $this->renderAttributes();
$result .= '>';
$result .= $this->renderOptions();
$result .= '</select>';
$result .= $this->renderHint();
return $result;
}
protected function renderOptions()
{
$result = '';
foreach ($this->options as $value => $label) {
if (is_array($label)) {
$result .= $this->renderOptGroup($value, $label);
continue;
}
$result .= $this->renderOption($value, $label);
}
return $result;
}
protected function renderOptGroup($label, $options)
{
$result = "<optgroup label=\"{$label}\">";
foreach ($options as $value => $label) {
$result .= $this->renderOption($value, $label);
}
$result .= '</optgroup>';
return $result;
}
protected function renderOption($value, $label)
{
$option = '<option ';
$option .= 'value="'.$value.'"';
$option .= $this->isSelected($value) ? ' selected' : '';
$option .= '>';
$option .= $label;
$option .= '</option>';
return $option;
}
protected function isSelected($value)
{
$selected = $this->selected;
if (is_string($selected)) {
$selected = html_entity_decode($selected);
}
return in_array($value, (array) $selected);
}
public function addOption($value, $label)
{
$this->options[$value] = $label;
return $this;
}
public function prependOption($value, $label)
{
$this->options = Arr::prepend($this->options, $label, $value);
return $this;
}
public function appendOption($value, $label)
{
$this->options = Arr::add($this->options, $value, $label);
return $this;
}
public function placeholder($label = '-- Select --')
{
return $this->prependOption('', $label);
}
public function defaultValue($value)
{
if ($this->selected !== null) {
return $this;
}
$this->select($value);
return $this;
}
public function multiple()
{
$name = $this->attributes['name'];
if (substr($name, -2) != '[]') {
$name .= '[]';
}
$this->setName($name);
$this->setAttribute('multiple', 'multiple');
return $this;
}
public function displayValue()
{
if (is_string($this->value)) {
return Arr::get($this->options, $this->value);
}
return null;
}
}