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()
  • Loading branch information
dg committed May 19, 2024
1 parent 09a4bf8 commit 7ed195a
Show file tree
Hide file tree
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
Expand Up @@ -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));
}
}


Expand Down
20 changes: 8 additions & 12 deletions src/Forms/Controls/ChoiceControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

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


Expand Down Expand Up @@ -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());
}


Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 7ed195a

Please sign in to comment.