Skip to content

Commit

Permalink
BaseControl::$disabled is bool, added $disabledChoices
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 19, 2024
1 parent 9f2c3bd commit 9140b26
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
6 changes: 2 additions & 4 deletions src/Forms/Controls/BaseControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements Con
protected mixed $value = null;
protected Html $control;
protected Html $label;

/** @var bool|bool[] */
protected bool|array $disabled = false;
protected bool $disabled = false;

/** @var callable[][] extension methods */
private static array $extMethods = [];
Expand Down Expand Up @@ -197,7 +195,7 @@ public function setDisabled(bool $state = true): static
*/
public function isDisabled(): bool
{
return $this->disabled === true;
return $this->disabled;
}


Expand Down
4 changes: 2 additions & 2 deletions src/Forms/Controls/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getControl(): Html
array_merge($input->attrs, [
'id' => null,
'checked?' => $this->value,
'disabled:' => $this->disabled,
'disabled:' => $this->disabled ?: $this->disabledChoices,
'required' => null,
'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']],
]),
Expand All @@ -82,7 +82,7 @@ public function getControlPart($key = null): Html
return parent::getControl()->addAttributes([
'id' => $this->getHtmlId() . '-' . $key,
'checked' => in_array($key, (array) $this->value, strict: true),
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
'disabled' => $this->disabled || isset($this->disabledChoices[$key]),
'required' => null,
'value' => $key,
]);
Expand Down
13 changes: 7 additions & 6 deletions src/Forms/Controls/ChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
abstract class ChoiceControl extends BaseControl
{
/** @var bool[] */
protected array $disabledChoices = [];
private bool $checkDefaultValue = true;

/** @var list<array{int|string, string|\Stringable}> */
Expand Down Expand Up @@ -77,7 +79,7 @@ public function setValue($value): static
public function getValue(): mixed
{
return $this->value !== null
&& !isset($this->disabled[$this->value])
&& !isset($this->disabledChoices[$this->value])
&& ([$res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
? $res
: null;
Expand Down Expand Up @@ -130,7 +132,7 @@ public function getItems(): array
public function getSelectedItem(): mixed
{
return $this->value !== null
&& !isset($this->disabled[$this->value])
&& !isset($this->disabledChoices[$this->value])
&& ([, $res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
? $res
: null;
Expand All @@ -143,12 +145,11 @@ public function getSelectedItem(): mixed
public function setDisabled(bool|array $value = true): static
{
if (!is_array($value)) {
$this->disabledChoices = [];
return parent::setDisabled($value);
}

parent::setDisabled(false);
$this->disabled = array_fill_keys($value, value: true);
return $this;
$this->disabledChoices = array_fill_keys($value, value: true);
return parent::setDisabled(false);
}


Expand Down
11 changes: 6 additions & 5 deletions src/Forms/Controls/MultiChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
abstract class MultiChoiceControl extends BaseControl
{
/** @var bool[] */
protected array $disabledChoices = [];
private bool $checkDefaultValue = true;

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

Expand All @@ -134,12 +136,11 @@ public function getSelectedItems(): array
public function setDisabled(bool|array $value = true): static
{
if (!is_array($value)) {
$this->disabledChoices = [];
return parent::setDisabled($value);
}

parent::setDisabled(false);
$this->disabled = array_fill_keys($value, value: true);
return $this;
$this->disabledChoices = array_fill_keys($value, value: true);
return parent::setDisabled(false);
}


Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/MultiSelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getControl(): Nette\Utils\Html
return Nette\Forms\Helpers::createSelectBox(
$items,
[
'disabled:' => is_array($this->disabled) ? $this->disabled : null,
'disabled:' => $this->disabledChoices,
] + $this->optionAttributes,
$this->value,
)->addAttributes(parent::getControl()->attrs)->multiple(true);
Expand Down
4 changes: 2 additions & 2 deletions src/Forms/Controls/RadioList.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getControl(): Html
array_merge($input->attrs, [
'id:' => $ids,
'checked?' => $this->value,
'disabled:' => $this->disabled,
'disabled:' => $this->disabled ?: $this->disabledChoices,
'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']],
]),
['for:' => $ids] + $this->itemLabel->attrs,
Expand All @@ -79,7 +79,7 @@ public function getControlPart($key = null): Html
return parent::getControl()->addAttributes([
'id' => $this->getHtmlId() . '-' . $key,
'checked' => in_array($key, (array) $this->value, strict: true),
'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
'disabled' => $this->disabled || isset($this->disabledChoices[$key]),
'value' => $key,
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/Controls/SelectBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getControl(): Nette\Utils\Html
}

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

$selected = $this->value;
if ($this->prompt !== false) {
Expand Down

0 comments on commit 9140b26

Please sign in to comment.