Skip to content

Commit 2d5a680

Browse files
committed
BaseControl::$disabled is bool, added $disabledChoices
1 parent aaf2afe commit 2d5a680

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
@@ -44,9 +44,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con
4444
protected mixed $value = null;
4545
protected Html $control;
4646
protected Html $label;
47-
48-
/** @var bool|bool[] */
49-
protected bool|array $disabled = false;
47+
protected bool $disabled = false;
5048

5149
/** @var callable[][] extension methods */
5250
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
@@ -59,7 +59,7 @@ public function getControl(): Html
5959
array_merge($input->attrs, [
6060
'id' => null,
6161
'checked?' => $this->value,
62-
'disabled:' => $this->disabled,
62+
'disabled:' => $this->disabled ?: $this->disabledChoices,
6363
'required' => null,
6464
'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']],
6565
]),
@@ -82,7 +82,7 @@ public function getControlPart($key = null): Html
8282
return parent::getControl()->addAttributes([
8383
'id' => $this->getHtmlId() . '-' . $key,
8484
'checked' => in_array($key, (array) $this->value, strict: true),
85-
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
85+
'disabled' => $this->disabled || isset($this->disabledChoices[$key]),
8686
'required' => null,
8787
'value' => $key,
8888
]);

src/Forms/Controls/ChoiceControl.php

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

@@ -68,7 +70,7 @@ public function setValue($value): static
6870
public function getValue(): mixed
6971
{
7072
return array_key_exists($this->value, $this->items)
71-
&& !isset($this->disabled[$this->value])
73+
&& !isset($this->disabledChoices[$this->value])
7274
? $this->value
7375
: null;
7476
}
@@ -127,12 +129,11 @@ public function getSelectedItem(): mixed
127129
public function setDisabled(bool|array $value = true): static
128130
{
129131
if (!is_array($value)) {
132+
$this->disabledChoices = [];
130133
return parent::setDisabled($value);
131134
}
132-
133-
parent::setDisabled(false);
134-
$this->disabled = array_fill_keys($value, value: true);
135-
return $this;
135+
$this->disabledChoices = array_fill_keys($value, value: true);
136+
return parent::setDisabled(false);
136137
}
137138

138139

src/Forms/Controls/MultiChoiceControl.php

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

@@ -118,7 +120,7 @@ public function getSelectedItems(): array
118120
{
119121
$res = [];
120122
foreach ($this->value as $key) {
121-
if (isset($this->items[$key]) && !isset($this->disabled[$key])) {
123+
if (isset($this->items[$key]) && !isset($this->disabledChoices[$key])) {
122124
$res[$key] = $this->items[$key];
123125
}
124126
}
@@ -132,12 +134,11 @@ public function getSelectedItems(): array
132134
public function setDisabled(bool|array $value = true): static
133135
{
134136
if (!is_array($value)) {
137+
$this->disabledChoices = [];
135138
return parent::setDisabled($value);
136139
}
137-
138-
parent::setDisabled(false);
139-
$this->disabled = array_fill_keys($value, value: true);
140-
return $this;
140+
$this->disabledChoices = array_fill_keys($value, value: true);
141+
return parent::setDisabled(false);
141142
}
142143

143144

src/Forms/Controls/MultiSelectBox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function getControl(): Nette\Utils\Html
6565
return Nette\Forms\Helpers::createSelectBox(
6666
$items,
6767
[
68-
'disabled:' => is_array($this->disabled) ? $this->disabled : null,
68+
'disabled:' => $this->disabledChoices,
6969
] + $this->optionAttributes,
7070
$this->value,
7171
)->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
@@ -57,7 +57,7 @@ public function getControl(): Html
5757
array_merge($input->attrs, [
5858
'id:' => $ids,
5959
'checked?' => $this->value,
60-
'disabled:' => $this->disabled,
60+
'disabled:' => $this->disabled ?: $this->disabledChoices,
6161
'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']],
6262
]),
6363
['for:' => $ids] + $this->itemLabel->attrs,
@@ -79,7 +79,7 @@ public function getControlPart($key = null): Html
7979
return parent::getControl()->addAttributes([
8080
'id' => $this->getHtmlId() . '-' . $key,
8181
'checked' => in_array($key, (array) $this->value, strict: true),
82-
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
82+
'disabled' => $this->disabled || isset($this->disabledChoices[$key]),
8383
'value' => $key,
8484
]);
8585
}

src/Forms/Controls/SelectBox.php

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

9797
$attrs = $this->optionAttributes;
98-
$attrs['disabled:'] = is_array($this->disabled) ? $this->disabled : [];
98+
$attrs['disabled:'] = $this->disabledChoices;
9999

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

0 commit comments

Comments
 (0)