import { useFormContext, useWatch } from 'react-hook-form'; import { ColorPicker } from '~/components/ui/color-picker'; import { Field } from '~/components/ui/field'; import { Input } from '~/components/ui/input'; import { BUTTON_CUSTOM_LIMITS, BUTTON_TEXT_COLORS } from '~/data/projects/constants'; import type { ProjectFormValues } from '~/schemas/projects/project-form'; type Measurement = keyof typeof BUTTON_CUSTOM_LIMITS; const MEASUREMENTS = Object.keys(BUTTON_CUSTOM_LIMITS) as Measurement[]; // An empty box is "not set": no width means the button fits its label; anything else must be filled in. // The form also passes stored values through here (a saved empty width is null), not only typed text. const toNumber = (optional: boolean) => (value: unknown) => { if (value === '' || value === null || value === undefined) return optional ? null : Number.NaN; return Number(value); }; // Exact button measurements for the "Custom" size export function ButtonCustomFields() { const { register, control, setValue, formState } = useFormContext(); const [borderWidth, borderColor] = useWatch({ control, name: ['arPage.buttonCustom.borderWidth', 'arPage.buttonCustom.borderColor'] }); const errors = formState.errors.arPage?.buttonCustom; return (
{MEASUREMENTS.map((key) => { const { label, min, max } = BUTTON_CUSTOM_LIMITS[key]; const optional = key === 'width'; return ( ); })}
{borderWidth > 0 && ( setValue('arPage.buttonCustom.borderColor', hex, { shouldDirty: true })} /> )}
); }