diff --git a/docs/choice-inputs/searchable-select.md b/docs/choice-inputs/searchable-select.md index 83e07787..654b1d14 100644 --- a/docs/choice-inputs/searchable-select.md +++ b/docs/choice-inputs/searchable-select.md @@ -108,7 +108,7 @@ While `onCreate` runs the row shows "Creating…"; if it throws, the row shows " ## Dependent fields -A field whose options depend on another (atoll → island): pass `parentValue`. The field is disabled until the parent has a value (the placeholder says which field to fill first) and **clears itself whenever the parent changes**: `onValueChange(null)` (or `[]` for multiple) is called. `options` can then be a map keyed by the parent value or a function of it. For options from an API, [API select](./api-select) takes the same props and sends the parent value with each request. +A field whose options depend on another (atoll → island): pass `parentValue`. The field is disabled until the parent has a value (the placeholder says which field to fill first) and **clears itself whenever the parent changes**: `onValueChange(null)` (or `[]` for multiple) is called. A value that arrives in the same render as its parent (react-hook-form's `reset(data)`, or an edit page that fetches both) is kept, and so is one set while the field waits for its parent. `options` can then be a map keyed by the parent value or a function of it. For options from an API, [API select](./api-select) takes the same props and sends the parent value with each request. ```tsx import { ApiSelect, SearchableSelect, Select } from '@javaabu/onu'; diff --git a/docs/choice-inputs/select.md b/docs/choice-inputs/select.md index 6b103464..ff4e6c89 100644 --- a/docs/choice-inputs/select.md +++ b/docs/choice-inputs/select.md @@ -27,8 +27,8 @@ Options with the same `group` are listed under that heading. Options may be plai | Prop | Type | Default | Description | | --------------------------------------- | ----------------------------------------- | ------------------ | ------------------------------------------------------------- | | `options` | `(ChoiceOption \| string)[]` | | Options; or pass `SelectItem` / `SelectGroup` children | -| `value` / `defaultValue` | `string` | `""` | Selected value; `""` shows the placeholder | -| `onValueChange` | `(value: string) => void` | | Also called with `""` when cleared | +| `value` / `defaultValue` | `string \| null` | `null` | Selected value; `null` shows the placeholder | +| `onValueChange` | `(value: string \| null, option) => void` | | `(null, null)` when cleared; `option` is `null` for children | | `placeholder` | `string` | "Select an option" | Text when empty | | `clearable` | `boolean` | `false` | Show a clear button when a value is set; hidden when required | | `size` | `"sm" \| "default" \| "lg"` | `"default"` | Trigger height | @@ -42,7 +42,7 @@ Other button props (`aria-label`, `id`, …) and `ref` go to the trigger button. :::info -`Select` replaces shadcn's `Select` root in the main export. It stays compatible: if its children contain `SelectTrigger` and `SelectContent`, it behaves exactly like the shadcn root. The primitive is still available from `@javaabu/onu/primitives`. Differences: it always manages the value (so it can be cleared), and an empty value is `""`. +`Select` replaces shadcn's `Select` root in the main export. It stays compatible: if its children contain `SelectTrigger` and `SelectContent`, it behaves exactly like the shadcn root. The primitive is still available from `@javaabu/onu/primitives`. Differences: it always manages the value (so it can be cleared), an empty value is `null`, and `onValueChange` also gets the option, like `SearchableSelect` and `ApiSelect`. ::: diff --git a/docs/date-and-time/date-range-picker.md b/docs/date-and-time/date-range-picker.md index da6aba60..f14d1dee 100644 --- a/docs/date-and-time/date-range-picker.md +++ b/docs/date-and-time/date-range-picker.md @@ -17,7 +17,7 @@ const [range, setRange] = React.useState({ from: null, to: null - Either end may be `null` for an open-ended range ("from 1 March onwards", "up to 31 March"). - Without `withTime`, `to` is the **end of that day** (23:59:59.999). With `withTime`, the To field's time defaults to 23:59. - If To is before From, the message "End date is before start date" shows under the fields and both triggers are marked invalid. -- With `name`, hidden inputs post `name[from]` and `name[to]` (`yyyy-MM-dd`, or ISO date-time with `withTime`); override them with `names`. +- With `name`, hidden inputs post `name[from]` and `name[to]` (`yyyy-MM-dd`, or ISO date-time with `withTime`); override them with `names`, which also works without `name`. - The fields stack in narrow containers (container query). ## Props diff --git a/docs/text-inputs/phone-input.md b/docs/text-inputs/phone-input.md index c3b081d8..3080c70a 100644 --- a/docs/text-inputs/phone-input.md +++ b/docs/text-inputs/phone-input.md @@ -3,7 +3,7 @@ title: PhoneInput sidebar_position: 9 --- -An international phone number input built on [`react-phone-number-input`](https://gitlab.com/catamphetamine/react-phone-number-input) (libphonenumber). It formats the number as you type, defaults to the Maldives (+960), has a searchable country picker built from Onu's `Popover` and `Command`, and returns an [E.164](https://en.wikipedia.org/wiki/E.164) value such as `+9607771234`. +An international phone number input built on [`react-phone-number-input`](https://gitlab.com/catamphetamine/react-phone-number-input) (libphonenumber). It formats the number as you type, defaults to the Maldives (+960), has a searchable country picker built from Onu's `Popover` and `Command`, and returns an [E.164](https://en.wikipedia.org/wiki/E.164) value such as `+9607771234`. With a `name` (or inside a `FormField`), the form submits that E.164 value, not the formatted text. ```tsx import { PhoneInput } from "@javaabu/onu" diff --git a/figma/plugin/code.js b/figma/plugin/code.js index 43bfac18..b1427456 100644 --- a/figma/plugin/code.js +++ b/figma/plugin/code.js @@ -2,7 +2,7 @@ (function() { //#region ../packages/onu/package.json - var version = "0.3.1"; + var version = "0.3.2"; //#endregion //#region ../tokens.json diff --git a/packages/onu-mcp/CHANGELOG.md b/packages/onu-mcp/CHANGELOG.md index fad5a59f..46e470a8 100644 --- a/packages/onu-mcp/CHANGELOG.md +++ b/packages/onu-mcp/CHANGELOG.md @@ -1,5 +1,9 @@ # @javaabu/onu-mcp +## 0.3.2 + +No changes in this release. + ## 0.3.1 No changes in this release. diff --git a/packages/onu-mcp/package.json b/packages/onu-mcp/package.json index 33e2ff4a..fb6a9897 100644 --- a/packages/onu-mcp/package.json +++ b/packages/onu-mcp/package.json @@ -1,6 +1,6 @@ { "name": "@javaabu/onu-mcp", - "version": "0.3.1", + "version": "0.3.2", "description": "MCP server for Onu (@javaabu/onu): gives AI assistants the component docs and the rules for writing Onu code.", "license": "MIT", "author": { diff --git a/packages/onu/CHANGELOG.md b/packages/onu/CHANGELOG.md index 64a6858c..069bee74 100644 --- a/packages/onu/CHANGELOG.md +++ b/packages/onu/CHANGELOG.md @@ -1,5 +1,20 @@ # @javaabu/onu +## 0.3.2 + +### Patch Changes + +- ac87774: `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. +- b3b347a: `DateRangePicker` submits under `names` (for example `{ from: "start", to: "end" }`) without `name` too. It used to submit nothing unless `name` was also set. +- aa000a1: 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. +- 4be1c44: `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([])} >