From 97a9565233faa3ff2a9959272a31c78d8d684a32 Mon Sep 17 00:00:00 2001 From: agent Date: Tue, 17 Nov 2026 10:00:48 +0000 Subject: [PATCH 01/11] Submit composite controls' values through one shared hidden field Disabled with the surrounding FormField, linked to the visible control so Form and ValidationSummary can clear and focus it (#66, 1.1). Co-Authored-By: Claude Opus 5.5 --- .changeset/shared-field-value.md | 5 + .../onu/src/components/choice/_combobox.tsx | 16 ++- .../src/components/choice/dual-listbox.tsx | 5 +- packages/onu/src/components/choice/rating.tsx | 5 +- .../components/choice/segmented-control.tsx | 3 +- .../onu/src/components/date/_picker-shell.tsx | 18 +-- .../src/components/date/date-picker.test.tsx | 3 +- .../onu/src/components/date/date-picker.tsx | 16 +-- .../src/components/date/date-range-picker.tsx | 17 ++- .../src/components/date/date-time-picker.tsx | 4 +- .../src/components/date/hijri-date-picker.tsx | 4 +- .../date/reporting-period-picker.tsx | 14 +-- .../onu/src/components/date/time-picker.tsx | 4 +- .../src/components/form/_field-value.test.tsx | 108 ++++++++++++++++++ .../onu/src/components/form/_field-value.tsx | 64 +++++++++++ .../onu/src/components/form/form-context.tsx | 9 ++ packages/onu/src/components/form/form.tsx | 78 +++++++------ .../onu/src/components/form/validation.ts | 16 ++- .../src/components/inputs/color-picker.tsx | 3 +- .../onu/src/components/inputs/icon-picker.tsx | 3 +- .../src/components/inputs/number-input.tsx | 5 +- .../onu/src/components/inputs/tags-input.tsx | 5 +- 22 files changed, 305 insertions(+), 100 deletions(-) create mode 100644 .changeset/shared-field-value.md create mode 100644 packages/onu/src/components/form/_field-value.test.tsx create mode 100644 packages/onu/src/components/form/_field-value.tsx diff --git a/.changeset/shared-field-value.md b/.changeset/shared-field-value.md new file mode 100644 index 00000000..9aab046e --- /dev/null +++ b/.changeset/shared-field-value.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +Composite controls (`NumberInput`, `TagsInput`, `SearchableSelect`, `ApiSelect`, the date and time pickers, `ColorPicker`, `IconPicker`, `SegmentedControl`, `DualListbox`, `Rating`) now submit their value through one shared hidden field. It is left out of the submission when the surrounding `FormField` is disabled, not only when the control itself is. `Form` clears a field's server error when such a control's value changes, and `ValidationSummary` links focus the visible control. A multiple `DatePicker` with no days chosen submits nothing (it used to submit an empty `name`), like `TagsInput` and `DualListbox`. diff --git a/packages/onu/src/components/choice/_combobox.tsx b/packages/onu/src/components/choice/_combobox.tsx index b3519808..aba1e042 100644 --- a/packages/onu/src/components/choice/_combobox.tsx +++ b/packages/onu/src/components/choice/_combobox.tsx @@ -12,6 +12,7 @@ import { Badge } from "../../ui/badge" import { Button } from "../../ui/button" import { Command, CommandGroup, CommandItem, CommandList, CommandSeparator } from "../../ui/command" import { useMergedRef } from "../../hooks/use-merged-ref" +import { FieldValue } from "../form/_field-value" import { Picker, PickerAnchor, PickerContent, PickerTrigger } from "./_picker" import { groupOptions, type ChoiceOption } from "./options" import { registerEnglish } from "../../i18n/english" @@ -547,15 +548,12 @@ export function ComboboxCore({ {list} - {name ? ( - multiple ? ( - selected.map((o) => ( - - )) - ) : ( - - ) - ) : null} + o.value) : selected[0]?.value} + control={triggerProps.id as string | undefined} + disabled={disabled} + /> {announcement || status} diff --git a/packages/onu/src/components/choice/dual-listbox.tsx b/packages/onu/src/components/choice/dual-listbox.tsx index 8fc5c950..aaee1cf7 100644 --- a/packages/onu/src/components/choice/dual-listbox.tsx +++ b/packages/onu/src/components/choice/dual-listbox.tsx @@ -8,6 +8,7 @@ import { cn } from "../../lib/utils" import { useTranslation } from "../../providers/onu-provider" import { useThaanaField, type ThaanaProp } from "../../thaana/use-thaana-field" import { Button } from "../../ui/button" +import { FieldValue } from "../form/_field-value" import { useFormField, useFormFieldControl } from "../form/form-field" import { filterOptions, normalizeOptions, type ChoiceOption, type ChoiceOptionInput } from "./options" import { registerEnglish } from "../../i18n/english" @@ -423,9 +424,7 @@ export function DualListbox({ {pane("chosen")} - {name - ? value.map((v) => ) - : null} + {region} ) diff --git a/packages/onu/src/components/choice/rating.tsx b/packages/onu/src/components/choice/rating.tsx index 30074d71..ca3a6b5b 100644 --- a/packages/onu/src/components/choice/rating.tsx +++ b/packages/onu/src/components/choice/rating.tsx @@ -6,6 +6,7 @@ import { cva, type VariantProps } from "class-variance-authority" import { Icon } from "../../icons/icon" import { cn } from "../../lib/utils" import { useOnu, useTranslation } from "../../providers/onu-provider" +import { FieldValue } from "../form/_field-value" import { useFormField, useFormFieldControl } from "../form/form-field" import { registerEnglish } from "../../i18n/english" import { choice as en_choice } from "../../locales/en/choice" @@ -171,7 +172,7 @@ export function Rating({ className={cn(ratingVariants({ size }), disabled && "opacity-50", className)} > {stars} - {name ? : null} + ) } @@ -199,7 +200,7 @@ export function Rating({ )} > {stars} - {name ? : null} + ) } diff --git a/packages/onu/src/components/choice/segmented-control.tsx b/packages/onu/src/components/choice/segmented-control.tsx index d1c01562..89060465 100644 --- a/packages/onu/src/components/choice/segmented-control.tsx +++ b/packages/onu/src/components/choice/segmented-control.tsx @@ -5,6 +5,7 @@ import { cva, type VariantProps } from "class-variance-authority" import { ToggleGroup as ToggleGroupPrimitive } from "radix-ui" import { cn } from "../../lib/utils" +import { FieldValue } from "../form/_field-value" import { useFormField, useFormFieldControl } from "../form/form-field" import { normalizeOptions, type ChoiceOptionInput } from "./options" import { useControllableState } from "../../hooks/use-controllable-state" @@ -118,7 +119,7 @@ export function SegmentedControl({ ))} - {name ? : null} + ) } diff --git a/packages/onu/src/components/date/_picker-shell.tsx b/packages/onu/src/components/date/_picker-shell.tsx index 7d9b6290..ac4db4bd 100644 --- a/packages/onu/src/components/date/_picker-shell.tsx +++ b/packages/onu/src/components/date/_picker-shell.tsx @@ -11,6 +11,7 @@ import { useOnu } from "../../providers/onu-provider" import { Button } from "../../ui/button" import { Drawer, DrawerContent, DrawerDescription, DrawerHeader, DrawerTitle, DrawerTrigger } from "../../ui/drawer" import { Popover, PopoverContent, PopoverTrigger } from "../../ui/popover" +import { FieldValue, type FieldValueValue } from "../form/_field-value" import { useFormField, useFormFieldControl } from "../form/form-field" import { registerEnglish } from "../../i18n/english" import { common as en_common } from "../../locales/en/common" @@ -85,8 +86,8 @@ interface PickerShellProps extends PickerFieldProps { icon?: SemanticIconName /** Name of the panel (dialog). */ panelLabel: string - /** Hidden form inputs for a field name. */ - hidden?: (name: string) => React.ReactNode + /** What the field submits for a field name: `[name, value]` pairs, rendered with `FieldValue`. */ + submit?: (name: string) => [string, FieldValueValue][] onClear?: () => void /** Extra footer content at the start. */ footerStart?: React.ReactNode @@ -106,7 +107,7 @@ export function PickerShell(props: PickerShellProps) { announcement, icon = "calendar", panelLabel, - hidden, + submit, onClear, footerStart, hideFooter, @@ -222,7 +223,11 @@ export function PickerShell(props: PickerShellProps) { {control["aria-label"]} ) : null} - {control.name && hidden ? hidden(control.name) : null} + {control.name && submit + ? submit(control.name).map(([name, value]) => ( + + )) + : null} {announcement} @@ -276,8 +281,3 @@ export function PickerShell(props: PickerShellProps) { ) } - -/** Hidden `` for plain form posts. */ -export function HiddenInput({ name, value }: { name: string; value: string }) { - return -} diff --git a/packages/onu/src/components/date/date-picker.test.tsx b/packages/onu/src/components/date/date-picker.test.tsx index e04a4978..1c4b4c7a 100644 --- a/packages/onu/src/components/date/date-picker.test.tsx +++ b/packages/onu/src/components/date/date-picker.test.tsx @@ -536,7 +536,8 @@ describe('DatePicker — mode="multiple"', () => { , ) expect(screen.getByRole("combobox")).toHaveTextContent("Select dates") - expect(container.querySelector('input[name="days"]')).toHaveValue("") + // Nothing is submitted with no days chosen, as a native multi-select would. + expect(container.querySelectorAll('input[name^="days"]:not(:disabled)')).toHaveLength(0) await user.click(screen.getByRole("combobox")) await user.click(day(/March 20th/)) await user.click(day(/March 5th/)) diff --git a/packages/onu/src/components/date/date-picker.tsx b/packages/onu/src/components/date/date-picker.tsx index 970fec05..99d38ec6 100644 --- a/packages/onu/src/components/date/date-picker.tsx +++ b/packages/onu/src/components/date/date-picker.tsx @@ -15,7 +15,7 @@ import type { Matcher } from "react-day-picker" import { useOnu } from "../../providers/onu-provider" import { DateCalendar } from "./date-calendar" -import { HiddenInput, PickerShell, type PickerFieldProps } from "./_picker-shell" +import { PickerShell, type PickerFieldProps } from "./_picker-shell" import { DEFAULT_DATE_FORMAT, formatDate, toISODate } from "./_utils" import { registerEnglish } from "../../i18n/english" import { date as en_date } from "../../locales/en/date" @@ -149,7 +149,7 @@ function SingleDate({ placeholder={placeholder ?? t("date.selectDate")} panelLabel={t("date.chooseDate")} announcement={value ? t("date.selected", { value: formatDate(value, "PPPP", locale.dateFns) }) : ""} - hidden={(name) => } + submit={(name) => [[name, value ? toISODate(value) : ""]]} onClear={() => setValue(null)} > {({ close }) => ( @@ -208,13 +208,7 @@ function MultipleDates({ placeholder={placeholder ?? t("date.selectDates")} panelLabel={t("date.chooseDates")} announcement={count ? summary : ""} - hidden={(name) => - value.length ? ( - value.map((d) => ) - ) : ( - - ) - } + submit={(name) => [[name, value.map(toISODate)]]} onClear={() => setValue([])} >