Skip to content

Forms

These notes apply to forms built on @flowcatalyst/ui and a generated API client. They solve the places where strict TypeScript, Nuxt UI inputs and generated types disagree.

Optional fields: optionalText and optionalNumber

Section titled “Optional fields: optionalText and optionalNumber”

The workspace’s TypeScript is strict, including exactOptionalPropertyTypes, and the generated client writes an optional field as include?: string: the property may be absent, never present with the value undefined. Nuxt UI’s UInput, UTextarea and UInputNumber cannot be bound to such a property: they take a string (or a number or null), not undefined, so v-model="state.include" does not compile.

optionalText and optionalNumber are the adapter. Each returns a writable ref that shows a missing value as an empty field and, when the field is emptied, removes the property from the form state instead of storing "" or undefined. The state always matches the generated type and serialises without the key.

<script setup lang="ts">
import { optionalNumber, optionalText } from "@flowcatalyst/ui";
import { reactive } from "vue";
import type { SearchRequest } from "./api/orders"; // { name: string; include?: string; limit?: number }
const state = reactive<SearchRequest>({ name: "" });
const include = optionalText(state, "include", { trim: true });
const limit = optionalNumber(state, "limit");
</script>
<template>
<UFormField label="Include" name="include"><UInput v-model="include" /></UFormField>
<!-- a UTextarea takes the same ref: <UTextarea v-model="include" /> -->
<UFormField label="Limit" name="limit"><UInputNumber v-model="limit" /></UFormField>
</template>
Helper Binds to Missing reads as Property is removed when
optionalText(state, key, { trim? }) UInput, UTextarea "" the value is "" (with trim: true, blank)
optionalNumber(state, key) UInputNumber null the value is null or NaN
  • Pass the reactive object (reactive(...), or a ref’s .value), not a copy. The key must name an optional property of the matching type (a string key for optionalText, a number key for optionalNumber); anything else is a compile error, and the package tests that.
  • 0 is a value, not empty: optionalNumber keeps it.
  • Required fields do not need this: bind them directly.

Edit dialogs: require-change and number fields

Section titled “Edit dialogs: require-change and number fields”

FwFormDialog with require-change keeps Save disabled until a value differs from the one the dialog opened with. UInputNumber commits what is typed to the form state only on blur or Enter, so the dialog also compares what the fields show (their drafts) with what they showed on opening. Save enables as soon as a number is typed, without leaving the field, and disables again when the old value is typed back. A click on Save blurs the focused field first, so the value being typed is the one that is sent.

FwFormDialog also maps a 400 validation response onto the fields (applyServiceError), shows a busy state while saving, and asks before discarding unsaved edits.

FwStatusBadge is solid by default and every solid colour passes WCAG AA. The soft and subtle variants put the colour’s text on its own 10 % tint. For success and warning that is slightly under 4.5:1 (4.38 and 4.48), so the theme draws those two variants’ text one shade darker in the light theme (green-800 and orange-800, both above 7:1). The dark theme keeps the alias. A test pins the ratios.

A form whose fields depend on a kind (a source that is HTTP or QUEUE, a generated oneOf variant) tempts one long template with a v-if="state.kind === '...'" block per variant. Write one component per variant instead.

TypeScript narrows state.kind inside a v-if, but vue-tsc type-checks the template as one generated function, and in a long template with many blocks the narrowing is lost part-way down: a v-model="state.url" in the fifth block reports that url does not exist on the union. A cast to silence it hides real mistakes. A component whose prop is the variant has nothing to narrow.

<!-- SourceForm.vue: the only place that looks at the kind -->
<script setup lang="ts">
import HttpFields from "./HttpFields.vue";
import QueueFields from "./QueueFields.vue";
import type { Source } from "./api/ingest"; // { kind: "HTTP"; url: string } | { kind: "QUEUE"; queue: string }
const source = defineModel<Source>({ required: true });
</script>
<template>
<HttpFields v-if="source.kind === 'HTTP'" v-model="source" />
<QueueFields v-else v-model="source" />
</template>
<!-- HttpFields.vue: the model is the HTTP variant, so `url` always exists -->
<script setup lang="ts">
import type { Source } from "./api/ingest";
const source = defineModel<Extract<Source, { kind: "HTTP" }>>({ required: true });
</script>
<template>
<UFormField label="URL" name="url"><UInput v-model="source.url" /></UFormField>
</template>
  • The parent keeps the one v-if chain on the kind and nothing else. Each variant component owns its fields, its optional-field adapters and its help text.
  • Changing the kind replaces the variant object (source.value = { kind: "QUEUE", queue: "" }). The old variant’s fields go with it, so none is sent to the server.
  • Rules that depend on the variant (patterns, ranges) stay server-side. The server’s validation errors come back as field errors on the same form.

Search select in a server-rendered form (Go)

Section titled “Search select in a server-rendered form (Go)”

This applies to the server-rendered kit (github.com/flowcatalyst/framework/go/ui, see The server-rendered UI kit), not to the Vue package above. From go/ui v0.6.0 the search select, a closed control that opens into a searchable list, has two modes.

  • Link mode (no Name): each option is a link built from its value by Href, so choosing navigates, or with OptionAttrs such as {"data-on:click__prevent": "@get(el.href)"} fetches the link live. Nothing is submitted.
  • Form mode (Name set): the options are native radios of that name, so a GET or POST form sends the chosen value (?app=orders). OptionAttrs go on each radio, so a filter can auto-submit with {"data-on:change": "el.form.requestSubmit()"}. With Placeholder set and Required false, a first radio with an empty value clears the choice; with Required the browser refuses an empty submission and opens the list so it can point at the control. Disabled, Invalid and DescribedBy apply in form mode only.
kit.SearchSelect(kit.SearchSelectProps{
ID: "app", Label: "Application", Name: "app", Signal: "appSearch",
Options: []kit.Option{{Value: "orders", Label: "Orders"}, {Value: "billing", Label: "Billing"}},
Selected: r.URL.Query().Get("app"),
Placeholder: "All applications",
OptionAttrs: templ.Attributes{"data-on:change": "el.form.requestSubmit()"},
})

The control keeps no selection of its own: render it again from the request on every page. Options are shown in the order given, and the search box filters them in the browser; while the search text matches none of them the list shows “No matches” (the catalogue key kit.search_select.no_matches). Without Datastar every option is still visible and works as a link or a radio.

Set Embedded: true inside a kit.Field: the field draws the visible label, description and error, the control draws no {id}-field wrapper of its own (so the field may use that id), and its label stays as a visually hidden name, because a <label for> cannot name a <summary>. Specification: ui-kit/components/search-select.md in the framework repository.

With exactOptionalPropertyTypes, a value bound with v-model must not be able to hold undefined when the Nuxt UI prop is modelValue?: T: form state holds "", not undefined. UTable’s v-model:sorting cannot be typed at all (the prop excludes undefined, the event includes it), so split it into :sorting and @update:sorting. UCheckbox emits boolean | "indeterminate".