Skip to content

Commit 97d369d

Browse files
committed
BaseControl::$disabled is bool, added $disabledChoices
1 parent 4c50304 commit 97d369d

7 files changed

+21
-21
lines changed

src/Forms/Controls/BaseControl.php

+2-4
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

+2-2
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

+7-6
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

2628
/** @var list<array{int|string, string|\Stringable}> */
@@ -77,7 +79,7 @@ public function setValue($value): static
7779
public function getValue(): mixed
7880
{
7981
return $this->value !== null
80-
&& !isset($this->disabled[$this->value])
82+
&& !isset($this->disabledChoices[$this->value])
8183
&& ([$res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
8284
? $res
8385
: null;
@@ -130,7 +132,7 @@ public function getItems(): array
130132
public function getSelectedItem(): mixed
131133
{
132134
return $this->value !== null
133-
&& !isset($this->disabled[$this->value])
135+
&& !isset($this->disabledChoices[$this->value])
134136
&& ([, $res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
135137
? $res
136138
: null;
@@ -143,12 +145,11 @@ public function getSelectedItem(): mixed
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/MultiChoiceControl.php

+6-5
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

2628
/** @var list<array{int|string, string|\Stringable}> */
@@ -123,7 +125,7 @@ public function getItems(): array
123125
public function getSelectedItems(): array
124126
{
125127
$flip = array_flip($this->value);
126-
$res = array_filter($this->choices, fn($choice) => isset($flip[$choice[0]]) && !isset($this->disabled[$choice[0]]));
128+
$res = array_filter($this->choices, fn($choice) => isset($flip[$choice[0]]) && !isset($this->disabledChoices[$choice[0]]));
127129
return array_column($res, 1, 0);
128130
}
129131

@@ -134,12 +136,11 @@ public function getSelectedItems(): array
134136
public function setDisabled(bool|array $value = true): static
135137
{
136138
if (!is_array($value)) {
139+
$this->disabledChoices = [];
137140
return parent::setDisabled($value);
138141
}
139-
140-
parent::setDisabled(false);
141-
$this->disabled = array_fill_keys($value, value: true);
142-
return $this;
142+
$this->disabledChoices = array_fill_keys($value, value: true);
143+
return parent::setDisabled(false);
143144
}
144145

145146

src/Forms/Controls/MultiSelectBox.php

+1-1
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

+2-2
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

+1-1
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)