Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for lazy props in modals #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Tofandel
Copy link

@Tofandel Tofandel commented Aug 5, 2022

Inertia doesn't support lazyness for nested props because it only does a simple array_filter, but it has a magic trick up it's sleeve that unpacks string props with dots in them after the array_filter has been done https://github.com/inertiajs/inertia-laravel/blob/master/src/Response.php#L148

We can use that and share a flat array of modal props to inertia which will enable the lazy prop feature by using only: ['modal.props.someProp']

This PR needs lepikhinb/momentum-modal-plugin#4 to work properly

Fixes: #13

Props can go back to their unflattened form when inertiajs/inertia-laravel#435 is released

@lepikhinb
Copy link
Owner

Thank you for the PR. I'm going to review and either merge or re-implement this soon.

@jeffreyvanhees
Copy link

jeffreyvanhees commented Sep 26, 2022

@lepikhinb Do you have any updates on this? It would be great when this feature is implemented. Essential for the 'modal principle' in my opinion.

(No pressure, I know there are too few hours in a day)

@what-the-diff
Copy link

what-the-diff bot commented Oct 24, 2022

  • The props are now passed as a parameter to the constructor.
  • The with method is used to set the props instead of passing them directly in the constructor.
  • A new array $flatProps is created and it's populated by looping through all keys/values from $this->props, adding 'modal.' before each key name (e.g: modal.$key). This array will be merged into inertia()->share().
  • Inertia::share() no longer receives an associative array for "modal" but only its component name ($this->component) and an indexed array containing all flatProps values (...$flatProps).

@jeffreyvanhees
Copy link

@lepikhinb Do you have an idea yet whether you will merge this PR? :)

@lepikhinb
Copy link
Owner

@jeffreyvanhees still not sure about the implementation

Comment on lines +47 to +52
$flatProps = [];
foreach ($this->props as $key => $prop) {
$flatProps['modal.props.'.$key] = $prop;
}
/** @phpstan-ignore-next-line */
inertia()->share(['modal' => $this->component()]);
inertia()->share(['modal' => $this->component(), ...$flatProps]);
Copy link

@pascalbaljet pascalbaljet Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manually looping over the props can be avoided by using the Arr::dot helper method:

inertia()->share(['modal' => $this->component(), ...Arr::dot($this->props, 'modal.props.')]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support lazy props - same behaviour as Inertia
4 participants