Skip to content

Commit

Permalink
fix: patch up component split states
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Feb 18, 2023
1 parent deb7738 commit 473b042
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/UI/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ protected static function buildComponent(Component $component, array $config, bo
foreach ($config['payload']['data'] as $key => $value) {
if (property_exists($componentCalled, $key)) {
$componentCalled->{$key} = $value;
static::$state[$componentCalled->key][$key] = $value;
}
}

if ($config['type'] === 'callMethod') {
// compile and render to get the latest state (we don't really output anything)
static::compileTemplate($componentCalled->render(), static::$state[$componentCalled->key]);
static::compileTemplate($component->render());

$config['payload']['methodArgs'] = explode(',', $config['payload']['methodArgs']);
$methodToCall = [$componentCalled, $config['payload']['method']];
Expand All @@ -94,6 +95,14 @@ protected static function buildComponent(Component $component, array $config, bo
if (in_array($config['payload']['method'], $wValue)) {
$componentCalled = new (static::$components[$wKey]);
$methodToCall = [$componentCalled, $config['payload']['method']];

// set the state for the component
foreach ($config['payload']['data'] as $key => $value) {
if (property_exists($componentCalled, $key)) {
$componentCalled->{$key} = $value;
}
}

break;
}
}
Expand All @@ -119,14 +128,14 @@ protected static function buildComponent(Component $component, array $config, bo
return [
'responseType' => 'json',
'html' => $withoutScripts ?
static::compileTemplate($componentCalled->render(), static::$state[$componentCalled->key]) :
static::compileTemplate($component->render()) :
str_replace(
'</body>',
Core::createElement('script', [], ['
window._leafUIConfig.methods = ' . json_encode(array_unique(static::$componentMethods)) . ';
window._leafUIConfig.components = ' . json_encode(static::$components) . ';
']) . Core::init() . '</body>',
static::compileTemplate($componentCalled->render(), static::$state[$componentCalled->key])
static::compileTemplate($component->render())
),
'state' => $pageState,
];
Expand Down Expand Up @@ -179,7 +188,7 @@ public static function view(string $filename): string
*/
public static function compileTemplate(string $rawText, array $state = []): string
{
if (!$state) {
if (empty($state)) {
foreach (array_values(static::$state) as $key => $value) {
$state = array_merge($state, $value);
}
Expand Down Expand Up @@ -302,12 +311,12 @@ public static function component(string $component, array $props = []): string
if (!$component->key) {
$component->key = static::randomId($component::class);
}
static::$state[$component->key] = array_unique(array_merge(get_class_vars($component::class), $props));

static::$state[$component->key] = array_unique(array_merge(get_class_vars($component::class), $props, static::$state[$component->key] ?? []));
static::$componentMethods = array_merge(static::$componentMethods, get_class_methods($component));
static::$mappedComponentMethods = array_merge(static::$mappedComponentMethods, [$component->key => get_class_methods($component)]);
static::$components = array_merge(static::$components, [$component->key => $component::class]);

$config = [
'type' => 'component',
'payload' => [
Expand Down

0 comments on commit 473b042

Please sign in to comment.