Skip to content

Commit 32a3c08

Browse files
committed
BaseControl::$disabled is bool, added $disabledChoices
1 parent d44ba1d commit 32a3c08

File tree

7 files changed

+20
-20
lines changed

7 files changed

+20
-20
lines changed

src/Forms/Controls/BaseControl.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con
4545
protected mixed $value = null;
4646
protected Html $control;
4747
protected Html $label;
48-
49-
/** @var bool|bool[] */
50-
protected bool|array $disabled = false;
48+
protected bool $disabled = false;
5149

5250
/** @var callable[][] extension methods */
5351
private static array $extMethods = [];
@@ -198,7 +196,7 @@ public function setDisabled(bool $state = true): static
198196
*/
199197
public function isDisabled(): bool
200198
{
201-
return $this->disabled === true;
199+
return $this->disabled;
202200
}
203201

204202

src/Forms/Controls/CheckboxList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function getControl(): Html
6060
array_merge($input->attrs, [
6161
'id' => null,
6262
'checked?' => $this->value,
63-
'disabled:' => $this->disabled,
63+
'disabled:' => $this->disabled ?: $this->disabledChoices,
6464
'required' => null,
6565
'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']],
6666
]),
@@ -83,7 +83,7 @@ public function getControlPart($key = null): Html
8383
return parent::getControl()->addAttributes([
8484
'id' => $this->getHtmlId() . '-' . $key,
8585
'checked' => in_array($key, (array) $this->value, strict: true),
86-
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
86+
'disabled' => $this->disabled || isset($this->disabledChoices[$key]),
8787
'required' => null,
8888
'value' => $key,
8989
]);

src/Forms/Controls/ChoiceControl.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
abstract class ChoiceControl extends BaseControl
2323
{
24+
/** @var bool[] */
25+
protected array $disabledChoices = [];
2426
private bool $checkDefaultValue = true;
2527
private array $items = [];
2628

@@ -70,7 +72,7 @@ public function getValue(): mixed
7072
{
7173
return $this->value !== null
7274
&& array_key_exists($this->value, $this->items)
73-
&& !isset($this->disabled[$this->value])
75+
&& !isset($this->disabledChoices[$this->value])
7476
? $this->value
7577
: null;
7678
}
@@ -129,12 +131,11 @@ public function getSelectedItem(): mixed
129131
public function setDisabled(bool|array $value = true): static
130132
{
131133
if (!is_array($value)) {
134+
$this->disabledChoices = [];
132135
return parent::setDisabled($value);
133136
}
134-
135-
parent::setDisabled(false);
136-
$this->disabled = array_fill_keys($value, value: true);
137-
return $this;
137+
$this->disabledChoices = array_fill_keys($value, value: true);
138+
return parent::setDisabled(false);
138139
}
139140

140141

src/Forms/Controls/MultiChoiceControl.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
abstract class MultiChoiceControl extends BaseControl
2323
{
24+
/** @var bool[] */
25+
protected array $disabledChoices = [];
2426
private bool $checkDefaultValue = true;
2527
private array $items = [];
2628

@@ -119,7 +121,7 @@ public function getSelectedItems(): array
119121
{
120122
$res = [];
121123
foreach ($this->value as $key) {
122-
if (isset($this->items[$key]) && !isset($this->disabled[$key])) {
124+
if (isset($this->items[$key]) && !isset($this->disabledChoices[$key])) {
123125
$res[$key] = $this->items[$key];
124126
}
125127
}
@@ -133,12 +135,11 @@ public function getSelectedItems(): array
133135
public function setDisabled(bool|array $value = true): static
134136
{
135137
if (!is_array($value)) {
138+
$this->disabledChoices = [];
136139
return parent::setDisabled($value);
137140
}
138-
139-
parent::setDisabled(false);
140-
$this->disabled = array_fill_keys($value, value: true);
141-
return $this;
141+
$this->disabledChoices = array_fill_keys($value, value: true);
142+
return parent::setDisabled(false);
142143
}
143144

144145

src/Forms/Controls/MultiSelectBox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getControl(): Nette\Utils\Html
6666
return Nette\Forms\Helpers::createSelectBox(
6767
$items,
6868
[
69-
'disabled:' => is_array($this->disabled) ? $this->disabled : null,
69+
'disabled:' => $this->disabledChoices,
7070
] + $this->optionAttributes,
7171
$this->value,
7272
)->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
@@ -58,7 +58,7 @@ public function getControl(): Html
5858
array_merge($input->attrs, [
5959
'id:' => $ids,
6060
'checked?' => $this->value,
61-
'disabled:' => $this->disabled,
61+
'disabled:' => $this->disabled ?: $this->disabledChoices,
6262
'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']],
6363
]),
6464
['for:' => $ids] + $this->itemLabel->attrs,
@@ -80,7 +80,7 @@ public function getControlPart($key = null): Html
8080
return parent::getControl()->addAttributes([
8181
'id' => $this->getHtmlId() . '-' . $key,
8282
'checked' => in_array($key, (array) $this->value, strict: true),
83-
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
83+
'disabled' => $this->disabled || isset($this->disabledChoices[$key]),
8484
'value' => $key,
8585
]);
8686
}

src/Forms/Controls/SelectBox.php

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

9898
$attrs = $this->optionAttributes;
99-
$attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : [];
99+
$attrs['disabled:'] = $this->disabledChoices;
100100

101101
$selected = $this->value;
102102
if ($this->prompt !== false) {

0 commit comments

Comments
 (0)