Files
summercms/admin/src/components/form/FormField.vue
Jakub Zych 7091d09699 feat(19.6.1-02): field trigger and admin build
- fields.yaml trigger (action disable|hide, field, condition checked|unchecked) compiled to FormField.Trigger
- boot errors for malformed triggers, controlling fields that are not a switch or checkbox, and triggers in repeater rows, settings and relation forms
- Administrators permissions overrides are disabled while Superuser is on (D-06)
- SPA: trigger.ts (triggerState, applyTriggers), FORM_TRIGGER_NOTES, the trigger note in FormField, FormView tabs/banner/payload after triggers, PreviewView hide only
- regenerated admin OpenAPI and schema.d.ts; rebuilt modules/boardwalk/dist; docs and README
2026-10-22 11:43:58 +02:00

96 lines
4.4 KiB
Vue

<script setup lang="ts">
import { computed, inject } from 'vue'
import { CircleAlert, Info } from '@lucide/vue'
import type { ControllerParams, FormField, RelationOption } from '../../api/types'
import FieldRenderer from './FieldRenderer.vue'
import { FIELD_SCOPE, FORM_TRIGGER_NOTES } from './formContext'
import { groupLabelled, isPluginField, ownsLabel } from './registry'
// One form row: label (600, red aria-hidden asterisk when required), the
// control, the comment, the error line and the save notices (what a plugin
// field type changed, D-10) linked by aria-describedby. Notices are text
// interpolation only: a notice may quote markup an administrator submitted.
// A field a trigger disables (D-21) shows the trigger note between the label
// and the control, so it stays visible above a long control.
const props = defineProps<{
field: FormField
modelValue: unknown
errors?: string[]
/** Save notices of this field: warning status lines under the control. */
notices?: string[]
labels?: RelationOption[]
source?: ControllerParams | null
recordId?: number | null
/** Prefix for element ids, so two forms on a page never collide. */
idPrefix?: string
}>()
const emit = defineEmits<{ 'update:modelValue': [value: unknown] }>()
const base = computed(() => `${props.idPrefix ?? 'field'}-${props.field.name}`)
const controlId = computed(() => base.value)
const commentId = computed(() => `${base.value}-comment`)
const errorId = computed(() => `${base.value}-error`)
const noticeId = computed(() => `${base.value}-notice`)
const triggerId = computed(() => `${base.value}-trigger`)
// Notes are keyed by top-level field name: a repeater row's field (path
// `<repeater>.<field>`) never matches.
const scope = inject(FIELD_SCOPE, null)
const triggerNotes = inject(FORM_TRIGGER_NOTES, null)
const triggerNote = computed(() => triggerNotes?.value[scope ? scope.path(props.field.name) : props.field.name] ?? '')
const invalid = computed(() => (props.errors?.length ?? 0) > 0)
const noticed = computed(() => (props.notices?.length ?? 0) > 0)
const selfLabelled = computed(() => ownsLabel(props.field.type))
// A widget, partial or plugin field type row labels a role="group" host, so
// its label is a span the host names with aria-labelledby, not a label for
// an input.
const labelsGroup = computed(() => groupLabelled(props.field.type) || isPluginField(props.field))
const showComment = computed(() => !selfLabelled.value && !!props.field.comment)
const describedBy = computed(() =>
[
triggerNote.value !== '' ? triggerId.value : '',
showComment.value || (selfLabelled.value && props.field.comment) ? commentId.value : '',
invalid.value ? errorId.value : '',
noticed.value ? noticeId.value : '',
]
.filter(Boolean)
.join(' '),
)
</script>
<template>
<div class="flex min-w-0 flex-col gap-1.5" :data-field="field.name">
<span v-if="labelsGroup && field.label" :id="`${controlId}-label`" class="font-semibold">
{{ field.label }}<span v-if="field.required" class="text-danger" aria-hidden="true"> *</span>
</span>
<label v-else-if="!selfLabelled && !labelsGroup" :id="`${controlId}-label`" :for="controlId" class="font-semibold">
{{ field.label || field.name }}<span v-if="field.required" class="text-danger" aria-hidden="true"> *</span>
</label>
<p v-if="triggerNote !== ''" :id="triggerId" data-trigger-note class="flex items-center gap-2 text-[13px] text-muted">
<Info :size="14" class="shrink-0" aria-hidden="true" />
<span>{{ triggerNote }}</span>
</p>
<FieldRenderer
:field="field"
:model-value="modelValue"
:control-id="controlId"
:invalid="invalid"
:described-by="describedBy"
:labels="labels"
:source="source"
:record-id="recordId"
@update:model-value="(value: unknown) => emit('update:modelValue', value)"
/>
<p v-if="showComment" :id="commentId" class="text-[13px] text-muted">{{ field.comment }}</p>
<div v-if="noticed" :id="noticeId" role="status" data-field-notice class="summer-callout summer-callout--warning text-[13px]!">
<p v-for="(text, index) in notices" :key="index" class="summer-callout__text">{{ text }}</p>
</div>
<div v-if="invalid" :id="errorId" class="flex flex-col gap-0.5 text-[13px] text-danger">
<p v-for="(text, index) in errors" :key="index" class="flex items-center gap-1.5">
<CircleAlert :size="14" class="shrink-0" aria-hidden="true" />
<span>{{ text }}</span>
</p>
</div>
</div>
</template>