"use client"; import { Control, ControllerRenderProps, FieldValues, Path, } from "react-hook-form"; import type { LucideIcon } from "lucide-react"; import { EntryUnitLabelRow } from "@/components/entry-unit-toggle"; import { UnitNumberInput } from "@/components/unit-number-input"; import { FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { useEntryUnits } from "@/hooks/useEntryUnits"; import { unitLabel, type EntryDimension } from "@/lib/units"; interface UnitNumberItemProps { /** The label without its unit, which follows the dimension's entry units. */ label: string; dimension: EntryDimension; /** Inside the box, on its left. Absent on a tank's pressures, which carry none. */ icon?: LucideIcon; min?: number; max?: number; placeholderValue?: number; emptyValue?: null | ""; } export interface DiveUnitNumberFieldProps< TFieldValues extends FieldValues, > extends UnitNumberItemProps { control: Control; name: Path; } // One of the dive form's measured numbers: a label carrying its unit and the toggle // that flips it, the box, and the field's message. Every box of one dimension shares // the toggle's setting - flipping Maximum depth flips Average depth too - since all // of them read the one setting `useEntryUnits` keeps per dimension. export function DiveUnitNumberField({ control, name, ...item }: DiveUnitNumberFieldProps) { return ( } /> ); } /** The field's body, for a host that already holds the `FormField` - see the weight. */ export function DiveUnitNumberItem({ field, label, dimension, icon: Icon, ...input }: UnitNumberItemProps & { field: ControllerRenderProps>; }) { const { entryUnits, toggleEntryUnits } = useEntryUnits(); const units = entryUnits(dimension); const box = ( } /> ); return ( toggleEntryUnits(dimension)} > {label} ({unitLabel(dimension, units)}) {Icon ? (
{box}
) : ( box )}
); }