Skip to content

Commit 0527437

Browse files
committed
BaseControl::$disabled is bool, added $disabledChoices
1 parent 350e7fd commit 0527437

7 files changed

Lines changed: 20 additions & 22 deletions

File tree

src/Forms/Controls/BaseControl.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con
4141
protected mixed $value = null;
4242
protected Html $control;
4343
protected Html $label;
44-
45-
/** @var bool|bool[] */
46-
protected bool|array $disabled = false;
44+
protected bool $disabled = false;
4745

4846
/** @var array<string, array<class-string, callable(self): mixed>> */
4947
private static array $extMethods = [];
@@ -197,7 +195,7 @@ public function setDisabled(bool $state = true): static
197195
*/
198196
public function isDisabled(): bool
199197
{
200-
return $this->disabled === true;
198+
return $this->disabled;
201199
}
202200

203201

src/Forms/Controls/CheckboxList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getControl(): Html
6464
array_merge($input->attrs, [
6565
'id' => null,
6666
'checked?' => $this->value,
67-
'disabled:' => $this->disabled,
67+
'disabled:' => $this->disabled ?: $this->disabledChoices,
6868
'required' => null,
6969
'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']],
7070
]),
@@ -87,7 +87,7 @@ public function getControlPart(int|string|null $key = null): Html
8787
return parent::getControl()->addAttributes([
8888
'id' => $this->getHtmlId() . '-' . $key,
8989
'checked' => in_array($key, (array) $this->value, strict: true),
90-
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
90+
'disabled' => $this->disabled || isset($this->disabledChoices[$key]),
9191
'required' => null,
9292
'value' => $key,
9393
]);

src/Forms/Controls/ChoiceControl.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
* Choice control that allows single item selection.
1818
*
1919
* @property mixed[] $items
20-
* @property bool|array<int|string,bool> $disabled
2120
*/
2221
abstract class ChoiceControl extends BaseControl
2322
{
23+
/** @var bool[] */
24+
protected array $disabledChoices = [];
2425
private bool $checkDefaultValue = true;
2526

2627
/** @var mixed[] */
@@ -73,7 +74,7 @@ public function getValue(): mixed
7374
{
7475
return $this->value !== null
7576
&& array_key_exists($this->value, $this->items)
76-
&& !isset($this->disabled[$this->value])
77+
&& !isset($this->disabledChoices[$this->value])
7778
? $this->value
7879
: null;
7980
}
@@ -135,12 +136,11 @@ public function getSelectedItem(): mixed
135136
public function setDisabled(bool|array $value = true): static
136137
{
137138
if (!is_array($value)) {
139+
$this->disabledChoices = [];
138140
return parent::setDisabled($value);
139141
}
140-
141-
parent::setDisabled(false);
142-
$this->disabled = array_fill_keys($value, value: true);
143-
return $this;
142+
$this->disabledChoices = array_fill_keys($value, value: true);
143+
return parent::setDisabled(false);
144144
}
145145

146146

src/Forms/Controls/MultiChoiceControl.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
* Choice control that allows multiple items selection.
1818
*
1919
* @property mixed[] $items
20-
* @property bool|array<int|string,bool> $disabled
2120
*/
2221
abstract class MultiChoiceControl extends BaseControl
2322
{
23+
/** @var bool[] */
24+
protected array $disabledChoices = [];
2425
private bool $checkDefaultValue = true;
2526

2627
/** @var mixed[] */
@@ -127,7 +128,7 @@ public function getSelectedItems(): array
127128
{
128129
$res = [];
129130
foreach ($this->value as $key) {
130-
if (isset($this->items[$key]) && !isset($this->disabled[$key])) {
131+
if (isset($this->items[$key]) && !isset($this->disabledChoices[$key])) {
131132
$res[$key] = $this->items[$key];
132133
}
133134
}
@@ -142,12 +143,11 @@ public function getSelectedItems(): array
142143
public function setDisabled(bool|array $value = true): static
143144
{
144145
if (!is_array($value)) {
146+
$this->disabledChoices = [];
145147
return parent::setDisabled($value);
146148
}
147-
148-
parent::setDisabled(false);
149-
$this->disabled = array_fill_keys($value, value: true);
150-
return $this;
149+
$this->disabledChoices = array_fill_keys($value, value: true);
150+
return parent::setDisabled(false);
151151
}
152152

153153

src/Forms/Controls/MultiSelectBox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getControl(): Nette\Utils\Html
7070
return Nette\Forms\Helpers::createSelectBox(
7171
$items,
7272
[
73-
'disabled:' => is_array($this->disabled) ? $this->disabled : null,
73+
'disabled:' => $this->disabledChoices,
7474
] + $this->optionAttributes,
7575
$this->value,
7676
)->addAttributes(parent::getControl()->attrs)->multiple(true);

src/Forms/Controls/RadioList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function getControl(): Html
6262
array_merge($input->attrs, [
6363
'id:' => $ids,
6464
'checked?' => $this->value,
65-
'disabled:' => $this->disabled,
65+
'disabled:' => $this->disabled ?: $this->disabledChoices,
6666
'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']],
6767
]),
6868
['for:' => $ids] + $this->itemLabel->attrs,
@@ -84,7 +84,7 @@ public function getControlPart(int|string|null $key = null): Html
8484
return parent::getControl()->addAttributes([
8585
'id' => $this->getHtmlId() . '-' . $key,
8686
'checked' => in_array($key, (array) $this->value, strict: true),
87-
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
87+
'disabled' => $this->disabled || isset($this->disabledChoices[$key]),
8888
'value' => $key,
8989
]);
9090
}

src/Forms/Controls/SelectBox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function getControl(): Nette\Utils\Html
100100
}
101101

102102
$attrs = $this->optionAttributes;
103-
$attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : [];
103+
$attrs['disabled:'] = $this->disabledChoices;
104104

105105
$selected = $this->value;
106106
if ($this->prompt !== false) {

0 commit comments

Comments
 (0)