diff --git a/.changeset/button-data-slot.md b/.changeset/button-data-slot.md new file mode 100644 index 00000000..20829afa --- /dev/null +++ b/.changeset/button-data-slot.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +`Button` and `LinkButton` keep `data-slot="button"` when a caller passes its own `data-slot`, so the 44px touch-target rule still applies. Use `data-part` to tag a button. diff --git a/.changeset/date-range-names.md b/.changeset/date-range-names.md new file mode 100644 index 00000000..39b985f7 --- /dev/null +++ b/.changeset/date-range-names.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +`DateRangePicker` submits under `names` (for example `{ from: "start", to: "end" }`) without `name` too. It used to submit nothing unless `name` was also set. diff --git a/.changeset/dependent-select-load.md b/.changeset/dependent-select-load.md new file mode 100644 index 00000000..aad08198 --- /dev/null +++ b/.changeset/dependent-select-load.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +Dependent `SearchableSelect` and `ApiSelect` fields (`parentValue`) keep a value that arrives together with the parent, as with react-hook-form's `reset(data)` or an edit page that fetches both, and a value set while the field waits for its parent. They still clear when the parent changes after the value was settled. diff --git a/.changeset/form-submitter.md b/.changeset/form-submitter.md new file mode 100644 index 00000000..7ca5b741 --- /dev/null +++ b/.changeset/form-submitter.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +`Form` passes the button that submitted it to `onSubmit`'s values, so ` + { + onValueChange(v) + setData((d) => ({ ...d, island: v as string | null })) + }} + /> + + ) + } + renderWithOnu() + await userEvent.click(screen.getByRole("button", { name: "Load" })) + expect(screen.getByRole("combobox", { name: "Island" })).toHaveTextContent("Malé") + expect(onValueChange).not.toHaveBeenCalled() + }) + + it("keeps a default value while the parent loads, then clears on a real parent change", async () => { + const onValueChange = vi.fn() + const props = { "aria-label": "Island", options: islandsByAtoll, defaultValue: "male", onValueChange } + const { rerender } = renderWithOnu() + rerender() + expect(screen.getByRole("combobox", { name: "Island" })).toHaveTextContent("Malé") + expect(onValueChange).not.toHaveBeenCalled() + rerender() + expect(onValueChange).toHaveBeenLastCalledWith(null, null) + }) + + it("keeps an ApiSelect value that arrives together with its parent", async () => { + vi.stubGlobal( + "fetch", + vi.fn(async () => new Response(JSON.stringify({ data: [] }))), + ) + const onValueChange = vi.fn() + const client = newClient() + const props = { "aria-label": "Island", url: "/api/islands", debounce: 0, queryClient: client, onValueChange } + const { rerender } = renderWithOnu() + rerender() + expect(onValueChange).not.toHaveBeenCalled() + }) + it("sends the parent with ApiSelect requests and clears on change", async () => { const fetchMock = vi.fn(async () => new Response(JSON.stringify({ data: [{ id: 9, name: "Thinadhoo" }] }))) vi.stubGlobal("fetch", fetchMock) diff --git a/packages/onu/src/components/choice/api-select.tsx b/packages/onu/src/components/choice/api-select.tsx index f874156a..a7fa2510 100644 --- a/packages/onu/src/components/choice/api-select.tsx +++ b/packages/onu/src/components/choice/api-select.tsx @@ -8,7 +8,7 @@ import { useTranslation } from "../../providers/onu-provider" import { useFormFieldControl } from "../form/form-field" import { ComboboxCore, type ComboboxPresentationProps } from "./_combobox" import { useQuickCreate, type QuickCreateProps } from "./_create" -import { useOnChange, useParentField, type ParentFieldProps } from "./_hooks" +import { useParentChange, useParentField, type ParentFieldProps } from "./_hooks" import { useChoiceSelection, useOpenState, type SelectionProps } from "./_selection" import { apiOptionsQueryKey, @@ -271,8 +271,8 @@ export function ApiSelect(props: ApiSelectProps) { }) // A new parent value: clear the child (R16.5 dependent use). - useOnChange([dependsOn, parent], () => { - if (selection.values.length) selection.clear() + useParentChange([dependsOn, parent], selection.values, waiting, (stale) => { + if (stale && selection.values.length) selection.clear() setQuery("") }) 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/radio-group.test.tsx b/packages/onu/src/components/choice/radio-group.test.tsx index 3103bf32..c0858003 100644 --- a/packages/onu/src/components/choice/radio-group.test.tsx +++ b/packages/onu/src/components/choice/radio-group.test.tsx @@ -20,6 +20,20 @@ const methods = [ ] describe("RadioGroup", () => { + it.each(["default", "card", "image"] as const)( + "adds the description to a caller's aria-describedby (%s variant)", + (variant) => { + renderWithOnu( + + + , + ) + const ids = screen.getByRole("radio").getAttribute("aria-describedby")!.split(" ") + expect(ids[0]).toBe("hint") + expect(document.getElementById(ids[1]!)).toHaveTextContent("For one person") + }, + ) + it("renders options and selects with the mouse", async () => { const onValueChange = vi.fn() renderWithOnu() diff --git a/packages/onu/src/components/choice/radio.tsx b/packages/onu/src/components/choice/radio.tsx index 54a01fe0..534a695d 100644 --- a/packages/onu/src/components/choice/radio.tsx +++ b/packages/onu/src/components/choice/radio.tsx @@ -91,6 +91,7 @@ export function Radio({ id: idProp, className, classNames, + "aria-describedby": describedByProp, ...props }: RadioProps) { const group = React.useContext(RadioGroupStyleContext) @@ -100,6 +101,7 @@ export function Radio({ const id = idProp ?? `radio-${generated}` const labelId = label ? `${id}-label` : undefined const descriptionId = description ? `${id}-description` : undefined + const describedBy = joinIds(describedByProp, descriptionId) const invalid = props["aria-invalid"] ?? (group.invalid || undefined) if (variant === "image") { @@ -109,7 +111,7 @@ export function Radio({ data-slot="radio-image" id={id} aria-labelledby={labelId} - aria-describedby={joinIds(props["aria-describedby"], descriptionId)} + aria-describedby={describedBy} aria-invalid={invalid} {...props} className={cn(radioImageVariants(), group.classNames?.item, classNames?.root, className)} @@ -163,7 +165,7 @@ export function Radio({ data-size={size} id={id} aria-labelledby={labelId} - aria-describedby={joinIds(props["aria-describedby"], descriptionId)} + aria-describedby={describedBy} aria-invalid={invalid} {...props} className={cn(radioCardVariants({ size }), group.classNames?.item, classNames?.root, className)} @@ -197,7 +199,7 @@ export function Radio({ const item = ( {stars} - {name ? : null} + ) } @@ -199,7 +200,7 @@ export function Rating({ )} > {stars} - {name ? : null} + ) } diff --git a/packages/onu/src/components/choice/searchable-select.tsx b/packages/onu/src/components/choice/searchable-select.tsx index bf373457..f5116856 100644 --- a/packages/onu/src/components/choice/searchable-select.tsx +++ b/packages/onu/src/components/choice/searchable-select.tsx @@ -6,7 +6,7 @@ import { useTranslation } from "../../providers/onu-provider" import { useFormFieldControl } from "../form/form-field" import { ComboboxCore, type ComboboxPresentationProps } from "./_combobox" import { useQuickCreate, type QuickCreateProps } from "./_create" -import { useOnChange, useParentField, type ParentFieldProps } from "./_hooks" +import { useParentChange, useParentField, type ParentFieldProps } from "./_hooks" import { useChoiceSelection, useOpenState, type SelectionProps } from "./_selection" import { filterOptions, normalizeOptions, type ChoiceOption, type ChoiceOptionInput } from "./options" @@ -113,8 +113,8 @@ export function SearchableSelect(props: SearchableSelectProps) { }, [optionsInput, parent]) const selection = useChoiceSelection({ multiple, value, defaultValue, onValueChange } as SelectionProps, options) // A new parent value: clear the child (R16.5). - useOnChange(parent, () => { - if (selection.values.length) selection.clear() + useParentChange(parent, selection.values, waiting, (stale) => { + if (stale && selection.values.length) selection.clear() setQuery("") }) const create = useQuickCreate({ 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/choice/select.test.tsx b/packages/onu/src/components/choice/select.test.tsx index b6feb840..875fe2b2 100644 --- a/packages/onu/src/components/choice/select.test.tsx +++ b/packages/onu/src/components/choice/select.test.tsx @@ -28,7 +28,7 @@ describe("Select", () => { await userEvent.click(trigger) expect(screen.getByText("North")).toBeInTheDocument() await userEvent.click(screen.getByRole("option", { name: "Seenu" })) - expect(onValueChange).toHaveBeenCalledWith("S") + expect(onValueChange).toHaveBeenCalledWith("S", expect.objectContaining({ value: "S", label: "Seenu" })) expect(trigger).toHaveTextContent("Seenu") }) @@ -74,22 +74,23 @@ describe("Select", () => { expect(trigger).toHaveTextContent("B") await userEvent.click(trigger) await userEvent.click(screen.getByRole("option", { name: "A" })) - expect(onValueChange).toHaveBeenCalledWith("a") + // SelectItem children have no option object to report. + expect(onValueChange).toHaveBeenCalledWith("a", null) }) it("clears optional fields, controlled", async () => { function Controlled() { - const [value, setValue] = React.useState("K") + const [value, setValue] = React.useState("K") return ( <> ` 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([])} >