Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Dec 26, 2021
1 parent 4abca0c commit dc76dd4
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 46 deletions.
36 changes: 19 additions & 17 deletions packages/forms/src/Components/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ protected function setUp(): void
{
parent::setUp();

$this->default([]);

$this->afterStateHydrated(function (Builder $component, array $state): void {
$items = collect($state)
->mapWithKeys(fn ($itemData) => [(string) Str::uuid() => $itemData])
->toArray();

$component->state($items);
});

$this->registerListeners([
'builder::createItem' => [
function (Builder $component, string $statePath, string $block, ?string $afterUuid = null): void {
Expand All @@ -48,7 +58,7 @@ function (Builder $component, string $statePath, string $block, ?string $afterUu
if ($afterUuid) {
$newItems = [];

foreach ($component->getNormalisedState() as $uuid => $item) {
foreach ($component->getState() as $uuid => $item) {
$newItems[$uuid] = $item;

if ($uuid === $afterUuid) {
Expand All @@ -74,7 +84,7 @@ function (Builder $component, string $statePath, string $uuidToDelete): void {
return;
}

$items = $component->getNormalisedState();
$items = $component->getState();

unset($items[$uuidToDelete]);

Expand All @@ -96,7 +106,7 @@ function (Builder $component, string $statePath, string $uuidToMoveDown): void {
return;
}

$items = array_move_after($component->getNormalisedState(), $uuidToMoveDown);
$items = array_move_after($component->getState(), $uuidToMoveDown);

$livewire = $component->getLivewire();
data_set($livewire, $statePath, $items);
Expand All @@ -116,7 +126,7 @@ function (Builder $component, string $statePath, string $uuidToMoveUp): void {
return;
}

$items = array_move_before($component->getNormalisedState(), $uuidToMoveUp);
$items = array_move_before($component->getState(), $uuidToMoveUp);

$livewire = $component->getLivewire();
data_set($livewire, $statePath, $items);
Expand All @@ -131,6 +141,10 @@ function (Builder $component, string $statePath, string $uuidToMoveUp): void {
'label' => lcfirst($component->getLabel()),
]);
});

$this->mutateDehydratedStateUsing(function (array $state): array {
return array_values($state);
});
}

public function blocks(array $blocks): static
Expand Down Expand Up @@ -181,7 +195,7 @@ public function getBlocks(): array

public function getChildComponentContainers(): array
{
return collect($this->getNormalisedState())
return collect($this->getState())
->map(function ($itemData, $itemIndex): ComponentContainer {
return $this->getBlock($itemData['type'])
->getChildComponentContainer()
Expand All @@ -200,18 +214,6 @@ public function getCreateItemButtonLabel(): string
return $this->evaluate($this->createItemButtonLabel);
}

public function getNormalisedState(): array
{
if (! is_array($state = $this->getState())) {
return [];
}

return array_filter(
$state,
fn ($item) => is_array($item) && $this->hasBlock($item['type'] ?? null),
);
}

public function hasBlock($name): bool
{
return (bool) $this->getBlock($name);
Expand Down
22 changes: 22 additions & 0 deletions packages/forms/src/Components/Concerns/HasState.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ trait HasState

protected ?Closure $dehydrateStateUsing = null;

protected ?Closure $mutateDehydratedStateUsing = null;

protected bool $hasDefaultState = false;

protected bool | Closure $isDehydrated = true;
Expand Down Expand Up @@ -120,6 +122,26 @@ public function hydrateNullState(): static
return $this;
}

public function mutateDehydratedState($state)
{
return $this->evaluate(
$this->mutateDehydratedStateUsing,
['state' => $state],
);
}

public function mutatesDehydratedState(): bool
{
return $this->mutateDehydratedStateUsing instanceof Closure;
}

public function mutateDehydratedStateUsing(?Closure $callback): static
{
$this->mutateDehydratedStateUsing = $callback;

return $this;
}

public function state($state): static
{
$livewire = $this->getLivewire();
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/Components/HasManyRepeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function saveRelationships(): void

public function getChildComponentContainers(): array
{
return collect($this->getNormalisedState())
return collect($this->getState())
->map(function ($itemData, $itemKey): ComponentContainer {
return $this
->getChildComponentContainer()
Expand Down
2 changes: 2 additions & 0 deletions packages/forms/src/Components/MultiSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ protected function setUp(): void
{
parent::setUp();

$this->default([]);

$this->getOptionLabelsUsing(function (MultiSelect $component, array $values): array {
$options = $component->getOptions();

Expand Down
31 changes: 18 additions & 13 deletions packages/forms/src/Components/Repeater.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ protected function setUp(): void
{
parent::setUp();

$this->defaultItems(1);

$this->afterStateHydrated(function (Repeater $component, array $state): void {
$items = collect($state)
->mapWithKeys(fn ($itemData) => [(string) Str::uuid() => $itemData])
->toArray();

$component->state($items);
});

$this->registerListeners([
'repeater::createItem' => [
function (Repeater $component, string $statePath): void {
Expand Down Expand Up @@ -51,7 +61,7 @@ function (Repeater $component, string $statePath, string $uuidToDelete): void {
return;
}

$items = $component->getNormalisedState();
$items = $component->getState();

unset($items[$uuidToDelete]);

Expand All @@ -73,7 +83,7 @@ function (Repeater $component, string $statePath, string $uuidToMoveDown): void
return;
}

$items = array_move_after($component->getNormalisedState(), $uuidToMoveDown);
$items = array_move_after($component->getState(), $uuidToMoveDown);

$livewire = $component->getLivewire();
data_set($livewire, $statePath, $items);
Expand All @@ -93,7 +103,7 @@ function (Repeater $component, string $statePath, string $uuidToMoveUp): void {
return;
}

$items = array_move_before($component->getNormalisedState(), $uuidToMoveUp);
$items = array_move_before($component->getState(), $uuidToMoveUp);

$livewire = $component->getLivewire();
data_set($livewire, $statePath, $items);
Expand All @@ -106,6 +116,10 @@ function (Repeater $component, string $statePath, string $uuidToMoveUp): void {
'label' => lcfirst($component->getLabel()),
]);
});

$this->mutateDehydratedStateUsing(function (array $state): array {
return array_values($state);
});
}

public function createItemButtonLabel(string | Closure | null $label): static
Expand Down Expand Up @@ -144,7 +158,7 @@ public function hydrateDefaultItemState(string $uuid): void

public function getChildComponentContainers(): array
{
return collect($this->getNormalisedState())
return collect($this->getState())
->map(function ($itemData, $itemIndex): ComponentContainer {
return $this
->getChildComponentContainer()
Expand All @@ -158,15 +172,6 @@ public function getCreateItemButtonLabel(): string
return $this->evaluate($this->createItemButtonLabel);
}

public function getNormalisedState(): array
{
if (! is_array($state = $this->getState())) {
return [];
}

return array_filter($state, fn ($item) => is_array($item));
}

public function isItemMovementDisabled(): bool
{
return $this->evaluate($this->isItemMovementDisabled);
Expand Down
2 changes: 2 additions & 0 deletions packages/forms/src/Components/TagsInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ protected function setUp(): void
{
parent::setUp();

$this->default([]);

$this->afterStateHydrated(function (TagsInput $component, $state): void {
if (is_array($state)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/Concerns/Cloneable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function cloneComponents(): static
{
$components = [];

foreach ($this->getComponents(true) as $component) {
foreach ($this->getComponents(withHidden: true) as $component) {
$components[] = $component->getClone();
}

Expand Down
6 changes: 5 additions & 1 deletion packages/forms/src/Concerns/HasComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ public function getComponents(bool $withHidden = false): array
return $component;
}, $this->evaluate($this->components));

if ($withHidden) {
return $components;
}

return array_filter(
$components,
fn (Component $component) => $withHidden ?: ! $component->isHidden(),
fn (Component $component) => ! $component->isHidden(),
);
}
}
67 changes: 54 additions & 13 deletions packages/forms/src/Concerns/HasState.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,25 @@ trait HasState

public function callAfterStateHydrated(): void
{
foreach ($this->getComponents() as $component) {
foreach ($this->getComponents(withHidden: true) as $component) {
$component->callAfterStateHydrated();

foreach ($component->getChildComponentContainers() as $container) {
if ($container->isHidden()) {
continue;
}

$container->callAfterStateHydrated();
}
}
}

public function callAfterStateUpdated(string $path): bool
{
foreach ($this->getComponents() as $component) {
foreach ($this->getComponents(withHidden: true) as $component) {
if ($component->getStatePath() === $path) {
$component->callAfterStateUpdated();

return true;
}

foreach ($component->getChildComponentContainers() as $container) {
if ($container->isHidden()) {
continue;
}

if ($container->callAfterStateUpdated($path)) {
return true;
}
Expand All @@ -49,7 +41,11 @@ public function callAfterStateUpdated(string $path): bool

public function callBeforeStateDehydrated(): void
{
foreach ($this->getComponents() as $component) {
foreach ($this->getComponents(withHidden: true) as $component) {
if ($component->isHidden()) {
continue;
}

$component->callBeforeStateDehydrated();

$componentModel = $component->getModel();
Expand All @@ -73,6 +69,10 @@ public function dehydrateState(array &$state = []): array
$this->callBeforeStateDehydrated();

foreach ($this->getComponents() as $component) {
if ($component->isHidden()) {
continue;
}

$componentStatePath = $component->getStatePath();

if ($component->isDehydrated()) {
Expand All @@ -92,6 +92,47 @@ public function dehydrateState(array &$state = []): array
}
}

$this->mutateDehydratedState($state);

return $state;
}

public function mutateDehydratedState(array &$state = []): array
{
foreach ($this->getComponents() as $component) {
if ($component->isHidden()) {
continue;
}

if (! $component->isDehydrated()) {
continue;
}

if ($component->getStatePath(isAbsolute: false)) {
if (! $component->mutatesDehydratedState()) {
continue;
}

$componentStatePath = $component->getStatePath();

data_set(
$state,
$componentStatePath,
$component->mutateDehydratedState(
data_get($state, $componentStatePath),
),
);
}

foreach ($component->getChildComponentContainers() as $container) {
if ($container->isHidden()) {
continue;
}

$container->mutateDehydratedState($state);
}
}

return $state;
}

Expand Down Expand Up @@ -120,7 +161,7 @@ public function fill(?array $state = null): static

public function hydrateDefaultState(): static
{
foreach ($this->getComponents() as $component) {
foreach ($this->getComponents(withHidden: true) as $component) {
$component->hydrateDefaultState();
$component->callAfterStateHydrated();

Expand All @@ -134,7 +175,7 @@ public function hydrateDefaultState(): static

public function hydrateNullState(): static
{
foreach ($this->getComponents() as $component) {
foreach ($this->getComponents(withHidden: true) as $component) {
$component->hydrateNullState();

foreach ($component->getChildComponentContainers() as $container) {
Expand Down
Loading

0 comments on commit dc76dd4

Please sign in to comment.