Skip to content

Commit 3cf4861

Browse files
committed
BaseControl::$disabled is bool, added $disabledChoices
1 parent 282c6cb commit 3cf4861

7 files changed

Lines changed: 20 additions & 20 deletions

File tree

src/Forms/Controls/BaseControl.php

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

5452
/** @var array<string, array<class-string, callable(self): mixed>> */
5553
private static array $extMethods = [];
@@ -203,7 +201,7 @@ public function setDisabled(bool $state = true): static
203201
*/
204202
public function isDisabled(): bool
205203
{
206-
return $this->disabled === true;
204+
return $this->disabled;
207205
}
208206

209207

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
abstract class ChoiceControl extends BaseControl
2424
{
25+
/** @var bool[] */
26+
protected array $disabledChoices = [];
2527
private bool $checkDefaultValue = true;
2628

2729
/** @var mixed[] */
@@ -74,7 +76,7 @@ public function getValue(): mixed
7476
{
7577
return $this->value !== null
7678
&& array_key_exists($this->value, $this->items)
77-
&& !isset($this->disabled[$this->value])
79+
&& !isset($this->disabledChoices[$this->value])
7880
? $this->value
7981
: null;
8082
}
@@ -136,12 +138,11 @@ public function getSelectedItem(): mixed
136138
public function setDisabled(bool|array $value = true): static
137139
{
138140
if (!is_array($value)) {
141+
$this->disabledChoices = [];
139142
return parent::setDisabled($value);
140143
}
141-
142-
parent::setDisabled(false);
143-
$this->disabled = array_fill_keys($value, value: true);
144-
return $this;
144+
$this->disabledChoices = array_fill_keys($value, value: true);
145+
return parent::setDisabled(false);
145146
}
146147

147148

src/Forms/Controls/MultiChoiceControl.php

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

2729
/** @var mixed[] */
@@ -128,7 +130,7 @@ public function getSelectedItems(): array
128130
{
129131
$res = [];
130132
foreach ($this->value as $key) {
131-
if (isset($this->items[$key]) && !isset($this->disabled[$key])) {
133+
if (isset($this->items[$key]) && !isset($this->disabledChoices[$key])) {
132134
$res[$key] = $this->items[$key];
133135
}
134136
}
@@ -143,12 +145,11 @@ public function getSelectedItems(): array
143145
public function setDisabled(bool|array $value = true): static
144146
{
145147
if (!is_array($value)) {
148+
$this->disabledChoices = [];
146149
return parent::setDisabled($value);
147150
}
148-
149-
parent::setDisabled(false);
150-
$this->disabled = array_fill_keys($value, value: true);
151-
return $this;
151+
$this->disabledChoices = array_fill_keys($value, value: true);
152+
return parent::setDisabled(false);
152153
}
153154

154155

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)