-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathSelectBox.php
More file actions
163 lines (131 loc) · 3.28 KB
/
SelectBox.php
File metadata and controls
163 lines (131 loc) · 3.28 KB
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
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Forms\Controls;
use Nette;
/**
* Select box control that allows single item selection.
*/
class SelectBox extends ChoiceControl
{
/** validation rule */
public const VALID = ':selectBoxValid';
/** @var array of option / optgroup */
private $options = [];
/** @var mixed */
private $prompt = false;
/** @var bool */
private $autoDefault = false;
/** @var array */
private $optionAttributes = [];
public function __construct($label = null, array $items = null)
{
parent::__construct($label, $items);
$this->setOption('type', 'select');
$this->addCondition(Nette\Forms\Form::BLANK)
->addRule([$this, 'isOk'], Nette\Forms\Validator::$messages[self::VALID]);
}
/**
* Sets first prompt item in select box.
* @param string|object $prompt
* @return static
*/
public function setPrompt($prompt)
{
$this->prompt = $prompt;
$this->setDefaultValueAuto(null, true);
return $this;
}
/**
* Returns first prompt item?
* @return mixed
*/
public function getPrompt()
{
return $this->prompt;
}
public function setDefaultValue($value)
{
$this->autoDefault = true;
return parent::setDefaultValue($value);
}
/**
* Sets options and option groups from which to choose.
* @return static
*/
public function setItems(array $items, bool $useKeys = true)
{
if (!$useKeys) {
$res = [];
foreach ($items as $key => $value) {
unset($items[$key]);
if (is_array($value)) {
foreach ($value as $val) {
$res[$key][(string) $val] = $val;
}
} else {
$res[(string) $value] = $value;
}
}
$items = $res;
}
$this->options = $items;
parent::setItems(Nette\Utils\Arrays::flatten($items, true));
if ($this->prompt === false && $this->items) {
reset($this->items);
$this->setDefaultValueAuto(key($this->items));
}
return $this;
}
/**
* Generates control's HTML element.
*/
public function getControl(): Nette\Utils\Html
{
$items = $this->prompt === false ? [] : ['' => $this->translate($this->prompt)];
foreach ($this->options as $key => $value) {
$items[is_array($value) ? $this->translate($key) : $key] = $this->translate($value);
}
return Nette\Forms\Helpers::createSelectBox(
$items,
[
'disabled:' => is_array($this->disabled) ? $this->disabled : null,
] + $this->optionAttributes,
$this->value
)->addAttributes(parent::getControl()->attrs);
}
public function setHtmlAttribute(string $name, $value = true)
{
if ($name === 'size' && $value > 1) {
$this->setDefaultValueAuto(null, true);
}
return parent::setHtmlAttribute($name, $value);
}
/**
* @return static
*/
public function addOptionAttributes(array $attributes)
{
$this->optionAttributes = $attributes + $this->optionAttributes;
return $this;
}
public function isOk(): bool
{
return $this->isDisabled()
|| $this->prompt !== false
|| $this->getValue() !== null
|| !$this->options
|| $this->control->size > 1;
}
private function setDefaultValueAuto($value, bool $autoDefault = false)
{
if ($this->autoDefault === false) {
$this->setDefaultValue($value);
$this->autoDefault = $autoDefault;
}
return $this;
}
}