Skip to content

Commit

Permalink
Declare template refs with useTemplateRef()
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Sep 25, 2024
1 parent 1fa0f88 commit 037a1b5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import arches from "arches";
import { computed, ref, inject } from "vue";
import { computed, inject, ref, useTemplateRef } from "vue";
import { useGettext } from "vue3-gettext";
import Button from "primevue/button";
Expand Down Expand Up @@ -52,7 +52,7 @@ const item = inject(itemKey) as Ref<ControlledListItem>;
const { image } = defineProps<{ image: ControlledListItemImage }>();
const editingRows = ref<NewOrExistingControlledListItemImageMetadata[]>([]);
const rowIndexToFocus = ref(-1);
const editorRef: Ref<HTMLDivElement | null> = ref(null);
const editorDiv = useTemplateRef("editorDiv");
const labeledChoices: LabeledChoice[] = [
{
Expand Down Expand Up @@ -227,8 +227,7 @@ const focusInput = () => {
// This should be reported/clarified with PrimeVue with a MWE.
setTimeout(() => {
if (rowIndexToFocus.value !== -1) {
const editorDiv = editorRef.value;
const rowEl = editorDiv!.querySelector(inputSelector.value);
const rowEl = editorDiv.value!.querySelector(inputSelector.value);
const inputEl = rowEl!.children[1].children[0];
// @ts-expect-error focusVisible not yet in typeshed
(inputEl as HTMLInputElement).focus({ focusVisible: true });
Expand All @@ -239,7 +238,7 @@ const focusInput = () => {
</script>

<template>
<div ref="editorRef">
<div ref="editorDiv">
<DataTable
v-if="image.metadata.length"
v-model:editing-rows="editingRows"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import arches from "arches";
import { computed, inject, ref } from "vue";
import { computed, inject, ref, useTemplateRef } from "vue";
import { useGettext } from "vue3-gettext";
import Column from "primevue/column";
Expand Down Expand Up @@ -38,7 +38,7 @@ const { valueType, valueCategory } = defineProps<{
}>();
const editingRows: Ref<Value[]> = ref([]);
const rowIndexToFocus = ref(-1);
const editorRef: Ref<HTMLDivElement | null> = ref(null);
const editorDiv = useTemplateRef("editorDiv");
const item = inject(itemKey) as Ref<ControlledListItem>;
Expand Down Expand Up @@ -233,8 +233,7 @@ const focusInput = () => {
// Note editor uses the second column.
const indexOfInputCol = valueCategory ? 1 : 0;
if (rowIndexToFocus.value !== -1) {
const editorDiv = editorRef.value;
const rowEl = editorDiv!.querySelector(inputSelector.value);
const rowEl = editorDiv.value!.querySelector(inputSelector.value);
const inputEl = rowEl!.children[indexOfInputCol].children[0];
// @ts-expect-error focusVisible not yet in typeshed
(inputEl as HTMLInputElement).focus({ focusVisible: true });
Expand All @@ -246,7 +245,7 @@ const focusInput = () => {

<template>
<div
ref="editorRef"
ref="editorDiv"
class="value-editor-container"
>
<h4>{{ headings.heading }}</h4>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { inject, ref } from "vue";
import { inject } from "vue";
import { useGettext } from "vue3-gettext";
import Button from "primevue/button";
Expand Down Expand Up @@ -27,8 +27,6 @@ const selectedKeys = defineModel<TreeSelectionKeys>("selectedKeys", {
required: true,
});
const abandonMoveRef = ref();
const selectedLanguage = inject(selectedLanguageKey) as Ref<Language>;
const systemLanguage = inject(systemLanguageKey) as Language;
Expand Down Expand Up @@ -61,7 +59,6 @@ const abandonMove = () => {
)
}}
<Button
ref="abandonMoveRef"
type="button"
class="banner-button"
:severity="shouldUseContrast() ? CONTRAST : SECONDARY"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, inject, ref, watch } from "vue";
import { computed, inject, ref, useTemplateRef, watch } from "vue";
import { useRoute } from "vue-router";
import { useGettext } from "vue3-gettext";
Expand All @@ -17,7 +17,7 @@ import LetterCircle from "@/arches_references/components/misc/LetterCircle.vue";
import ListTreeControls from "@/arches_references/components/tree/ListTreeControls.vue";
import TreeRow from "@/arches_references/components/tree/TreeRow.vue";
import type { ComponentPublicInstance, Ref } from "vue";
import type { Ref } from "vue";
import type { RouteLocationNormalizedLoadedGeneric } from "vue-router";
import type { TreePassThroughMethodOptions } from "primevue/tree";
import type { TreeExpandedKeys, TreeSelectionKeys } from "primevue/tree";
Expand Down Expand Up @@ -45,7 +45,7 @@ const movingItem: Ref<TreeNode | undefined> = ref();
const isMultiSelecting = ref(false);
const refetcher = ref(0);
const filterValue = ref("");
const treeDOMRef: Ref<ComponentPublicInstance | null> = ref(null);
const treeComponent = useTemplateRef("treeComponent");
// For next new item's pref label (input textbox)
const newLabelFormValue = ref("");
Expand Down Expand Up @@ -187,8 +187,8 @@ const expandPathsToFilterResults = (newFilterValue: string) => {
};
const getInputElement = () => {
if (treeDOMRef.value !== null) {
return treeDOMRef.value.$el.ownerDocument.querySelector(
if (treeComponent.value !== null) {
return treeComponent.value.$el.ownerDocument.querySelector(
'input[data-pc-name="pcfilter"]',
) as HTMLInputElement;
}
Expand Down Expand Up @@ -265,7 +265,7 @@ const ptNodeContent = ({ instance }: TreePassThroughMethodOptions) => {
/>
<Tree
v-if="tree"
ref="treeDOMRef"
ref="treeComponent"
:key="rerenderTree"
v-model:selection-keys="selectedKeys"
v-model:expanded-keys="expandedKeys"
Expand Down
26 changes: 13 additions & 13 deletions arches_references/src/arches_references/components/tree/TreeRow.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, inject, ref, watch } from "vue";
import { computed, inject, ref, useTemplateRef, watch } from "vue";
import { useGettext } from "vue3-gettext";
import Button from "primevue/button";
Expand Down Expand Up @@ -84,16 +84,16 @@ const { setDisplayedRow }: { setDisplayedRow: RowSetter } =
const awaitingMove = ref(false);
// Workaround for autofocusing the new list/label input boxes
// https://github.com/primefaces/primevue/issues/2397
const newListInputRef = ref();
const newLabelInputRef = ref();
watch(newLabelInputRef, () => {
if (newLabelInputRef.value) {
newLabelInputRef.value.$el.focus();
const newListInput = useTemplateRef("newListInput");
const newLabelInput = useTemplateRef("newLabelInput");
watch(newLabelInput, () => {
if (newLabelInput.value) {
newLabelInput.value.$el.focus();
}
});
watch(newListInputRef, () => {
if (newListInputRef.value) {
newListInputRef.value.$el.focus();
watch(newListInput, () => {
if (newListInput.value) {
newListInput.value.$el.focus();
}
});
Expand Down Expand Up @@ -241,13 +241,13 @@ const acceptNewItemShortcutEntry = async () => {
const triggerAcceptNewItemShortcut = () => {
if (newLabelFormValue.value.trim()) {
newLabelInputRef.value.$el.blur();
newLabelInput.value!.$el.blur();
}
};
const triggerAcceptNewListShortcut = () => {
if (newListFormValue.value.trim()) {
newListInputRef.value.$el.blur();
newListInput.value!.$el.blur();
}
};
Expand Down Expand Up @@ -281,7 +281,7 @@ const acceptNewListShortcutEntry = async () => {
>
<div v-if="isNewItem(node)">
<InputText
ref="newLabelInputRef"
ref="newLabelInput"
v-model="newLabelFormValue"
autofocus
@blur="acceptNewItemShortcutEntry"
Expand All @@ -290,7 +290,7 @@ const acceptNewListShortcutEntry = async () => {
</div>
<div v-else-if="isNewList(node)">
<InputText
ref="newListInputRef"
ref="newListInput"
v-model="newListFormValue"
autofocus
@blur="acceptNewListShortcutEntry"
Expand Down

0 comments on commit 037a1b5

Please sign in to comment.