Skip to content

Commit c6e3fe3

Browse files
committed
BaseControl::$disabled is bool, added $disabledItems
1 parent a08dbc9 commit c6e3fe3

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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ public function loadHttpData(): void
4646
? $this->getHttpData(Nette\Forms\Form::DataText)
4747
: explode(',', $data);
4848
$this->value = array_keys(array_flip($data));
49-
if (is_array($this->disabled)) {
50-
$this->value = array_diff($this->value, array_keys($this->disabled));
51-
}
49+
$this->value = array_diff($this->value, array_keys($this->disabledItems));
5250
}
5351

5452

@@ -62,7 +60,7 @@ public function getControl(): Html
6260
array_merge($input->attrs, [
6361
'id' => null,
6462
'checked?' => $this->value,
65-
'disabled:' => $this->disabled,
63+
'disabled:' => $this->disabled ?: $this->disabledItems,
6664
'required' => null,
6765
'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']],
6866
]),
@@ -85,7 +83,7 @@ public function getControlPart($key = null): Html
8583
return parent::getControl()->addAttributes([
8684
'id' => $this->getHtmlId() . '-' . $key,
8785
'checked' => in_array($key, (array) $this->value, strict: true),
88-
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
86+
'disabled' => $this->disabled || isset($this->disabledItems[$key]),
8987
'required' => null,
9088
'value' => $key,
9189
]);

src/Forms/Controls/ChoiceControl.php

Lines changed: 6 additions & 3 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 $disabledItems = [];
2325
private bool $checkDefaultValue = true;
2426
private array $items = [];
2527

@@ -37,7 +39,7 @@ public function loadHttpData(): void
3739
{
3840
$this->value = $this->getHttpData(Nette\Forms\Form::DataText);
3941
if ($this->value !== null) {
40-
$this->value = is_array($this->disabled) && isset($this->disabled[$this->value])
42+
$this->value = $this->disabled || isset($this->disabledItems[$this->value])
4143
? null
4244
: key([$this->value => null]);
4345
}
@@ -134,12 +136,13 @@ public function getSelectedItem(): mixed
134136
public function setDisabled(bool|array $value = true): static
135137
{
136138
if (!is_array($value)) {
139+
$this->disabledItems = [];
137140
return parent::setDisabled($value);
138141
}
139142

140143
parent::setDisabled(false);
141-
$this->disabled = array_fill_keys($value, value: true);
142-
if (isset($this->disabled[$this->value])) {
144+
$this->disabledItems = array_fill_keys($value, value: true);
145+
if (isset($this->disabledItems[$this->value])) {
143146
$this->value = null;
144147
}
145148

src/Forms/Controls/MultiChoiceControl.php

Lines changed: 5 additions & 4 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 $disabledItems = [];
2325
private bool $checkDefaultValue = true;
2426
private array $items = [];
2527

@@ -36,9 +38,7 @@ public function __construct($label = null, ?array $items = null)
3638
public function loadHttpData(): void
3739
{
3840
$this->value = array_keys(array_flip($this->getHttpData(Nette\Forms\Form::DataText)));
39-
if (is_array($this->disabled)) {
40-
$this->value = array_diff($this->value, array_keys($this->disabled));
41-
}
41+
$this->value = array_diff($this->value, array_keys($this->disabledItems));
4242
}
4343

4444

@@ -142,11 +142,12 @@ public function getSelectedItems(): array
142142
public function setDisabled(bool|array $value = true): static
143143
{
144144
if (!is_array($value)) {
145+
$this->disabledItems = [];
145146
return parent::setDisabled($value);
146147
}
147148

148149
parent::setDisabled(false);
149-
$this->disabled = array_fill_keys($value, value: true);
150+
$this->disabledItems = array_fill_keys($value, value: true);
150151
$this->value = array_diff($this->value, $value);
151152
return $this;
152153
}

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->disabledItems,
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->disabledItems,
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->disabledItems[$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->disabledItems;
9999

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

0 commit comments

Comments
 (0)