Skip to content

Commit a454744

Browse files
committed
Move scroll logic to formError component
1 parent c1d9fcf commit a454744

35 files changed

Lines changed: 35 additions & 47 deletions

resources/js/components/AdminStreamingRoomTypeEditButton.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ function save() {
213213
})
214214
.finally(() => {
215215
isLoadingAction.value = false;
216-
formErrors.scrollToFirstError();
217216
});
218217
}
219218
</script>

resources/js/components/FormError.vue

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
11
<script setup>
2+
import { computed, nextTick, onMounted, ref, watch } from "vue";
3+
24
const props = defineProps({
35
errors: {
46
type: [Object, null],
57
required: true,
68
default: null,
79
},
810
});
11+
12+
const formError = ref(null);
13+
14+
const hasError = computed(() => {
15+
return props.errors != null && Object.keys(props.errors).length > 0;
16+
});
17+
18+
async function scrollToFirstError() {
19+
await nextTick();
20+
21+
const selfDomElement = formError.value;
22+
const firstDomElement = document.getElementsByClassName("form-error")[0];
23+
24+
// Only scroll into view, if this form error is the first form error rendered on the page
25+
if (selfDomElement != null && selfDomElement === firstDomElement) {
26+
selfDomElement.scrollIntoView({ behavior: "smooth", block: "center" });
27+
}
28+
}
29+
30+
// Scroll to error if errors visible state changes
31+
watch(hasError, async () => {
32+
await scrollToFirstError();
33+
});
34+
35+
// Scroll to error if the component is mounted with errors present
36+
onMounted(async () => {
37+
if (hasError.value) {
38+
await scrollToFirstError();
39+
}
40+
});
941
</script>
1042

1143
<template>
1244
<p
13-
v-if="props.errors != null && Object.keys(props.errors).length > 0"
45+
v-if="hasError"
1446
class="form-error text-red-500"
1547
role="alert"
48+
ref="formError"
1649
>
1750
<template v-for="(error, index) in props.errors" :key="index">
1851
{{ error }}

resources/js/components/RoomCreateComponent.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ function handleOk() {
169169
}
170170
171171
formErrors.set(error.response.data.errors);
172-
formErrors.scrollToFirstError();
173172
return;
174173
}
175174
// permission denied

resources/js/components/RoomJoinButton.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,7 @@ function getJoinUrl() {
411411
if (error.response.status === env.HTTP_UNPROCESSABLE_ENTITY) {
412412
formErrors.set(error.response.data.errors);
413413
414-
loadStartJoinRequirements().then(() => {
415-
formErrors.scrollToFirstError();
416-
});
414+
loadStartJoinRequirements();
417415
return;
418416
}
419417

resources/js/components/RoomTabDescription.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ function save() {
234234
.finally(() => {
235235
// Disable saving indicator
236236
isBusy.value = false;
237-
formErrors.scrollToFirstError();
238237
});
239238
}
240239

resources/js/components/RoomTabFilesEditButton.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ function save() {
211211
})
212212
.finally(() => {
213213
isLoadingAction.value = false;
214-
formErrors.scrollToFirstError();
215214
});
216215
}
217216
</script>

resources/js/components/RoomTabFilesUploadButton.vue

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,6 @@ function uploadFile(file) {
266266
}
267267
}
268268
api.error(error, { redirectOnUnauthenticated: false });
269-
})
270-
.finally(() => {
271-
formErrors.scrollToFirstError();
272269
});
273270
}
274271
</script>

resources/js/components/RoomTabMembersAddSingleModal.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ function save() {
243243
})
244244
.finally(() => {
245245
isLoadingAction.value = false;
246-
formErrors.scrollToFirstError();
247246
});
248247
}
249248
</script>

resources/js/components/RoomTabMembersBulkDeleteButton.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ function deleteMembers() {
134134
})
135135
.finally(() => {
136136
isLoadingAction.value = false;
137-
formErrors.scrollToFirstError();
138137
});
139138
}
140139
</script>

resources/js/components/RoomTabMembersBulkEditButton.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ function save() {
172172
})
173173
.finally(() => {
174174
isLoadingAction.value = false;
175-
formErrors.scrollToFirstError();
176175
});
177176
}
178177
</script>

0 commit comments

Comments
 (0)