Skip to content

Commit

Permalink
Merge branch 'main' into pr/466
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Mar 20, 2024
2 parents f59a1dc + 56b5375 commit eeb5325
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/Components/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Form extends Component

private $json;

/** @var Model|null */
private $model;

private static $defaultGuardAttributes = true;
Expand All @@ -42,6 +43,8 @@ class Form extends Component

private static $instances = [];

private $resolvedData = false;

/**
* Create a new component instance.
*
Expand Down Expand Up @@ -169,6 +172,20 @@ private function parseUnguardedValue($unguarded = null, $value = null)
}
}

/**
* Returns the resolved data but evualates it only once.
*/
public function resolveData(): ?object
{
if ($this->resolvedData !== false) {
return $this->resolvedData;
}

$this->resolvedData = $this->guardedData() ?: $this->defaultData();

return $this->resolvedData;
}

/**
* This is a workaround for https://github.com/vuejs/core/issues/5339
*
Expand All @@ -183,12 +200,10 @@ public static function selected(string $name, $value): bool
return false;
}

/** @var Form */
$instance = Arr::last(static::$instances);

$data = data_get(
$instance->guardedData() ?: $instance->defaultData(),
static::dottedName($name)
);
$data = data_get($instance->resolveData(), static::dottedName($name));

return is_array($data)
? in_array($value, $data, true)
Expand Down Expand Up @@ -403,7 +418,7 @@ private function defaultData(): ?object
public function formData(): array
{
$data = [
'data' => $this->guardedData() ?: $this->defaultData(),
'data' => $this->resolveData(),
'json' => $this->json,
];

Expand Down

0 comments on commit eeb5325

Please sign in to comment.