Skip to content

Commit

Permalink
BaseControl::$disabled is bool, added $disabledItems
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 29, 2024
1 parent ca57973 commit 5ca1950
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 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
8 changes: 3 additions & 5 deletions src/Forms/Controls/CheckboxList.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public function loadHttpData(): void
? $this->getHttpData(Nette\Forms\Form::DataText)
: explode(',', $data);
$this->value = array_keys(array_flip($data));
if (is_array($this->disabled)) {
$this->value = array_diff($this->value, array_keys($this->disabled));
}
$this->value = array_diff($this->value, array_keys($this->disabledItems));
}


Expand All @@ -62,7 +60,7 @@ public function getControl(): Html
array_merge($input->attrs, [
'id' => null,
'checked?' => $this->value,
'disabled:' => $this->disabled,
'disabled:' => $this->disabled ?: $this->disabledItems,
'required' => null,
'data-nette-rules:' => [array_key_first($items) => $input->attrs['data-nette-rules']],
]),
Expand All @@ -85,7 +83,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->disabledItems[$key]),
'required' => null,
'value' => $key,
]);
Expand Down
9 changes: 6 additions & 3 deletions src/Forms/Controls/ChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
abstract class ChoiceControl extends BaseControl
{
/** @var bool[] */
protected array $disabledItems = [];
private bool $checkDefaultValue = true;
private array $items = [];

Expand All @@ -37,7 +39,7 @@ public function loadHttpData(): void
{
$this->value = $this->getHttpData(Nette\Forms\Form::DataText);
if ($this->value !== null) {
$this->value = is_array($this->disabled) && isset($this->disabled[$this->value])
$this->value = $this->disabled || isset($this->disabledItems[$this->value])
? null
: key([$this->value => null]);
}
Expand Down Expand Up @@ -134,12 +136,13 @@ public function getSelectedItem(): mixed
public function setDisabled(bool|array $value = true): static
{
if (!is_array($value)) {
$this->disabledItems = [];
return parent::setDisabled($value);
}

parent::setDisabled(false);
$this->disabled = array_fill_keys($value, value: true);
if (isset($this->disabled[$this->value])) {
$this->disabledItems = array_fill_keys($value, value: true);
if (isset($this->disabledItems[$this->value])) {
$this->value = null;
}

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

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


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

parent::setDisabled(false);
$this->disabled = array_fill_keys($value, value: true);
$this->disabledItems = array_fill_keys($value, value: true);
$this->value = array_diff($this->value, $value);
return $this;
}
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->disabledItems,
] + $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 @@ -90,7 +90,7 @@ public function getControl(): Html
array_merge($input->attrs, [
'id:' => $ids,
'checked?' => $selected,
'disabled:' => $this->disabled,
'disabled:' => $this->disabled ?: $this->disabledItems,
'data-nette-rules:' => [key($items) => $input->attrs['data-nette-rules']],
]),
['for:' => $ids] + $this->itemLabel->attrs,
Expand All @@ -112,7 +112,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->disabledItems[$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->disabledItems;

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

0 comments on commit 5ca1950

Please sign in to comment.