|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Oscer\Cms\Backend\Resources\Fields; |
|
4
|
|
|
|
|
5
|
|
|
class SelectField extends Field |
|
6
|
|
|
{ |
|
7
|
|
|
public string $component = 'SelectField'; |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
public array $options = []; |
|
10
|
|
|
|
|
11
|
|
|
public bool $filterable = false; |
|
12
|
|
|
|
|
13
|
|
|
public bool $searchable = false; |
|
14
|
|
|
|
|
15
|
|
|
public bool $multiple = false; |
|
16
|
|
|
|
|
17
|
|
|
public string $placeholder = ''; |
|
18
|
|
|
|
|
19
|
|
|
protected array $with = ['options', 'filterable', 'searchable', 'multiple', 'placeholder']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Each option must have a 'name', a 'label' and a 'value' like this |
|
27
|
|
|
* ['name' => 'foo', 'label' => 'bar', 'value' => 'baz']. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function options(array $options) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->options = $this->filterOptions($options); |
|
32
|
|
|
|
|
33
|
|
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Filter an array of options to provide consistent data. Therefore |
|
38
|
|
|
* remove data that does not provide at least a 'name' and |
|
39
|
|
|
* fill 'label' & 'value' with 'name' if they aren't |
|
40
|
|
|
* set neither. |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function filterOptions(array $options) |
|
43
|
|
|
{ |
|
44
|
|
|
$data = []; |
|
45
|
|
|
foreach ($options as $option) { |
|
46
|
|
|
if ($name = is_string($option) ? $option : $option['name']) { |
|
47
|
|
|
$data[] = [ |
|
48
|
|
|
'name' => $name, |
|
49
|
|
|
'label' => $option['label'] ?? $name, |
|
50
|
|
|
'value' => $option['value'] ?? $name, |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $data; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function searchable() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->searchable = true; |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function filterable() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->filterable = true; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function multiple() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->multiple = true; |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set a custom placeholder. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function placeholder(string $placeholder) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->placeholder = $placeholder; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|