Skip to content

Commit 75fd44f

Browse files
committed
BaseControl::$disabled is bool, added $disabledChoices
1 parent 839e5dc commit 75fd44f

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
@@ -46,9 +46,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con
4646
protected mixed $value = null;
4747
protected Html $control;
4848
protected Html $label;
49-
50-
/** @var bool|bool[] */
51-
protected bool|array $disabled = false;
49+
protected bool $disabled = false;
5250

5351
/** @var array<string, array<class-string, callable(static): mixed>> */
5452
private static array $extMethods = [];
@@ -196,7 +194,7 @@ public function setDisabled(bool $state = true): static
196194
*/
197195
public function isDisabled(): bool
198196
{
199-
return $this->disabled === true;
197+
return $this->disabled;
200198
}
201199

202200

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
]),
@@ -91,7 +91,7 @@ public function getControlPart(int|string|null $key = null): Html
9191
return parent::getControl()->addAttributes([
9292
'id' => $this->getHtmlId() . '-' . $key,
9393
'checked' => in_array($key, (array) $this->value, strict: true),
94-
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
94+
'disabled' => $this->disabled || isset($this->disabledChoices[$key]),
9595
'required' => null,
9696
'value' => $key,
9797
]);

src/Forms/Controls/ChoiceControl.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
* Choice control that allows single item selection.
1616
*
1717
* @property mixed[] $items
18-
* @property bool|array<bool> $disabled
1918
* @property-deprecated mixed $selectedItem
2019
*/
2120
abstract class ChoiceControl extends BaseControl
2221
{
22+
/** @var bool[] */
23+
protected array $disabledChoices = [];
2324
private bool $checkDefaultValue = true;
2425

2526
/** @var mixed[] */
@@ -72,7 +73,7 @@ public function getValue(): mixed
7273
{
7374
return $this->value !== null
7475
&& array_key_exists($this->value, $this->items)
75-
&& !isset($this->disabled[$this->value])
76+
&& !isset($this->disabledChoices[$this->value])
7677
? $this->value
7778
: null;
7879
}
@@ -134,12 +135,11 @@ public function getSelectedItem(): mixed
134135
public function setDisabled(bool|array $value = true): static
135136
{
136137
if (!is_array($value)) {
138+
$this->disabledChoices = [];
137139
return parent::setDisabled($value);
138140
}
139-
140-
parent::setDisabled(false);
141-
$this->disabled = array_fill_keys($value, value: true);
142-
return $this;
141+
$this->disabledChoices = array_fill_keys($value, value: true);
142+
return parent::setDisabled(false);
143143
}
144144

145145

src/Forms/Controls/MultiChoiceControl.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
* Choice control that allows multiple items selection.
1616
*
1717
* @property mixed[] $items
18-
* @property bool|array<bool> $disabled
1918
* @property-deprecated mixed[] $selectedItems
2019
*/
2120
abstract class MultiChoiceControl extends BaseControl
2221
{
22+
/** @var bool[] */
23+
protected array $disabledChoices = [];
2324
private bool $checkDefaultValue = true;
2425

2526
/** @var mixed[] */
@@ -125,7 +126,7 @@ public function getSelectedItems(): array
125126
{
126127
$res = [];
127128
foreach ($this->value as $key) {
128-
if (isset($this->items[$key]) && !isset($this->disabled[$key])) {
129+
if (isset($this->items[$key]) && !isset($this->disabledChoices[$key])) {
129130
$res[$key] = $this->items[$key];
130131
}
131132
}
@@ -140,12 +141,11 @@ public function getSelectedItems(): array
140141
public function setDisabled(bool|array $value = true): static
141142
{
142143
if (!is_array($value)) {
144+
$this->disabledChoices = [];
143145
return parent::setDisabled($value);
144146
}
145-
146-
parent::setDisabled(false);
147-
$this->disabled = array_fill_keys($value, value: true);
148-
return $this;
147+
$this->disabledChoices = array_fill_keys($value, value: true);
148+
return parent::setDisabled(false);
149149
}
150150

151151

src/Forms/Controls/MultiSelectBox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function getControl(): Nette\Utils\Html
6868
return Nette\Forms\Helpers::createSelectBox(
6969
$items,
7070
[
71-
'disabled:' => is_array($this->disabled) ? $this->disabled : null,
71+
'disabled:' => $this->disabledChoices,
7272
] + $this->optionAttributes,
7373
$this->value,
7474
)->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
@@ -60,7 +60,7 @@ public function getControl(): Html
6060
array_merge($input->attrs, [
6161
'id:' => $ids,
6262
'checked?' => $this->value,
63-
'disabled:' => $this->disabled,
63+
'disabled:' => $this->disabled ?: $this->disabledChoices,
6464
'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']],
6565
]),
6666
['for:' => $ids] + $this->itemLabel->attrs,
@@ -86,7 +86,7 @@ public function getControlPart(int|string|null $key = null): Html
8686
return parent::getControl()->addAttributes([
8787
'id' => $this->getHtmlId() . '-' . $key,
8888
'checked' => in_array($key, (array) $this->value, strict: true),
89-
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
89+
'disabled' => $this->disabled || isset($this->disabledChoices[$key]),
9090
'value' => $key,
9191
]);
9292
}

src/Forms/Controls/SelectBox.php

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

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

103103
$selected = $this->value;
104104
if ($this->prompt !== false) {

0 commit comments

Comments
 (0)