Skip to content

Commit

Permalink
ChoiceControl, MultiChoiceControl: disabled items are processed in ge…
Browse files Browse the repository at this point in the history
…tValue() instead of loadHttpData()
dg committed Jun 4, 2024
1 parent 14c4917 commit 3b6651f
Showing 3 changed files with 10 additions and 21 deletions.
3 changes: 0 additions & 3 deletions src/Forms/Controls/CheckboxList.php
Original file line number Diff line number Diff line change
@@ -46,9 +46,6 @@ 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));
}
}


20 changes: 8 additions & 12 deletions src/Forms/Controls/ChoiceControl.php
Original file line number Diff line number Diff line change
@@ -38,12 +38,8 @@ public function __construct($label = null, ?array $items = null)

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])
? null
: key([$this->value => null]);
}
$value = $this->getHttpData(Nette\Forms\Form::DataText);
$this->value = $value === null ? null : Arrays::toKey($value);
}


@@ -81,7 +77,9 @@ public function setValue($value)
*/
public function getValue(): mixed
{
return $this->value !== null && ([$res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
return $this->value !== null
&& !isset($this->disabled[$this->value])
&& ([$res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
? $res
: null;
}
@@ -133,7 +131,9 @@ public function getItems(): array
*/
public function getSelectedItem(): mixed
{
return $this->value !== null && ([, $res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
return $this->value !== null
&& !isset($this->disabled[$this->value])
&& ([, $res] = Arrays::first($this->choices, fn($choice) => $choice[0] === $this->value))
? $res
: null;
}
@@ -150,10 +150,6 @@ public function setDisabled(bool|array $value = true): static

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

return $this;
}

8 changes: 2 additions & 6 deletions src/Forms/Controls/MultiChoiceControl.php
Original file line number Diff line number Diff line change
@@ -39,9 +39,6 @@ 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));
}
}


@@ -86,7 +83,7 @@ public function setValue($values)
*/
public function getValue(): array
{
return array_values(array_intersect($this->value, array_column($this->choices, 0)));
return array_keys($this->getSelectedItems());
}


@@ -128,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]]));
$res = array_filter($this->choices, fn($choice) => isset($flip[$choice[0]]) && !isset($this->disabled[$choice[0]]));
return array_column($res, 1, 0);
}

@@ -144,7 +141,6 @@ public function setDisabled(bool|array $value = true): static

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

0 comments on commit 3b6651f

Please sign in to comment.