Skip to content

Commit 10ee74e

Browse files
committed
BaseControl::$disabled is bool, added $disabledChoices
1 parent da6c777 commit 10ee74e

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
@@ -39,9 +39,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con
3939
protected mixed $value = null;
4040
protected Html $control;
4141
protected Html $label;
42-
43-
/** @var bool|bool[] */
44-
protected bool|array $disabled = false;
42+
protected bool $disabled = false;
4543

4644
/** @var array<string, array<class-string, callable(static): mixed>> */
4745
private static array $extMethods = [];
@@ -189,7 +187,7 @@ public function setDisabled(bool $state = true): static
189187
*/
190188
public function isDisabled(): bool
191189
{
192-
return $this->disabled === true;
190+
return $this->disabled;
193191
}
194192

195193

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,10 +15,11 @@
1515
* Choice control that allows single item selection.
1616
*
1717
* @property mixed[] $items
18-
* @property bool|array<bool> $disabled
1918
*/
2019
abstract class ChoiceControl extends BaseControl
2120
{
21+
/** @var bool[] */
22+
protected array $disabledChoices = [];
2223
private bool $checkDefaultValue = true;
2324

2425
/** @var mixed[] */
@@ -71,7 +72,7 @@ public function getValue(): mixed
7172
{
7273
return $this->value !== null
7374
&& array_key_exists($this->value, $this->items)
74-
&& !isset($this->disabled[$this->value])
75+
&& !isset($this->disabledChoices[$this->value])
7576
? $this->value
7677
: null;
7778
}
@@ -133,12 +134,11 @@ public function getSelectedItem(): mixed
133134
public function setDisabled(bool|array $value = true): static
134135
{
135136
if (!is_array($value)) {
137+
$this->disabledChoices = [];
136138
return parent::setDisabled($value);
137139
}
138-
139-
parent::setDisabled(false);
140-
$this->disabled = array_fill_keys($value, value: true);
141-
return $this;
140+
$this->disabledChoices = array_fill_keys($value, value: true);
141+
return parent::setDisabled(false);
142142
}
143143

144144

src/Forms/Controls/MultiChoiceControl.php

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

2425
/** @var mixed[] */
@@ -124,7 +125,7 @@ public function getSelectedItems(): array
124125
{
125126
$res = [];
126127
foreach ($this->value as $key) {
127-
if (isset($this->items[$key]) && !isset($this->disabled[$key])) {
128+
if (isset($this->items[$key]) && !isset($this->disabledChoices[$key])) {
128129
$res[$key] = $this->items[$key];
129130
}
130131
}
@@ -139,12 +140,11 @@ public function getSelectedItems(): array
139140
public function setDisabled(bool|array $value = true): static
140141
{
141142
if (!is_array($value)) {
143+
$this->disabledChoices = [];
142144
return parent::setDisabled($value);
143145
}
144-
145-
parent::setDisabled(false);
146-
$this->disabled = array_fill_keys($value, value: true);
147-
return $this;
146+
$this->disabledChoices = array_fill_keys($value, value: true);
147+
return parent::setDisabled(false);
148148
}
149149

150150

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)