feat(19.6.1-05): persist picked relation labels across FormView tab remount

- FORM_REMEMBER_LABEL inject so hosts keep picked option names
- RelationField choose() writes the label before emit
- Vitest backstops remount and tab-switch labeling

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Jakub Zych
2026-10-22 14:40:35 +02:00
parent ba0501c274
commit 14baafb27c
6 changed files with 122 additions and 4 deletions

View File

@@ -1,10 +1,11 @@
<script setup lang="ts">
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import { computed, inject, onBeforeUnmount, ref, watch } from 'vue'
import { ChevronDown, Lock, X } from '@lucide/vue'
import { api } from '../../../api/client'
import type { RelationOption } from '../../../api/types'
import { t } from '../../../app/i18n'
import type { FieldControlProps } from '../control'
import { FORM_REMEMBER_LABEL } from '../formContext'
// Relation field (D-17, D-18, D-26), three modes from the schema:
// readOnly the label from meta.labels only, no control
@@ -22,6 +23,7 @@ import type { FieldControlProps } from '../control'
// refuses the change in any case.
const props = defineProps<FieldControlProps>()
const emit = defineEmits<{ 'update:modelValue': [value: number | number[] | null] }>()
const rememberLabel = inject(FORM_REMEMBER_LABEL, undefined)
const PER_PAGE = 20
const SEARCH_DEBOUNCE = 300
@@ -226,6 +228,13 @@ function choose(entry: Entry): void {
if (entry.locked) {
return
}
if (entry.value !== null) {
rememberLabel?.(props.field.name, {
value: entry.value,
label: entry.label,
locked: entry.locked || undefined,
})
}
if (mode.value === 'multiple') {
if (entry.value !== null && !selectedIds.value.includes(entry.value)) {
emit('update:modelValue', [...selectedIds.value, entry.value])

View File

@@ -5,7 +5,7 @@
// provider (settings pages) gets the defaults of the injecting control.
import { ref, type InjectionKey, type Ref } from 'vue'
import type { FileRoutes } from '../../api/files'
import type { AdminDependentOptionsResult, AdminRecord, FormField, PermissionRecordLock } from '../../api/types'
import type { AdminDependentOptionsResult, AdminRecord, FormField, PermissionRecordLock, RelationOption } from '../../api/types'
/** Read-only view of the form's current values. */
export const FORM_VALUES: InjectionKey<Readonly<Ref<AdminRecord>>> = Symbol('summer.form.values')
@@ -171,5 +171,13 @@ export interface FieldScope {
export const FIELD_SCOPE: InjectionKey<FieldScope> = Symbol('summer.form.fieldScope')
/**
* Form hosts persist a picked relation option so a remounted RelationField
* can seed its id→label cache from props.labels; a host without a provider
* is a no-op.
*/
export const FORM_REMEMBER_LABEL: InjectionKey<(field: string, option: RelationOption) => void> =
Symbol('summer.form.rememberLabel')
/** Dependent options per field path and sibling value, for the life of a form view. */
export const DEPENDENT_CACHE: InjectionKey<Map<string, AdminDependentOptionsResult>> = Symbol('summer.form.dependentCache')