Skip to content

Commit d3c00f6

Browse files
committed
Sync React/Vue navigate hook
1 parent 052dab9 commit d3c00f6

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

demo-app/tests/Browser/FormTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public function it_can_submit_a_form_from_within_the_modal_and_show_the_validati
2323
->waitForTextIn('.im-modal-content', 'Edit User')
2424
->type('name', 'a')
2525
->press('Save')
26-
->waitForTextIn('.im-modal-content', 'The name field must be at least 3 characters.');
26+
->waitForTextIn('.im-modal-content', 'The name field must be at least 3 characters.')
27+
->within('.im-dialog[data-inertiaui-modal-index="0"]', function (Browser $browser) {
28+
$browser->assertSee('The name field must be at least 3 characters.');
29+
})
30+
->assertMissing('.im-dialog[data-inertiaui-modal-index="1"]');
2731
});
2832
}
2933

demo-app/tests/ModalTestCase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ trait ModalTestCase
77
public static function booleanProvider(): array
88
{
99
return [
10-
[true],
11-
[false],
10+
'true' => [true],
11+
'false' => [false],
1212
];
1313
}
1414
}

vue/src/ModalRoot.vue

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,27 @@
11
<script setup>
2-
import { onBeforeMount, onUnmounted, ref, watch } from 'vue'
2+
import { onBeforeMount, onUnmounted, ref } from 'vue'
33
import { router } from '@inertiajs/vue3'
4-
import { usePage } from '@inertiajs/vue3'
54
import { useModalStack } from './modalStack'
65
import ModalRenderer from './ModalRenderer.vue'
76
import { default as Axios } from 'axios'
87
98
const modalStack = useModalStack()
109
1110
const isNavigating = ref(false)
11+
const previousModalOnBase = ref(null)
12+
1213
onUnmounted(router.on('start', () => (isNavigating.value = true)))
1314
onUnmounted(router.on('finish', () => (isNavigating.value = false)))
15+
onUnmounted(
16+
router.on('navigate', ($event) => {
17+
const modalOnBase = $event.detail.page.props._inertiaui_modal
1418
15-
const axiosRequestInterceptor = (config) => {
16-
// A Modal is opened on top of a base route, so we need to pass this base route
17-
// so it can redirect back with the back() helper method...
18-
config.headers['X-InertiaUI-Modal-Base-Url'] = modalStack.getBaseUrl()
19-
20-
return config
21-
}
22-
23-
onBeforeMount(() => {
24-
Axios.interceptors.request.use(axiosRequestInterceptor)
25-
})
26-
27-
onUnmounted(() => {
28-
Axios.interceptors.request.eject(axiosRequestInterceptor)
29-
})
30-
31-
const $page = usePage()
32-
33-
watch(
34-
() => $page.props?._inertiaui_modal,
35-
(modalOnBase) => {
3619
if (!modalOnBase) {
37-
modalStack.closeAll()
20+
previousModalOnBase.value && modalStack.closeAll()
3821
return
3922
}
4023
41-
// TODO: Is this really necessary?
24+
previousModalOnBase.value = modalOnBase
4225
modalStack.setBaseUrl(modalOnBase.baseUrl)
4326
4427
modalStack.pushFromResponseData(modalOnBase, {}, () => {
@@ -54,9 +37,24 @@ watch(
5437
})
5538
}
5639
})
57-
},
58-
{ immediate: true },
40+
}),
5941
)
42+
43+
const axiosRequestInterceptor = (config) => {
44+
// A Modal is opened on top of a base route, so we need to pass this base route
45+
// so it can redirect back with the back() helper method...
46+
config.headers['X-InertiaUI-Modal-Base-Url'] = modalStack.getBaseUrl()
47+
48+
return config
49+
}
50+
51+
onBeforeMount(() => {
52+
Axios.interceptors.request.use(axiosRequestInterceptor)
53+
})
54+
55+
onUnmounted(() => {
56+
Axios.interceptors.request.eject(axiosRequestInterceptor)
57+
})
6058
</script>
6159

6260
<template>

0 commit comments

Comments
 (0)