livewire / volt

Volt is an elegantly crafted functional API for Livewire.
https://livewire.laravel.com/docs/volt
MIT License
339 stars 19 forks source link

Trigger hook on parent array property #108

Open undjike opened 3 months ago

undjike commented 3 months ago

This PR addresses issue https://github.com/livewire/volt/issues/107.

It allows triggering the update hook on the parent array property when a change is made to a key.

Before : When updating on a key, the hook is only triggered for that key.

updated([
    'profiles' => function () {
        // Not triggered
    },
    /*'profiles.0' => function () {
        // You need to explicitly listen for the key being updated.
        // There can be several keys.
        // You must listen to them all if you want to take action on any changes made to the parent array.
    }*/
]);

With this PR : When updating on a key, the hook is triggered on the parent array, if not explicitly listening for changes to the key.

updated([
    'profiles' => function ($key) {
        // Triggered, and the modified key is passed as a parameter.
    }
]);

That said... I'm wondering whether we should make it more dynamic to handle deep subkey as well.

updated([
    'profiles.admin' => function () {
        // Triggered on change made to key 'profiles.admin.0'.
    }
]);

This PR will forward the hook to the parent property profiles not to profiles.admin.

taylorotwell commented 3 months ago

If we are going to merge this it should probably handle your last case of deep subkeys.

taylorotwell commented 3 months ago

Mark as ready for review again when you want me to take another look please. Thanks!

undjike commented 3 months ago

Added support for deep subkeys. Hope the codebase is still in line with the package behaviour.

undjike commented 3 months ago

Hello @taylorotwell any comment ?

undjike commented 2 months ago

@taylorotwell let me know if there is something you want me to adjust.